【问题标题】:Angular Checkboxes “Select All” functionality not working角度复选框“全选”功能不起作用
【发布时间】:2016-12-07 20:12:13
【问题描述】:

我有这样的代码
HTML

 <div class="check_toggle" ng-click="toggleAll(payout)">                               
         select all
   <input type="checkbox" aria-label="Art"  ng-model="checkall"/>
 </div>

<table>
<thead>
  <tr>
    <th>Week</th>
    <th>Release Payment</th>
  </tr>
</thead>
<tbody>

  <tr ng-repeat="item in payout">
    <td>{{item.value}}</td>

    <td>
      <div class="checkbox pay_check">

        <input aria-label="Art" type="checkbox" ng-model="allCheckBox[$index]" ng-click="selectItem(item._id,selected,payout)">

      </div>
    </td>
  </tr>
</tbody>

控制器

 $scope.payout= [
{'_id':1, value:'Option1'},  
{'_id':2, value:'Option2'}
];

 $scope.toggleAll = function(payout) {

        $scope.checkall = !$scope.checkall;
        for(var i=0;i<payout.length;i++){
             if($scope.checkall===false){
                   $rootScope.selected=[];
                   $scope.allCheckBox[i] = $scope.checkall ;
             }else{
                 $rootScope.selected[i]=payout[i]._id;
                 $scope.allCheckBox[i] =$scope.checkall ;
             }

        }
 }


    $scope.selectItem = function(id, list,payout) {

        console.log('id',id);
        var idx = list.indexOf(id);
        if (idx > -1) {
            list.splice(idx, 1);
        } else {
            list.push(id);
        }
         if(payout.length==$rootScope.selected.length){
            $scope.checkall=true;
            console.log($scope.checkall);
            // $scope.checkall=  $scope.checkall;
            console.log('All checkboxes selected');
        }
        else{
            $scope.checkall=false;
             console.log('Not All checkboxes selected');
        }

    }

我有使用 ng-repeat 的单独复选框,并选择所有复选框。首先,当我选择所有单独的选择框时,复选框将按我的预期自动检查,并且还检查所有复选框,并按我的预期选择所有单独的复选框,但问题是,如果我先单击 checkall 复选框,然后单击所有单个项目,它将无法按预期工作(将不会根据选择选中或取消选中 checkall 复选框)。
我尝试了一些堆栈溢出的答案,但它是一样的。谁能告诉我怎么做。请

