【问题标题】:How to use checkbox to filter results with Angular?如何使用复选框来过滤 Angular 的结果?
【发布时间】:2013-05-06 18:15:58
【问题描述】:

我正在尝试使用复选框应用过滤器。

复选框显示正确:

<div data-ng-repeat="cust in customers">
    <input type="checkbox" data-ng-model="search.city" data-ng-true-value="{{ cust.city }}" data-ng-false-value=""/> {{ cust.city }}
</div>

但在检查任何复选框时,没有任何反应:

<table>

    <!-- table heading goes here -->

    <tbody>
        <tr data-ng-repeat="customer in customers | filter : search">
            <td >
                {{ customer.firstName }}
            </td>
            <td >
                {{ customer.lastName }}
            </td>
            <td >
                {{ customer.address }}
            </td>
            <td >
                {{ customer.city }}
            </td>
        </tr>
    </tbody>
</table>

表格显示所有客户。

我想要实现的是:当一个或多个复选框被选中时,表格必须只显示符合选中复选框条件的这些行。

我必须做些什么才能让它工作?

【问题讨论】:

    标签: javascript html angularjs checkbox angular-filters


    【解决方案1】:

    您可以将函数传递给 AngularJS 过滤器。例如:

    将你的输入标签设置为:

    <input type="checkbox" ng-model="search[cust.city]" /> {{ cust.city }}
    

    将您的过滤器设置为:

    <tr data-ng-repeat="customer in customers | filter:searchBy() ">
    

    在您的控制器中:

    function ctrl($scope) {
      $scope.customers = [...];
    
      $scope.search = {};    
      $scope.searchBy = function () {
        return function (customer) {
          if ( $scope.search[customer.city] === true ) {
            return true;
          }
        }
      };
    }
    

    如果您希望在启动时显示所有客户,只需使用来自 customers 数组的 city 初始化 $scope.search

    Here is a sample Plunker.

    【讨论】:

    • 我更喜欢这个答案。看起来它也会更快。
    • "如果您希望在启动时显示所有客户,只需使用客户数组中的城市初始化 $scope.search。" 请您再解释一下。这有什么例子吗??
    • 如果 2 个或更多客户属于同一个城市,此解决方案不会对复选框进行分组
    【解决方案2】:

    It looks like you are providing a list of customers, and when one or more is selected, display a table of customers that are in the same city as those customers that are selected.

    为此,您需要一个自定义过滤器:

    // Define our filter
    app.filter('selectedCustomerCities', function($filter) {
      return function(customers) {
        var i, len;
    
        // get customers that have been checked
        var checkedCustomers = $filter('filter')(customers, {checked: true});
    
        // Add in a check to see if any customers were selected. If none, return 
        // them all without filters
        if(checkedCustomers.length == 0) {
          return customers;
        }
    
        // get all the unique cities that come from these checked customers
        var cities = {};
        for(i = 0, len = checkedCustomers.length; i < len; ++i) {
          // if this checked customers cities isn't already in the cities object 
          // add it
          if(!cities.hasOwnProperty(checkedCustomers[i].city)) {
            cities[checkedCustomers[i].city] = true;
          }
        }
    
        // Now that we have the cities that come from the checked customers, we can
        //get all customers from those cities and return them
        var ret = [];
        for(i = 0, len = customers.length; i < len; ++i) {
          // If this customer's city exists in the cities object, add it to the 
          // return array
          if(cities[customers[i].city]) {
            ret.push(customers[i]);
          } 
        }
    
        // we have our result!
        return ret;
      };
    });
    

    然后你的标记会变成这样:

    <div data-ng-repeat="customer in customers">
      <!-- record that this customer has been selected -->
      <input type="checkbox" ng-checked="customer.checked" ng-model="customer.checked" /> {{ customer.city }}
    </div>
    
    <table>
      <!-- table heading goes here -->
      <tbody>
          <!-- use our custom filter to only display customers from the cities selected -->
          <tr data-ng-repeat="customer in customers | selectedCustomerCities">
              <td>{{ customer.firstName }}</td>
              <td>{{ customer.lastName }}</td>
              <td>{{ customer.address }}</td>
              <td>{{ customer.city }}</td>
          </tr>
      </tbody>
    </table>
    

    你可以在这个 Plunker 上看到它工作:http://plnkr.co/edit/GlHRLKECR4jeBS7Vf7TX?p=preview

    【讨论】:

    • 这几乎就是我想要的。默认情况下会显示所有客户。检查某个城市(或城市)的复选框后,应用过滤器。 span>
    • 我已经更新了我的答案。更改是微不足道的:if(checkedCustomers.length == 0) { return customers; } 基本上返回所有客户,如果没有检查。希望这一切对您有意义,如果您需要任何解释,请告诉我。
    • 这正是我想要的。除了$filter('filter')(customers, {checked: true}); 之外,我几乎都懂,你能再解释一下吗?
    • 在 AnguarJS 中,您可以在 HTML 模板或 JavaScript 本身中使用 $filter service。为了方便起见,AngularJS 本身提供了一组过滤器,例如currencylowercase 等。他们还提供了过滤器“filter”,这是我在 JavaScript 中使用的。阅读过滤器服务的文档 + 提供的过滤器“过滤器”以获取更多信息。
    • 如果您的数据来自 web api,为了避免 cannot read property 'length' of undefined 错误,您必须检查 if (!angular.isUndefined(customers))
    猜你喜欢
    • 2012-01-14
    • 1970-01-01
    • 2023-01-03
    • 1970-01-01
    • 2020-04-06
    • 1970-01-01
    • 1970-01-01
    • 2011-06-28
    • 2021-10-23
    相关资源
    最近更新 更多