Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Checkboxes "Select All" functionality with only one box selected initially

I have a form that contains 3 checkboxes: "Select All", "Option 1", and "Option 2".

<form id="selectionForm">     <input type="checkbox" ng-model="selectAll" >Select all     <br>     <input type="checkbox" ng-checked="selectAll" checked>Option 1     <br>     <input type="checkbox" ng-checked="selectAll">Option 2 </form> 

On the initial page load I want only Option 1 to be checked. And then if the Select All checkbox gets checked it should automatically check Option 1 and Option 2 so all are selected.

The problem is on the initial page load the ng-checked="selectAll" gets evaluated which overrides my attempt to initially check only Option 1 (selectAll = false initially), so nothing is selected.

This seems like a simple problem to solve, but I can't figure out a solution... Thanks in advance for any insights or advice!

like image 954
Cumulo Nimbus Avatar asked Dec 12 '14 20:12

Cumulo Nimbus


People also ask

How do I select all checkboxes with one checkbox?

In order to select all the checkboxes of a page, we need to create a selectAll () function through which we can select all the checkboxes together. In this section, not only we will learn to select all checkboxes, but we will also create another function that will deselect all the checked checkboxes.

Does checkbox allow multiple selection?

In case of checkbox , more than one option can be selected.


2 Answers

Another way to go about it is to use a model for the options, set default selection in the model and have your controller handle the logic of doing select all.

angular.module("app", []).controller("ctrl", function($scope){        $scope.options = [      {value:'Option1', selected:true},       {value:'Option2', selected:false}    ];        $scope.toggleAll = function() {       var toggleStatus = !$scope.isAllSelected;       angular.forEach($scope.options, function(itm){ itm.selected = toggleStatus; });         }        $scope.optionToggled = function(){      $scope.isAllSelected = $scope.options.every(function(itm){ return itm.selected; })    }  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js">    </script>  <div ng-app="app" ng-controller="ctrl">  <form id="selectionForm">      <input type="checkbox" ng-click="toggleAll()" ng-model="isAllSelected">Select all      <br>       <div ng-repeat = "option in options">          <input type="checkbox" ng-model="option.selected" ng-change="optionToggled()">{{option.value}}       </div>  </form>    {{options}}   </div>
like image 64
PSL Avatar answered Sep 20 '22 13:09

PSL


Try this:

<form id="selectionForm">     <input type="checkbox" ng-model="selectAll" >Select all     <br>     <input type="checkbox" ng-checked="selectAll || option1" ng-init="option1=true" ng-model="option1">Option 1     <br>     <input type="checkbox" ng-checked="selectAll">Option 2 </form> 
like image 45
Josep Avatar answered Sep 20 '22 13:09

Josep