Reputation: 93
I am creting a table using ng-repeat in angularjs, and i want to change background colour of row on button click. Like on clicking yes green colour should be set and on no button red colour is set. can somebody help please
Upvotes: 0
Views: 2514
Reputation: 1185
<ul ng-repeat="vote in votes" ng-click="setSelected(vote.id)" ng-class="
{selected: vote.id === idSelectedVote}">
...
</ul>
in controller
$scope.idSelectedVote = null;
$scope.setSelected = function (idSelectedVote) {
$scope.idSelectedVote = idSelectedVote;
};
refer:http://plnkr.co/edit/SkaYSbtKFdx1I9N0xP5E?p=preview
Upvotes: 0
Reputation: 3429
Each row has an ID. All you have to do is to send this ID to the function setSelected(),
store it (in $scope.idSelectedVote
for instance), and then check for each row if the selected ID is the same as the current one. Here is a solution(see the documentation or ngClass, if needed);
$scope.idSelectedVote = null;
$scope.setSelected = function (idSelectedVote) {
$scope.idSelectedVote = idSelectedVote;
};
.
<ul ng-repeat="vote in votes" ng-click="setSelected(vote.id)" ng-class="{selected: vote.id === idSelectedVote}">
...
</ul>
Upvotes: 1