Add a class selectively to an ng-repeat AngularJS
Asked Answered
R

3

6

I have an ng-repeat for a table, I want to be able to add a class when <td> is clicked, and remove the class when un-clicked. Multiple <td> can be selected at the same time. Right now ALL of the cities are or are not getting the class applies.

For example: (lets say nodes has 100 items)

<tr ng-repeat node in nodes>
  <td>{{node.name}}</td>
  <td>{{node.date}}</td>
  <td ng-click="toggleMe( node.city )" ng-class"{clicked : isClicked()}" >{{node.city}}</td>
</tr>

in my JS

$scope.cityArr = [];

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

$scope.isClicked = function() {
  return $scope.clicked;
};
Reface answered 12/5, 2014 at 21:11 Comment(0)
J
4

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>
Jarad answered 12/5, 2014 at 21:22 Comment(0)
A
4

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.

Armagnac answered 12/5, 2014 at 21:27 Comment(0)
S
1

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>
Spelaean answered 1/8, 2018 at 15:23 Comment(1)
Thanks @georgeawg!Furnary

© 2022 - 2024 — McMap. All rights reserved.