user3415447
user3415447

Reputation: 93

change background of row in table on button click in angularjs

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

Answers (2)

Arun
Arun

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

Ivin Raj
Ivin Raj

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>

DEMO PLUNKER

Upvotes: 1

Related Questions