如果我选中所有复选框,则会在AngularJS中自动选中全选复选框,反之亦然

时间:2017-05-04 11:12:28

标签: javascript angularjs

如果我选中了顶部的check all复选框,则会检查所有复选框。但是当我单独检查每个复选框时会出现问题,应自动选中全选复选框,反之亦然。 这是代码:

{{1}}

这是plunker的链接 http://plnkr.co/edit/LFShX3PTEITLaN7fSElX?p=preview

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

您可以在全选复选框上添加ng-checked,例如:

 ng-checked="allChecked()"

其中allChecked是控制器中的一个函数,用于查明是否已选择所有名称。像,

$scope.allChecked = function() {
  return $scope.names.filter(obj => obj.select).length === $scope.names.length
}

working example

编辑:没有箭头功能,

$scope.allChecked = function() {
  var selectedNames = $scope.names.filter(function(obj) {
    return obj.select
  });
  return selectedNames.length === $scope.names.length
}

答案 1 :(得分:1)

下面给出的代码可能会对您有所帮助。一旦您从最顶层的复选框中选中或取消选中任何所有复选框,您就会完成检查全部的代码。但是对于ng-click上的Individual复选框,你调用一个函数但没有实现这个函数。这是您更新的代码。

<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

<body>

  <p>Click the table headers to change the sorting order:</p>

  <div ng-app="myApp" ng-controller="namesCtrl">
    <p>
      <button type="button" ng-click="remove($index)">Delete Selected</button>
    </p>

    <table border="1" width="100%">
      <tr>
        <th>
          <input type="checkbox" ng-model="selectAll" ng-click="checkAll()" />
        </th>
        <th></th>
        <th>Sl no</th>
        <th ng-click="orderByMe('name')">Name</th>
        <th ng-click="orderByMe('country')">Country</th>
        <th>Delete</th>
      </tr>
      <tr ng-repeat="x in names | orderBy:myOrderBy">
        <td>
          <input type="checkbox" ng-model="x.select" ng-click="xsetting(x)" />
        </td>
        <td>{{x.select}}</td>
        <td>{{$index+1}}</td>
        <td>{{x.name}}</td>
        <td>{{x.country}}</td>
        <td>
          <button type="button" ng-click="Delete(x)">Delete</button>
        </td>
      </tr>
    </table>

  </div>

  <script>
    angular.module('myApp', []).controller('namesCtrl', ['$scope', 'filterFilter', function($scope, filterFilter) {
      $scope.names = [{
        name: 'Jani',
        country: 'Norway'
      }, {
        name: 'Carl',
        country: 'Sweden'
      }, {
        name: 'Margareth',
        country: 'England'
      }, {
        name: 'Hege',
        country: 'Norway'
      }, {
        name: 'Joe',
        country: 'Denmark'
      }, {
        name: 'Gustav',
        country: 'Sweden'
      }, {
        name: 'Birgit',
        country: 'Denmark'
      }, {
        name: 'Mary',
        country: 'England'
      }, {
        name: 'Kai',
        country: 'Norway'
      }];

      //for sorting the rows    
      $scope.orderByMe = function(x) {
          $scope.myOrderBy = x;
        }
        //single row deletion
      $scope.Delete = function(x) {
        $scope.names.splice($scope.names.indexOf(x), 1);
      };
      //selecting all checkboxes in the table
      $scope.checkAll = function() {
        angular.forEach($scope.names, function(x) {
          x.select = $scope.selectAll;
        });
      };

      $scope.xsetting = function(x) {
          $scope.selectAll = true;
          angular.forEach($scope.names, function(v, k) {
            if (!v.checked) {
              $scope.selectAll = false;
            }
          });
        }
        //for selecting and deleting checked items
      $scope.remove = function() {
        $scope.names = filterFilter($scope.names, function(x) {
          return !x.select;
        });
      };

    }]);
  </script>

</body>

</html>

这是http://plnkr.co/edit/TmF4jFtRmFFKoYngSYpq?p=preview,它正在运作。