我遇到了一个关于ng-table和分页的问题。基本上我有两个数据集,用户可以选择在表中呈现哪个。
数据集1有2页,数据集2只有1页。现在的问题是,如果我导航到第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: {} }
});
});
欢迎任何想法!
谢谢,凯文。
http://plnkr.co/edit/Q3GLxP55bGgVB7I28kbY?p =
预览 $scope.$watch("dataset", function () {
$scope.tableParams.$params.page=1;
$scope.tableParams.reload();
});