Angularjs:实现全选

html:

<div class="input-group">
  <span class="input-group-addon" style="background-color: #fff;border:none">状态</span>
  <div ng-model="queryParam.status" class="ui-checkbox ui-checkbox-primary" style="margin-top: 4px;margin-bottom: 0;">
    <label class="ui-checkbox-inline">
      <input type="checkbox" ng-model="all" ng-change="selectAll()">
        <span>全部</span>
    </label>
    <label class="ui-checkbox-inline" ng-repeat = "x in List">
      <input type="checkbox" ng-model="x.checked" ng-change="selectOne()">
      <span>{{x.name}}</span>
    </label>
  </div> </div>

js:

$scope.checked = [];
$scope.selectAll = function () {
  if($scope.all) {
    $scope.checked = [];
    angular.forEach($scope.List, function (i) {
      i.checked = true;
      $scope.checked.push(i.value);
    })
  }else {
    angular.forEach($scope.List, function (i) {
      i.checked = false;
      $scope.checked = [];
    })
  }
};

$scope.selectOne = function () {
  angular.forEach($scope.List , function (i) {
    var index = $scope.checked.indexOf(i.value);
    if(i.checked && index === -1) {
      $scope.checked.push(i.value);
    } else if (!i.checked && index !== -1){
      $scope.checked.splice(index, 1);
    };
  })
  if ($scope.List.length === $scope.checked.length) {
    $scope.all = true;
  } else {
    $scope.all = false;
  }
};

 

posted @ 2017-12-05 17:09  -cyber  阅读(475)  评论(0编辑  收藏  举报