从ng-repeat

时间:2017-12-18 12:03:06

标签: javascript jquery css angularjs

我正在尝试uniqeley识别tablerow中的按钮,因此如果单击其中一个,我可以更改样式。这是我正在处理的表格:

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

3 个答案:

答案 0 :(得分: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 :(得分: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;
};

答案 2 :(得分:0)

要唯一标识嵌套ng-repeat中的元素,您可以通过合并Id循环中的$index和{{1}来为其分配唯一的parent循环为:

child

现在,将id="tableButton_{{$parent.$index}}_{{$index}}" parent index传递给事件,获取元素并更改其css:

child index

以下是示例数据的摘录:

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");
  };

});