gpt4 book ai didi

javascript - 使用分页 AngularJs 进行复合过滤

转载 作者:搜寻专家 更新时间:2023-11-01 04:29:42 24 4
gpt4 key购买 nike

在我正在开发的 Angular 应用程序中,我正在尝试将复合过滤与分页结合使用。将有一个包含 x 条目的表,每行三列。我在每列上方都有一个输入,用户可以在其中根据输入文本过滤列。过滤器复合以将结果缩小到最佳范围 - 但在我的用例中,可能仍然有太多无法显示在一页上,因此我正在尝试使用分页。

表体是这样的:

  <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>

我遇到的棘手部分是让分页与应用的过滤器一起工作。我见过像 this one where the author uses a $watch 这样的例子在单个搜索字段上,仍然可以管理分页。我还通过研究看到有一个 $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);
});

watch 是这种情况下的最佳选择吗?我有这个工作 plunker作为我正在处理的示例,但仍需要一些分页指导。我确实有一个问题是我不确定如何使分页特别适用于数据列表。我看过其他示例并进行了模仿,但我认为过滤可能会搞砸(不确定)?任何帮助表示赞赏。

最佳答案

您不必留意过滤器。您只需要将过滤器绑定(bind)到您的分页。这样 Angular 将处理所有事情,您不需要在 Controller 中编写额外的代码。我更新了你的 plunker,你可以看到一切都是如何用 Angular 绑定(bind)实现的。

还有处理分页的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

关于javascript - 使用分页 AngularJs 进行复合过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40409410/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com