【问题标题】:If I check all checkboxes the select all checkbox should automatically checked and vice-versa in AngularJS如果我选中所有复选框,则应自动选中全选复选框,反之亦然
【发布时间】:2017-05-04 20:51:03
【问题描述】:

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

    <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;
        });
      };
      //for selecting and deleting checked items
      $scope.remove = function() {
        $scope.names = filterFilter($scope.names, function(x) {
          return !x.select;
        });
      };

    }]);
  </script>

</body>

</html>

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

有人可以帮忙吗?

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    下面给出的代码可能会对您有所帮助。一旦您从最顶部的复选框中选中或取消选中任何所有复选框,然后您完成代码以选中所有。但是对于 ng-click 上的单个复选框,您调用一个函数但未实现此函数。这是您的更新代码。

    <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,它正在工作。

    【讨论】:

    • 实际上它正在部分工作。当我选中所有单个复选框时,未选中全选复选框,而是如果我选中全选复选框,然后取消选中一个,它正在工作
    • 你能告诉我你现在面临什么问题吗?
    • 对不起 KP Chundawat。很高兴提供帮助。我以为你是坦梅。实际上,当您单击顶部的全部选中时...所有复选框都被选中,然后当您取消选中单个复选框然后再次选中它时,它不起作用
    • 您可以使用我的代码,它正在工作。如果它对您有帮助,那么也请接受这个作为答案。快乐编码 :) .
    • 你的代码有错误。你为什么不明白。只是检查它。 1.单击全选复选框 2.取消选中任何单行并再次选中它。 3.现在你可以看到全选复选框没有被选中
    【解决方案2】:

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

     ng-checked="allChecked()"
    

    allChecked 是控制器中的一个函数,用于确定是否所有名称都已被选中。比如,

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

    working example

    EDIT:没有箭头功能,

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

    【讨论】:

    • 我在 Eclipse 中使用 angularjs。所以在eclipse中使用“=>”符号显示错误。有什么建议我该怎么办?
    • 是的,实际上昨天它不起作用。但是今天当我尝试使用该符号时。他们在日食中有什么问题吗?它显示该符号的错误,但工作正常。我这里还有一个问题是链接stackoverflow.com/questions/43800095/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-13
    • 2018-06-09
    • 2017-03-18
    • 1970-01-01
    • 2014-04-05
    相关资源
    最近更新 更多