angularjs多选框




选中/取消选中所有复选框-AngularJS ng-repeat (4)

我有一个这样的结构: 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)


工作示例: 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);
    };

});

如果您正在使用Angular 1.3+,则可以使用ng-model-options中的allSelected来解决此问题,并避免手动跟踪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/


这是一个完美的小方法

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

使用一点不想要的(只是减少)和NG检查

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


这个答案与Kmart2k1的答案有类似的逻辑。 它把每个孩子的主复选框更新为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
    });
}

叉子小提琴





angularjs-ng-repeat