javascript - Code for select/unselect all checkboxes in an AngularJS Table -
iam confused @ point , need anyone's here. went through various examples nothing help.
i have created dynamic table, added checkboxes. whenever row selected id bound array , diplayed @ top of table.
what need is:the code functionality of select check box. , whenever rows selected select checkbox, ids has displayed.
below code table:
<table> <thead> <tr> <th> <input name="all" type="checkbox" ng-click="selectall()" /> </th> <th>id</th> <th>date</th> </tr> </thead> <tbody ng-repeat="x in cons"> <tr> <td> <input type="checkbox" name="selectedids[]" value="{{x.id}}" ng-checked="idselection.indexof(x.id) > -1" ng-click="toggleselection(x.id, idselection)"> </td> <td>{{x.id}}</td> <td>{{x.title}}</td> </tr> </tbody>
app.js:
$scope.idselection = []; $scope.toggleselection = function toggleselection(selectionname, listselection) { var idx = listselection.indexof(selectionname); // selected if (idx > -1) { listselection.splice(idx, 1); } // newly selected else { listselection.push(selectionname); } }; //$scope.selectall=function(){} //need code function work
here demo
: http://plnkr.co/edit/m9eqexrmwzrdfcui5ypx?p=preview.
will grateful, if can guide.
you need variable keep track of whether 'all' active or not. if not, create new array of item id's using array map
function, , pass idselection
. if allselected
active, pass empty array idselection
$scope.allselected = false; $scope.selectall = function() { $scope.allselected = !$scope.allselected; if($scope.allselected) { $scope.idselection = $scope.cons.map(function(item) { return item.id; }); } else { $scope.idselection = []; } }
Comments
Post a Comment