【问题标题】:Check/ Uncheck All checkboxes - AngularJS ng-repeat选中/取消选中所有复选框 - AngularJS ng-repeat
【发布时间】:2015-09-09 16:48:30
【问题描述】:

我的结构如下所示:http://jsfiddle.net/deeptechtons/TKVH6/

<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>

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;
    });

};


});

选中或取消选中全部选中时,会选中所有其他复选框。但是选择“检查所有”时,我取消选择其中一个项目,我希望“检查所有”以取消选择。

提前致谢!

(PS:感谢 JSFiddle 的 deeptechtons)

【问题讨论】:

  • 谁否决了这个问题,你能告诉我原因吗?将来会有所帮助。谢谢!

标签: angularjs checkbox angularjs-ng-repeat


【解决方案1】:

此答案与Kmart2k1's answer 具有相似的逻辑。它负责更新ng-repeat 中每个子项的主复选框。我没有使用array.forEach,而是使用array.some 来更快地检查是否有任何 项未被选中。

HTML:

<li ng-repeat="item in Items">
    <label>{{item.Name}}
        <input type="checkbox" ng-model="item.Selected" ng-change="notifyMaster()"/>
    </label>
</li>

Javascript

$scope.notifyMaster = function () {
    $scope.selectedAll = !$scope.Items.some(function (item) {
       return !item.Selected;  //check if ANY are unchecked
    });
}

Forked fiddle

【讨论】:

    【解决方案2】:

    如果您使用的是 Angular 1.3+,您可以使用 ng-model-options 中的 getterSetter 来解决此问题并避免手动跟踪 allSelected 状态

    HTML:

    <input type="checkbox" ng-model="allSelected" ng-model-options="{getterSetter: true}"/>
    

    JS:

    var getAllSelected = function () {
        var selectedItems = $scope.Items.filter(function (item) {
            return item.Selected;
        });
    
        return selectedItems.length === $scope.Items.length;
    }
    
    var setAllSelected = function (value) {
        angular.forEach($scope.Items, function (item) {
            item.Selected = value;
        });
    }
    
    $scope.allSelected = function (value) {
        if (value !== undefined) {
            return setAllSelected(value);
        } else {
            return getAllSelected();
        }
    }
    

    http://jsfiddle.net/2jm6x4co/

    【讨论】:

      【解决方案3】:

      这是一个巧妙的小方法

      http://jsfiddle.net/TKVH6/850/

      使用一点 undescore(只是 reduce)和 ng-checked

        <input type="checkbox" ng-model="selectedAll" ng-click="checkAll()" ng-checked="allSelected()"/>
      
      
         $scope.allSelected = function () {
            var allSelect = _.reduce($scope.Items, function (memo, todo) {
                return memo + (todo.Selected ? 1 : 0);
              }, 0);
            return (allSelect === $scope.Items.length);
        };
      

      如果您不想使用下划线,可以使用 .reduce 中内置的 javascript。

      【讨论】:

        【解决方案4】:

        您可以使用此功能检查是否在复选框更改时检查了所有记录:

        $scope.checkStatus= function() {
                    var checkCount = 0;
                    angular.forEach($scope.Items, function(item) {
                         if(item.Selected) checkCount++;
                    });
                        $scope.selectedAll = ( checkCount === $scope.Items.length);
                };
        

        查看代码:

        <input type="checkbox" ng-model="item.Selected" ng-change="checkStatus();"/>
        

        http://jsfiddle.net/TKVH6/840/

        【讨论】:

        • 对不起,我想编辑我的,但我不小心编辑了你的。我是否将其还原为正确的 jsFiddle 链接?
        【解决方案5】:

        工作示例: http://jsfiddle.net/egrendon/TKVH6/845/

        查看:

        <input type="checkbox" ng-model="item.Selected" ng-click="setCheckAll(item)" />
        

        控制器:

        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;
                });
        
            };
        
            $scope.setCheckAll = function (item) {
                //
                // Check if checkAll should be unchecked
                //
                if ($scope.selectedAll && !item.Selected) {
                    $scope.selectedAll = false;
                } 
                //
                // Check if all are checked.
                //
                var checkCount = 0;
                angular.forEach($scope.Items, function(item) {
                    if(item.Selected) checkCount++;
                });
                $scope.selectedAll = ( checkCount === $scope.Items.length);
            };
        
        });
        

        【讨论】:

          最近更新 更多