从ng-repeat更改元素的样式

问题描述 投票:1回答:3

我试图唯一地识别表格行中的按钮,所以如果单击其中一个,我可以更改样式。这是我正在处理的表格:

table

以下是代码:

<table class="table">
    <thead>
        <tr>
            <th>Allergi navn</th>
            <th>Allergi verdi</th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="items in $ctrl.allergen">
            <td>
                <b>
                    {{items.Allergennavn}}
                </b>
            </td>
            <td id="{{$index}}">
                <button 
                    id="tableButton" 
                    type="button" 
                    class="btn-grey"  
                    ng-repeat="item in $ctrl.allergenVerdiType"
                    ng-click="$ctrl.allergiAssigned($index, items, item)"
                >
                <b>
                    {{item.Allergenverdi}}
                </b>
                </button>
                <hr>
            </td>
            </tr>
        </tbody>
    </table>

和js:

ctrl.allergiAssigned = function($index, itemAllergen, itemAllergenType){
        var index = $('button').index(this);
        $(index, '#tableButton').css("background-color", "green");           
    }

我已经尝试了几种方法来引用特定的按钮元素,使用它,索引等。我还需要确认每行只有一个按钮被选中。

我还尝试传递{{$index}}来获取该行的唯一标识符,但jquery不支持语法。

根据答案更新:

<table class="table">
    <thead>
        <tr>
            <th>Allergi navn</th>
            <th>Allergi verdi</th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="items in $ctrl.allergen">
              <td>
                  <b>
                      {{items.Allergennavn}}
                  </b>
              </td>
              <td id="{{$index}}">
                  <button id="tableButton" type="button" 
                      class="btn-grey" ng-class="{activeBtn: item.active == true}"
                      ng-repeat="item in $ctrl.allergenVerdiType"
                      ng-click="$ctrl.select(items.type, item)">
                  {{item.AllergenverditypeKode}}</button>
                  <hr>
              </td>
            </tr>
          </tbody>
    </table>

  ctrl.select = function(items, index) {
        angular.forEach(items, function(item){
          item.active=false;
        });
        index.active = true;
    };

index返回undefined

javascript jquery css angularjs
3个回答
1
投票

您可以使用ng-class指令根据用户操作更改CSS类。 details

代码就是这样的。在CSS类:.activeButton:{background-color", "green"}

在按钮ng-click功能:buttonClicked[$index]=true;

在Html按钮输入中:

.....  ng-class="{'btn-grey':'btn-grey',                      
        'activeButton':<add your condition like 'buttonClicked[$index]'>}" 

1
投票

您可以尝试类似下面的代码,也可以检查您给出的工作plunker示例。

模板:

<button id="tableButton" type="button" class="defaultBtn" ng-class="{activeBtn: item.active}" ng-repeat="item in items.type" ng-click="select(items.type, item)">{{item.value}}</button>

控制器:

$scope.select= function(items, index) {
    // You can remove this loop part if you don't want to reset your selection..
    angular.forEach(items, function(item){
      item.active=false;
    });
    index.active = true;
};

0
投票

要唯一地标识嵌套ng-repeat中的元素,可以通过组合Id循环中的$indexparent循环来为其分配唯一的child

id="tableButton_{{$parent.$index}}_{{$index}}"

现在,将parent indexchild index传递给事件,获取元素并更改其css:

ng-click="assigned($parent.$index, $index)"

以下是示例数据的片段:

angular.module("app", []).controller("ctrl", function($scope) {
  $scope.items = ["Item A", "Item B"];
  $scope.buttonTypes = ["Btn1", "Btn2", "Btn3", "Btn4"];

  $scope.assigned = function(parentIndex, childIndex) {
    //Reset all buttons for one row
    var parentBtns = "#" + parentIndex + " :button";
    $(parentBtns).css("background", "transparent");
    
    //Change the selected button css
    var btn = "#tableButton_" + parentIndex + "_" + childIndex;
    $(btn).css("background", "green");
  };

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>

<body ng-app="app" ng-controller="ctrl">
  <table class="table">
    <thead>
      <tr>
        <th>Allergi navn</th>
        <th>Allergi verdi</th>
      </tr>
    </thead>
    <tbody>
      <tr ng-repeat="item in items">
        <td>
          <b>{{item}}</b>
        </td>
        <td id="{{$index}}">
          <button id="tableButton_{{$parent.$index}}_{{$index}}" type="button" class="btn-grey" ng-repeat="type in buttonTypes" ng-click="assigned($parent.$index, $index)">
                <b>{{type}}</b>
                </button>
          <hr>
        </td>
      </tr>
    </tbody>
  </table>
</body>
© www.soinside.com 2019 - 2024. All rights reserved.