【发布时间】:2014-10-19 18:33:13
【问题描述】:
我需要 AngularJS 应用程序中的分页帮助。
我的网址:localhost/myurl 给我的数据如下:
Success: {
total: 349,
per_page: 10,
current_page: 1,
last_page: 35,
from: 1,
to: 10,
data: [{
name: 'name1',
age: 'age1'
}, {
name: 'name2',
age: 'age2'
}]
}
我的看法:
<div ng-controller="DemoCtrl">
<p><strong>Page:</strong> {{tableParams.page()}}</p>
<p><strong>Count per page:</strong> {{tableParams.count()}}</p>
<table ng-table="tableParams" class="table">
<tr ng-repeat="user in $data">
<td data-title="'Name'">{{user.Name}}</td>
<td data-title="'Email'">{{user.EMAIL}}</td>
</tr>
</table>
</div>
我的 js
var app = angular.module('main', ['ngTable'])
.service('myservice', ['$http', function($http){
this.getData = function(url){
return $http.get(url);
};
}])
.controller('DemoCtrl',['$scope', 'ngTableParams', 'myservice', function($scope, ngTableParams, myservice) {
myservice.getData('http://localhost/testpagination/public/testpagination').success(function(response){
if (response.Success) {
console.log(response.Success.data);
var data = response.Success.data;
$scope.tableParams = new ngTableParams({
page: 1, // show first page
count: 10 // count per page
}, {
total: response.Success.total, // length of data
getData: function($defer, params) {
$defer.resolve(data.slice((params.page() - 1) * params.count(), params.page() * params.count()));
}
});
} else if (response.Fail) {
console.log('i got fail.');
}
}).error(function(response){
console.log('http error');
});
}]);
我是第一次获取前十个(1 到 10)个数据,但我如何才能动态进入下一页(调用第二个分页 API)?
【问题讨论】:
-
您使用的是 $routeProvider 还是 $stateProvider?根据它,您可以从 URL 中获取页面参数值,并将其传递给您的 API!
-
用我的代码更新了我的问题
标签: angularjs pagination ngtable