Add a class selectively to an ng-repeat AngularJS

前端 未结 3 994
天命终不由人
天命终不由人 2020-12-20 21:27

I have an ng-repeat for a table, I want to be able to add a class when is clicked, and remove the class when un-clicked. Multiple

相关标签:
3条回答
  • 2020-12-20 21:33

    You don't need a special function or controller to accomplish this:

    <table>
        <tbody>
            <tr ng-repeat="node in nodes">
                <td>{{node.name}}</td>
                <td>{{node.date}}</td>
                <td ng-click="node.highlight = !node.highlight" 
                    ng-class="{ highlight: node.highlight }">
                    {{node.city}}
                </td>
            </tr>
        </tbody>
    </table>
    

    Full Plunker example: http://plnkr.co/edit/1hdcIOfz0nHb91uFWKrv

    I could show you the controller I used by it's empty except for the test data. You don't need a function.

    0 讨论(0)
  • 2020-12-20 21:48

    Alternately, the code can use a separate array and $index to set classes:

    <tr ng-repeat="node in nodes"
        ng-class="{ highlight: highlightRows[$index] }">
      <td class="x" ng-click="toggleHighlight($index)">
        X
      </td>
    

    This approach is useful if you want to separate Model data from View data.

    The DEMO

    angular.module("app", [])
    .controller("TestController", function($scope) {
      $scope.highlightRows = [];
      $scope.toggleHighlight = function(idx) {
          $scope.highlightRows[idx] = !$scope.highlightRows[idx];
      };
      $scope.nodes = [
        { name: "Alpha", date: new Date(), city: "Omaha" },
        { name: "Bravo", date: new Date(), city: "New York" },
        { name: "Charlie", date: new Date(), city: "Minneapolis" }
      ];
    })
    table {
      border-collapse: collapse;
      font-family: sans-serif;
    }
    td {
      padding: 5px;
      border: solid black 1px;
    }
    .x {
      cursor: pointer;
    }
    .highlight {
      background: yellow;
    }
    <script src="//unpkg.com/angular/angular.js"></script>
    <body ng-app="app" ng-controller="TestController">
        <table>
         <h3>Click on X to highlight</h3>
          <tbody>
            <tr ng-repeat="node in nodes"
                ng-class="{ highlight: highlightRows[$index] }">
              <td class="x" ng-click="toggleHighlight($index)">
                X
              </td>
              <td>{{node.name}}</td>
              <td>{{node.date | date}}</td>
              <td>{{node.city}}</td>
            </tr>
          </tbody>
        </table>
        highlightRows={{highlightRows}}
    </body>

    0 讨论(0)
  • 2020-12-20 21:54

    Right now there is a single clicked property on the scope that you're changing and everything refers to that. Try to put clicked on the node instead...

    $scope.toggleMe = function(node) {
      if ($scope.count > 0) {
        angular.forEach($scope.cityArr, function(value) {
          if (node.city === value) {
            node.clicked = false;
          } else {
            $scope.cityArr.push(node.city);
            node.clicked = true;
          }
        });
      } else {
        $scope.cityArr.push(node.city);
        node.clicked = true;
      }
      $scope.count = 1;
    };
    

    And in the ngRepeat...

    <tr ng-repeat node in nodes>
      <td>{{node.name}}</td>
      <td>{{node.date}}</td>
      <td ng-click="toggleMe( node )" ng-class"{clicked : node.clicked}" >{{node.city}}</td>
    </tr>
    
    0 讨论(0)
提交回复
热议问题