【问题讨论】:

    标签: javascript html angularjs checkbox angular-material


    【解决方案1】:

    这里的主要问题是您将 ng-model 分配给“checkall”复选框,并且 ng-click 调用了一个函数,您在其中再次定义了 $scope.checkall 的值。

    您不需要这样做,因为 ng-model 已经为您设置了该变量的值。

    查看这个小提琴,使用您提供的相同代码,但修复: https://jsfiddle.net/h46rLzhs/5/

     angular.module('MyApp',[])
        .controller('MyController', function($rootScope, $scope){
         $rootScope.selected=[];
         $scope.allCheckBox=[];
         $scope.payout= [
             {'_id':1, value:'Option1'},  
             {'_id':2, value:'Option2'}
         ];
    
         $scope.toggleAll = function(payout) {
    
        	// Line below is not needed
            //$scope.checkall = !$scope.checkall;
            for(var i in payout){
               if($scope.checkall===false){
                    $rootScope.selected=[];
                    $scope.allCheckBox[i] = $scope.checkall ;
               }else{
                    $rootScope.selected[i]=payout[i]._id;
                    $scope.allCheckBox[i] =$scope.checkall ;
               }
            }
         }
    
         $scope.selectItem = function(id, list,payout) {
    
             console.log('id',id);
             var idx = list.indexOf(id);
             if (idx > -1) {
                 list.splice(idx, 1);
             } else {
                 list.push(id);
             }
             if(payout.length==$rootScope.selected.length){
                 $scope.checkall=true;
                 console.log($scope.checkall);
                 // $scope.checkall=  $scope.checkall;
                 console.log('All checkboxes selected');
             }
             else{
                 $scope.checkall=false;
                 console.log('Not All checkboxes selected');
             }
         }
     })
    <div ng-app="MyApp">
      <div ng-controller="MyController">
        
       <div ng-click="toggleAll(payout)">
             select all
         <input type="checkbox" ng-model="checkall"/>
       </div>
    
       <table>
       <thead>
        <tr>
           <th>Week</th>
           <th>Release Payment</th>
        </tr>
      </thead>
      <tbody>
    
        <tr ng-repeat="item in payout">
          <td>{{item.value}}</td>
    
          <td>
            <input type="checkbox" ng-model="allCheckBox[$index]" ng-click="selectItem(item._id,selected,payout)">
          </td>
        </tr>
        </tbody>
       </table>
      </div>
    </div>

    【讨论】:

    • 太棒了!!!谢谢。它在 jsfiddle 中工作。但是当我将相同的代码复制到项目时,我得到 $scope.checkall is undefined in console console.log('if',$scope.checkall); 。在函数toggleAll 内,当我最初单击切换所有复选框时,但它在jsfiddle 中给出了假控制台。你能告诉我为什么我变得不确定
    • 在我点击单个项目后,如果我点击全部检查,则每次如果条件(假)都会显示全部检查
    • @codelearner :我已将 plunker 链接添加到我的答案中。这是您的代码解决了小错误。您可以简单地将其复制粘贴到您的项目中。
    【解决方案2】:

    我已修改您的代码以使其正常工作:

      $scope.checkall=false;
      $scope.allCheckBox=[];
      $rootScope.selected = [];
      $scope.payout = [{
        '_id': 1,
        value: 'Option1'
      }, {
        '_id': 2,
        value: 'Option2'
      }];
    
      $scope.toggleAll = function() {
        $scope.checkall = !$scope.checkall;
        $rootScope.selected = [];
        $scope.allCheckBox=[];
    
        for (var i = 0; i < $scope.payout.length; i++) {
          $scope.allCheckBox.push($scope.checkall);
          if ($scope.checkall)
            $rootScope.selected.push($scope.payout[i]);
        }
      }
    
    
      $scope.selectItem = function(id) {
    
        console.log('id', id);
        var idx = $rootScope.selected.indexOf(id);
        if (idx > -1) {
          $rootScope.selected.splice(idx, 1);
        } else {
          $rootScope.selected.push(id);
        }
        if ($scope.payout.length == $rootScope.selected.length) {
          $scope.checkall = true;
          console.log($scope.checkall);
          // $scope.checkall=  $scope.checkall;
          console.log('All checkboxes selected');
        } else {
          $scope.checkall = false;
          console.log('Not All checkboxes selected');
        }
    
      }
    

    html:

    <div class="check_toggle" ng-click="toggleAll()">                               
         select all
       <input type="checkbox" aria-label="Art"  ng-model="checkall"  ng-click="toggleAll()"/>
     </div>
    
    <table>
    <thead>
      <tr>
        <th>Week</th>
        <th>Release Payment</th>
      </tr>
    </thead>
    <tbody>
    
      <tr ng-repeat="item in payout">
        <td>{{item.value}}</td>
    
        <td>
          <div class="checkbox pay_check">
    
            <input aria-label="Art" type="checkbox" ng-model="allCheckBox[$index]" ng-click="selectItem(item._id)">
    
          </div>
        </td>
      </tr>
    </tbody>
    </table>
    

    Plunk:https://plnkr.co/edit/SmabE9?p=preview

    【讨论】:

    • 我添加了 plunker 链接
    • 它工作正常。但我需要更多优化代码使用 angular.forEach 而不是 for 循环
    【解决方案3】:

    我没有阅读您的问题,但要切换所有复选框,此代码将起作用。这是您必须操作它的示例。您可以在这里查看。 https://plnkr.co/edit/qD7CABUF9GLoFCb8vDuO?p=preview

    var app = angular.module("test", []);
         app.controller('testctrl',function($scope){
    
           $scope.options=[
    
             {option:"option1",
              selected:false ,
    
             },
             {option:"option1",
              selected:false ,
    
             },
             {option:"option1",
              selected:false ,
    
             },
             {option:"option1", 
              selected:false ,
    
             },
             {option:"option1",
              selected:false ,
    
             },
    
    
             ]
    
           $scope.test=function(event){
    
             if(event.currentTarget.checked==true)
           { 
             var togglestatus=$scope.isAllSelected;
             angular.forEach($scope.options,function(object){
    
               object.selected=togglestatus
    
             })
    
    
    
    <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>      
    
    
           }else{
              angular.forEach($scope.options,function(object){
    
               object.selected=false
    
             })
           }
           }
         })
    

    【讨论】:

    • 谢谢。我已经尝试过,它可以在 plunker 中使用。但不在我的代码中。你能检查一下我的问题有什么问题吗
    猜你喜欢
    • 1970-01-01
    • 2019-10-12
    • 1970-01-01
    • 1970-01-01
    • 2016-05-26
    • 1970-01-01
    • 2019-06-02
    • 1970-01-01
    相关资源
    最近更新 更多