简体   繁体   中英

How to select all the check boxes in nested ng-repeat

I'm trying to create a list where there are multiple select all buttons to for ng-repeat check boxes. HTML code

<div class="col-xs-12"ng-repeat="items in testArray">
    <div class="col-xs-6">
        {{items.brand}}
    </div>
    <div class="col-xs-6 col-sm-offset-6">
        <button ng-click="selectAll()">select All</button>
        <div ng-repeat="i in items.form">
            <input type="checkbox"/>{{i.formName}}
        </div>
    </div>
</div>

you can try it.this example you can manipulate it according to your need https://plnkr.co/edit/qD7CABUF9GLoFCb8vDuO?p=preview

 $scope.test=function(event){

     if(event.currentTarget.checked==true)
   { 
     var togglestatus=$scope.isAllSelected;
     angular.forEach($scope.options,function(object){

       object.selected=togglestatus

     })



   }else{
      angular.forEach($scope.options,function(object){

       object.selected=false

     })
   }
   }

html:

 <body ng-app="test" ng-controller="testctrl">
       <label>
       <input type="checkbox" ng-model="isAllSelected" ng-click="test($event)">SelectAll</label>

       <div ng-repeat="option in options">
         <input type="checkbox" ng-model="option.selected">checkbox</div>

      </body> 

Please check ngChecked directive. You need to add it to your input

<input type="checkbox" ng-checked="i.isChecked" />

and then in selectAll() method set isChecked property on every item to true.

You can simply set a variable to true like this

ng-click="selectAll = true"

and then use the ng-checked directive to set the checkbox to checked when the expression inside is true

ng-checked="selectAll"

HTML

<div class="col-xs-12"ng-repeat="items in testArray">
    <div class="col-xs-6">
    {{items.brand}}
    </div>
    <div class="col-xs-6 col-sm-offset-6">
        <button ng-click="selectAll = true">select All</button>
        <div ng-repeat="i in items.form">
            <input type="checkbox"  ng-checked="selectAll"/>{{i.formName}}
        </div>
    </div>
</div>

Created a fiddle, showcasing your need for requirement "select all" - http://jsfiddle.net/deeptechtons/TKVH6/

//html

<div>
    <ul ng-controller="checkboxController">
        <li>Check All
            <input type="checkbox" ng-model="selectedAll" ng-click="checkAll()" />
        </li>
        <li ng-repeat="item in Items">
            <label>{{item.Name}}
                <input type="checkbox" ng-model="item.Selected" />
            </label>
        </li>
    </ul>
</div>

//js
angular.module("CheckAllModule", [])
    .controller("checkboxController", function checkboxController($scope) {


    $scope.Items = [{
        Name: "Item one"
    }, {
        Name: "Item two"
    }, {
        Name: "Item three"
    }];

    $scope.checkAll = function () {
        if ($scope.selectedAll) {
            $scope.selectedAll = true;
        } else {
            $scope.selectedAll = false;
        }
        angular.forEach($scope.Items, function (item) {
            item.Selected = $scope.selectedAll;
        });

    };


});

look at code snippet given below , this will give you idea.

 var app = angular.module("myApp",[]); app.controller('demoCtrl',function($scope){ $scope.itemsArray = [{name:"Test 1",forms:[{formName:"xyz 1 "},{formName:"xyz 1 "}]},{name:"Test 2",forms:[{formName:"xyz 2 "},{formName:"xyz 1 "}]},{name:"Test 3 ",forms:[{formName:"xyz 3"},{formName:"xyz 1 "}]}]; $scope.selectAll = function(item){ for(var i =0;i < item.forms.length;i++) { item.forms[i].isChecked = true; } } $scope.removeAll = function(item) { for(var i =0;i < item.forms.length;i++) { item.forms[i].isChecked = false; } } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="myApp"> <div ng-controller="demoCtrl"> <div ng-repeat="item in itemsArray"> <span>{{item.name}}</span> <button ng-click="selectAll(item)"> select All </button> <button ng-click="removeAll(item)"> Deselect All </button> <div ng-repeat="i in item.forms"> <input type="checkbox" ng-model="i.isChecked"/> </div> </div> </div> </div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM