1

I have the following HTML

<input type="checkbox" id="cbxSelectAllPreventive"/> <label>Select All</label>
    <ul style="list-style: none;">
       <li ng-repeat="test in lists.Tests">
       </li>
    </ul>

Test is an array of complex objects having isSelected property. I want to use the checkbox as SelectAll functionality. To do this , I need to supply ng-model to the checkbox, I can supply it as a method which checks in each of the tests and returns true/false. Is there any way to do this inline, without writing a method ?

2 Answers 2

2

I only see a way to do it using a function on ng-change

 <input type="checkbox" id="cbxSelectAllPreventive" 
      ng-model="checked" ng-change="setAll(checked)"/>
...
$scope.setAll = function(isSelected){
    $scope.lists.Tests.map(function(el){
        el.isSelected = isSelected;
    })
}

Working fiddle

EDIT:

For complete two-way connection between items' selection and check box the code will be a bit more complicated.

We will use an extra variable in $scope to reflect the label of check box. Don't forget to init the variable at controller creation

<label for="cbxSelectAllPreventive">{{selectLabel}}</label>
...
$scope.selectLabel = 'Select All';

setAll function should take care of setting this variable.

$scope.setAll = function(isSelected){
    $scope.selectLabel = isSelected ? 'Deselect All' : 'Select All';
    $scope.lists.Tests.map(function(el){
        el.isSelected = isSelected;
    })
}

And finally you definitely will have an option to select/deselect individual items. For this case you have to $watch your list. Mind the third parameter true which does deep comparison otherwise it won't "notice" changes inside objects.

$scope.$watch('lists.Tests', function(){
    var text = $scope.lists.Tests.map(function(el){ return el.isSelected; }).join();
    console.log(text);
    var allSelected = $scope.lists.Tests.every(function(el){ return el.isSelected;});
    var noneSelected = $scope.lists.Tests.every(function(el){ return !el.isSelected;});
    if(noneSelected){
        $scope.selectLabel = 'Select All';
        $scope.checked = false;
    }else if(allSelected){
        $scope.selectLabel = 'Deselect All';
        $scope.checked = true;
    }
    if(!noneSelected && !allSelected) {
       $scope.selectLabel = 'Undetermined'; 
// here set the check box to undetermined (third) state
    }
}, true);

Updated fiddle

Sign up to request clarification or add additional context in comments.

2 Comments

another add on question, how can I change text to select All/ Deselect all in the same scenario ?
In my opinion this will be not the only thing you will have to trace. Obviously you will have a mechanism to select individual items and it should be reflected too. You will have to build a three-state checkbox on your own. In my updated answer I give an idea when to trugger it
0

Did you try ng-checkbox is should do exaclty what

1 Comment

How exactly does it help to modify a list in scope without an extra function in $scope?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.