snowflakes74 snowflakes74 - 5 months ago 10
AngularJS Question

How to set all the rows of ng-repeat to be selected in Angular

I have a smimple ng-repeat that displays user details. I am trying to set all the rows to be selected.

Currently I can manually click and select all the rows individually using following code:

<tr ng-repeat="room in classrooms" ng-class="{'selected': room.selected}" ng-click="select(room)">


in controller

$scope.select = function(item) {
item.selected ? item.selected = false : item.selected = true;
}


and to get data from the selected rows I use following logic

$scope.getAllSelectedRows = function()
{
var x = $filter("filter")($scope.classrooms,
{
selected: true
}, true);

console.log(x);
}


UPDATED FROM @KADIMA RESPONSE

$scope.toggleSelectAll = function()
{
angular.forEach($scope.classrooms, function(room) {
room.selected ? room.selected = false : room.selected = true;
})
}

Answer

Set up a new function in your controller:

$scope.selectAll = function() {
    angular.forEach(classrooms, function(room) {
        room.selected = true
    }
}

And then you can create a button in your html to call this function.