1

我遇到了 ng-table 和分页的问题。基本上我有两个数据集,用户可以选择在表格中呈现哪个。

数据集一有 2 页,数据集二只有一页。现在的问题是,如果我导航到第 2 页然后更改到数据集 2,我会得到一个没有分页选项的空白表。

Plunker 在下面创建,您可以在其中进行测试: http ://plnkr.co/edit/0D4ih7bPNf3Jz87Rymc7

我的代码几乎只是来自 ngtable 示例的复制和粘贴:

var app = angular.module('main', ['ngTable']).
controller('DemoCtrl', function($scope, $filter, ngTableParams) {

$scope.datasets = ["1","2"];
$scope.dataset = "1";
var data1 = [{name: "One", age: 50},
            {name: "Two", age: 43},
            {name: "Three", age: 27},
            {name: "Four", age: 29},
            {name: "Five", age: 34},
            {name: "Six", age: 43},
            {name: "Seven", age: 27},
            {name: "Eight", age: 29},
            {name: "Nine", age: 34},
            {name: "Ten", age: 43},
            {name: "Eleven", age: 27},
            {name: "Twelve", age: 29},
            {name: "Thirteen", age: 34},
            {name: "Fourteen", age: 43},
            {name: "Fifteen", age: 27},
            {name: "Sixteen", age: 29}];

var data2 = [{name: "Jacob", age: 50},
            {name: "Jacob", age: 43},
            {name: "Jacob", age: 27}];

var getData = function() {
    return $scope.dataset === "1" ? data1 : data2;
};
$scope.$watch("dataset", function () {
    $scope.tableParams.reload();
});         
$scope.tableParams = new ngTableParams({
    page: 1,            // show first page
    count: 10,          // count per page
    sorting: {
        name: 'asc'     // initial sorting
    }
}, {
    total: function () { return getData().length; }, // length of data
    getData: function($defer, params) {
        var filteredData = getData();
        var orderedData = params.sorting() ?
                            $filter('orderBy')(filteredData, params.orderBy()) :
                            filteredData;

        $defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));
    },
    $scope: { $data: {} }
});

});

欢迎任何想法!

谢谢,凯文。

4

1 回答 1

2

http://plnkr.co/edit/Q3GLxP55bGgVB7I28kbY?p=preview

 $scope.$watch("dataset", function () {
          $scope.tableParams.$params.page=1;

            $scope.tableParams.reload();
        });
于 2014-08-11T14:29:34.540 回答