【问题标题】:Dynamic Angular table not filtering with pagination动态角度表不使用分页过滤
【发布时间】:2015-10-06 23:52:36
【问题描述】:

感谢另一位贡献者的帮助,我有一个动态的 Angular 表来排序/过滤。根据我在另一篇文章中找到的示例,我今晚能够添加分页,但表格过滤器仅适用于第一页。

我认为它与以下表格行代码有关,但无法缩小范围。

<tr class="paginationclass" ng-repeat="row in rows |
                                   orderBy:sort.type:sort.reverse |
                                   filter:searchData |
                                   pagination: curPage * pageSize |
                                   limitTo: pageSize">
    <td ng-repeat="column in cols">{{row[column]}}</td>
</tr>

如果我能在哪里出错,我将不胜感激。谢谢!

Fiddle

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    这是因为过滤器被应用到整个集合,所以如果你过滤它,你不能再超过一页,这就是为什么第二页是空的,试着回到第一页然后您将看到您正在搜索的元素。

    我在过滤器输入上添加了ng-change 指令,因此当项目少于 pageSize 时,它​​会修改实际页面。

    我已经把你的小提琴分叉了,这里是:

    https://jsfiddle.net/ignaciovillaverde/2rxg2e0y/3/

    顺便说一下,你的“下一步”按钮的ng-disabled条件不好用,我已经修改了,看看试试看。

    【讨论】:

    • 这太棒了!感谢您花时间分叉/更新 Fiddle 并进行解释。
    【解决方案2】:

    你可以在那里观看 $scope.$watch searchData filter ..

     $scope.$watch('searchData',function(newValue , oldValue){
        $scope.rows = $filter("filter")(data,newValue);
            $scope.curPage = 0;
            $scope.numberOfPages = Math.ceil($scope.rows.length / $scope.pageSize);
        },true);
    

    这是你的 js

    var data = [
        {"id" : "3", "name" : "Item 3", "cost" : "300"}, 
        {"id" : "1", "name" : "Item 1", "cost" : "100"}, 
        {"id" : "2", "name" : "Item 2", "cost" : "200"},
        {"id" : "6", "name" : "Item 6", "cost" : "600"}, 
        {"id" : "5", "name" : "Item 5", "cost" : "500"}, 
        {"id" : "4", "name" : "Item 4", "cost" : "400"}
    ];
    angular.module('myApp', []).controller('myController', function ($scope,$filter) {
        $scope.sort = {
            type: 'id',
            reverse: false
        };
        $scope.searchData = '';
        $scope.rows = data;
        $scope.cols = Object.keys($scope.rows[0]);
    
        $scope.curPage = 0;
        $scope.pageSize = 3;
        $scope.numberOfPages =  Math.ceil($scope.rows.length / $scope.pageSize);
    
        $scope.$watch('searchData',function(newValue , oldValue){
        $scope.rows = $filter("filter")(data,newValue);
            $scope.curPage = 0;
            $scope.numberOfPages = Math.ceil($scope.rows.length / $scope.pageSize);
        },true);
    }
    );
    

    ng-repeat="row in rows | orderBy:sort.type:sort.reverse | limitTo: pageSize"

    <div class="container" ng-app="myApp" ng-controller="myController">
        <div class="row">
            <div class="col-md-8">
                <form>
                    <div class="form-group">
                        <div class="input-group">
                            <div class="input-group-addon"><i class="glyphicon glyphicon-filter"></i>
                            </div>
                            <input type="text" class="form-control" placeholder="Filter" ng-model="searchData" id="filterText" />
                        </div>
                    </div>
                </form>
            </div>
        </div>
        <div class="row">
            <div class="col-md-12">
                <div>
                    <table class="table table-bordered table-striped">
                        <thead>
                            <tr>
                                <td ng-repeat="column in cols"> 
                                    <a href="#" ng-click="sort.type = column; sort.reverse = !sort.reverse">
                                        {{column}}
                                        <span ng-show="sort.type == column && !sort.reverse" class="glyphicon glyphicon-arrow-down"></span>
                                        <span ng-show="sort.type == column && sort.reverse" class="glyphicon glyphicon-arrow-up"></span>
                                    </a>
                                </td>
                            </tr>
                        </thead>
                        <tbody>
                            <tr class="paginationclass" ng-repeat="row in rows | orderBy:sort.type:sort.reverse | limitTo: pageSize">
                                <td ng-repeat="column in cols">{{row[column]}}</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    
        <!-- pagination buttons -->
        <div class="row">
            <div class="pagination pagination-centered" ng-show="rows.length">
                <ul class="pagination-controle pagination">
                    <li>
                        <button type="button" class="btn btn-primary"
                                ng-disabled="curPage == 0"
                                ng-click="curPage=curPage-1"> &lt; PREV</button>
                    </li>
                    <li>
                        <span>Page {{curPage + 1}} of {{ numberOfPages }}</span>
                    </li>
                    <li>
                        <button type="button" class="btn btn-primary"
                                ng-disabled="curPage >= datalists.length/pageSize - 1"
                                ng-click="curPage = curPage+1">NEXT &gt;</button>
                    </li>
                </ul>
            </div>        
        </div>
    </div>
    

    【讨论】:

      【解决方案3】:

      或者更简单,只需添加以下内容:

      $scope.$watch('searchData', function() { $scope.curPage = 0; });

      因此,每当“searchData”发生任何更改时,我们都会转到第一页。

      【讨论】:

        猜你喜欢
        • 2015-02-19
        • 2015-12-05
        • 1970-01-01
        • 2016-06-29
        • 1970-01-01
        • 2017-11-29
        • 1970-01-01
        • 2016-11-06
        • 1970-01-01
        相关资源
        最近更新 更多