Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在“选择所有复选框”中仅在AngularJS中检查筛选的复选框?_Javascript_Angularjs - Fatal编程技术网

Javascript 如何在“选择所有复选框”中仅在AngularJS中检查筛选的复选框?

Javascript 如何在“选择所有复选框”中仅在AngularJS中检查筛选的复选框?,javascript,angularjs,Javascript,Angularjs,我有一张有棱角的桌子: $scope.customers = [{ first_name: "Oded", last_name: "Taizi", id: 1, type_link: 2 }, { first_name: "Ploni", last_name: "Almoni", id: 2, type_link: 2 }, { first_name: "Fred", last_name: "Dyllan", id: 3, type_link: 2

我有一张有棱角的桌子:

$scope.customers = [{
  first_name: "Oded",
  last_name: "Taizi",
  id: 1,
  type_link: 2
}, {
  first_name: "Ploni",
  last_name: "Almoni",
  id: 2,
  type_link: 2
}, {
  first_name: "Fred",
  last_name: "Dyllan",
  id: 3,
  type_link: 2
}, {
  first_name: "Dan",
  last_name: "Omer",
  id: 4,
  type_link: 4
}, {
  first_name: "Amir",
  last_name: "Maly",
  id: 5,
  type_link: 3
}, {
  first_name: "Noa",
  last_name: "Levy",
  id: 6,
  type_link: 3
}];
这个功能是:

 $scope.checkAll = function(isCheck) {
    angular.forEach($scope.customers, function(cust) {
      cust.select = isCheck.selectAll;
    });
 };
我想通过免费搜索和分组来过滤这些数据。这对我有用。
我的问题是,单击“全选”复选框时,它会选中所有复选框,即使已过滤

我只想检查所有筛选的行

这是我的名片。

尝试按“选择”框进行筛选

为什么不在检查$scope时设置值。选择All`

$scope.checkAll = function() {
    angular.forEach($scope.customers, function(cust) {
      cust.select = $scope.selectAll;
    });
};
作为补充说明,您还可以将每个客户的复选框更改为以下内容:

<input type="checkbox" ng-model="cust.select" ng-click="cust.select = !cust.select">

您可以对过滤后的数组进行引用:

<tr ng-repeat="cust in filteredCustomers = (customers | orderBy:orderByField:reverseSort | 
    filter :searchInput | filter: {type_link: typesModel}) ">

|使用
$scope.filteredCustomers

您还可以使用过滤器复制逻辑

$filter
服务注入控制器构造函数,如下所示:

app.controller('myCtrl', function($scope,$http,$filter){ ... });
然后您可以使用
$filter(“”)
来获得相应的过滤器:

  $scope.checkAll = function(isCheck) {
        let customersBySearchInput = $filter('filter')($scope.customers, $scope.searchInput);
        let customersBySearchInputAndType = $filter('filter')(customersBySearchInput, {type_link: $scope.typesModel});

        angular.forEach(customersBySearchInputAndType, function(cust) {
          cust.select = isCheck.selectAll;
        });
     };
在多个地方有相同的逻辑不是一个好主意。因此,您可以在控制器中创建一个进行过滤的函数,然后可以在模板和其他函数中重用该函数


注意:不要忘记可测试性-通常最好减少模板中的代码复杂性,并提供执行业务或将问题委托给其他服务的控制器方法。关于可测试性,这将提高,因为您可以轻松地对控制器逻辑进行单元测试,并增加代码覆盖率。

因为使用他的函数,您也可以取消选择所有的控制器逻辑,而且这实际上根本不能回答他的问题。@George My bad,fixed.fyi您不需要将其传递给函数,因为它应该在作用域上创建,所以您可以将其更改为使用
$scope.filteredCustomers
,谢谢,它成功了。但是,
filteredCustomers
不能像
$scope那样工作。filteredCustomers
@oded是这样的,请检查以下内容:对于这两种不同的方式。。两者都有效
  $scope.checkAll = function(isCheck) {
        let customersBySearchInput = $filter('filter')($scope.customers, $scope.searchInput);
        let customersBySearchInputAndType = $filter('filter')(customersBySearchInput, {type_link: $scope.typesModel});

        angular.forEach(customersBySearchInputAndType, function(cust) {
          cust.select = isCheck.selectAll;
        });
     };