简体   繁体   English

使用分页AngularJs进行复合过滤

[英]Compound filtering with pagination AngularJs

In an angular application I am working on I am trying to use compound filtering in conjunction with pagination. 在我正在进行的角度应用程序中,我正在尝试将复合过滤与分页结合使用。 There will be a table with x entries, each row with three columns. 将有一个包含x个条目的表,每个行有三列。 I have an input above each column where the user can filter the columns based on input text. 我在每列上面都有一个输入,用户可以根据输入文本过滤列。 The filters compound to narrow down the results to the best - but in my use case there still might be too many to display on one page, therefore I am trying to use pagination. 过滤器复合以将结果缩小到最佳 - 但在我的用例中,仍然可能在一个页面上显示太多,因此我尝试使用分页。

The table body looks like this: 表体看起来像这样:

  <tbody>
    <tr>
      <td class="col-md-4">
        <input type="text" ng-model="nameFilter" class="form-control" placeholder="Name">
      </td>
      <td class="col-md-4">
        <input type="text" ng-model="ageFilter" class="form-control" placeholder="Age">
      </td>
      <td class="col-md-4">
        <input type="text" ng-model="stateFilter" class="form-control" placeholder="State">
      </td>
    </tr>
    <tr data-ng-repeat="person in people | filter:{'name': nameFilter} | filter:{'age':ageFilter} | filter:{'state':stateFilter} | limitTo: itemsPerPage">
      <td class="col-md-4">{{person.name}}</td>
      <td class="col-md-4">{{person.age}}</td>
      <td class="col-md-4">{{person.state}}</td>
    </tr>
  </tbody>

The tricky part I am having is getting the pagination to work with the filters applied. 我所遇到的棘手部分是让分页符合所应用的过滤器。 I have seen an example like this one where the author uses a $watch on a singular search field and can still manage pagination. 我见过这样一个例子,作者在一个单一的搜索字段上使用$ watch并且仍然可以管理分页。 I also have seen through research there is a $watchCollection that can be used, but am not sure how to implement it. 我也通过研究看到有一个可以使用的$watchCollection ,但我不确定如何实现它。

$scope.$watchCollection('[nameFilter, ageFilter, stateFilter]', function(newValues) {
      //not sure what to do here.
      console.debug(newValues);

      $scope.filtered = '';; //really it should equal the now filtered by 3 filters group of people
      $scope.noOfPages = Math.ceil($scope.filtered.length/$scope.itemsPerPage);
    });

Are watches even the best choice to use in this case? 在这种情况下,手表是否是最佳选择? I have this working plunker as an example of what I am working on, but still need some guidance with the pagination. 我有这个工作的plunker作为我正在做的事情的一个例子,但仍然需要一些指导与分页。 One of the questions I do have is I am not sure how to make the pagination apply specifically with the list of data. 我遇到的一个问题是我不确定如何使分页专门适用于数据列表。 I have seen other examples and have mimicked but I think the filtering might be messing it up (not sure)? 我已经看过其他的例子并且模仿了但是我认为过滤可能会弄乱它(不确定)? Any help appreciated. 任何帮助赞赏。

You don't have to watch for the filters. 您不必注意过滤器。 You only need to bind the filters to your pagination. 您只需要将过滤器绑定到您的分页。 This way angular will handle everything and you don't need to write additional code in the controller. 这样,angular将处理所有内容,您无需在控制器中编写其他代码。 I updated your plunker and you can see how is everything is implemented with angular bindings. 我更新了你的plunker,你可以看到一切都是用角度绑定实现的。

Also you have limitTo filter that handles pagination 你也有limitTo过滤器来处理分页

<tr data-ng-repeat="person in people | filter:{'name': nameFilter} | filter:{'age':ageFilter} | filter:{'state':stateFilter} | limitTo: itemsPerPage: (currentPage - 1) * itemsPerPage">
              <td class="col-md-4">{{person.name}}</td>
              <td class="col-md-4">{{person.age}}</td>
              <td class="col-md-4">{{person.state}}</td>
            </tr>


 <ul uib-pagination 
        total-items="(people | filter:{'name': nameFilter} | filter:{'age':ageFilter} | filter:{'state':stateFilter}).length" 
        items-per-page="itemsPerPage"
        boundary-links="true" ng-model="currentPage"></ul>

https://plnkr.co/edit/5bW3XV3eEmomOtHJWW7x?p=preview https://plnkr.co/edit/5bW3XV3eEmomOtHJWW7x?p=preview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM