使用ngTables生成分页

时间:2017-04-07 01:55:55

标签: angularjs pagination ngtable

好吧,我正在尝试生成一个ngtable,但这不会产生正确的分页,我正在关注这些示例,但分页对我不起作用,问题是什么:

function usersMainCtrl($scope, NgTableParams, usersFactory)
{
    var tableData = []

    //Table configuration
    $scope.tableParams = new NgTableParams({
        page: 1, // show first page
        count: 5 // count per page
    },{
        //total:tableData.length,
        filterDelay: 300,
        //Returns the data for rendering
        getData : function(params) {
            return usersFactory.getUsers(params.url()).then(function(response) {
                console.log(response);
                console.log(params);
                params.total(response.data.length);
                return response.data;

            });
        }
    });
}

enter image description here

1 个答案:

答案 0 :(得分:0)

我首先要说的是,我自己是一个使用ngTable的新手,但我相信这个问题与你从服务器端获取数据的事实有关。

我也试图让这个工作,并且在这里遵循ngTables示例: http://ng-table.com/#/intro/demo-real-world

在这个例子中没有明确表示,分页是由服务器处理的(实际上,这个站点的大多数例子都非常糟糕,因为他们使用的是导入的代码而不是列出)。

我是如何通过在NgTableParams中显式设置返回的数据来解决这个问题,然后ngTable处理分页本身。下面是我所拥有的一个表的示例,该表从作为Angular Factory设置的其余API中返回一些库存记录 - 这是我的控制器,它简单,但工作:

myApp.controller('stockListController', [
'$scope', '$timeout', '$q', '$http', 'stockList', 'NgTableParams', 
function ($scope, $timeout, $q, $http, stockList, NgTableParams) {
    $scope.loading = true;

   function initTable() {
       stockList.getAll({ url: "localhost:52457"})
            .$promise.then(function (response) {
                $scope.data = response;
                $scope.tableParams = new NgTableParams({
                    count: 2 // size of page
                }, {
                    dataset: response
                });
                $scope.loading = false;
            }, function (response) {
                alert('Rejected');
            });
    }

    initTable();
}

]);

这在您的场景中可能没有用(如果有大量数据,我想负载可能会很慢),但希望它可能会给你一些工作,或者另一个角度来看待。