如何在 Restangular 和 Django Rest Framework 中添加分页?
How to add pagination in Restangular and Django Rest Framework?
在 DRF 中,我将分页限制添加到 100 'PAGINATE_BY': 100,
因为 Restangular 需要数组形式的结果,所以我不得不在我的 angular 应用程序模块
中使用下面的元提取函数
var app = angular.module("myapp", ["restangular"].config(function(
RestangularProvider){
RestangularProvider.setResponseExtractor(function(response, operation, what, url) {
if (operation === "getList") {
var newResponse = response.results;
newResponse._resultmeta = {
"count": response.count,
"next": response.next,
"previous": response.previous
};
return newResponse;
}
return response;
});
});
我的控制器看起来像
app.controller('DataCtrl',function($scope, Restangular){
var resource = Restangular.all('myapp/api/dataendpoint/');
resource.getList().then(function(data){
$scope.records = data;
});
}
控制器中没有元信息,如果可用记录超过 100 条,我该如何分页?
我想你可以简单地调用:
RestangularProvider.addResponseExtractor(function(data, operation, what, url, response) {
if (operation === "getList") {
data._resultmeta = {
"count": response.count,
"next": response.next,
"previous": response.previous
};
return data;
}
return response;
});
和
var page = 2;
var resource = Restangular.all('myapp/api/dataendpoint/');
resource.getList({page: page}).then(function(data){
console.log(data._resultmeta.next ? 'there is more pages' : 'You reach the end');
});
我不常和 Rectangular
但 Django Rest Framework support pagination from query parameter
在 DRF 中,我将分页限制添加到 100 'PAGINATE_BY': 100,
因为 Restangular 需要数组形式的结果,所以我不得不在我的 angular 应用程序模块
var app = angular.module("myapp", ["restangular"].config(function(
RestangularProvider){
RestangularProvider.setResponseExtractor(function(response, operation, what, url) {
if (operation === "getList") {
var newResponse = response.results;
newResponse._resultmeta = {
"count": response.count,
"next": response.next,
"previous": response.previous
};
return newResponse;
}
return response;
});
});
我的控制器看起来像
app.controller('DataCtrl',function($scope, Restangular){
var resource = Restangular.all('myapp/api/dataendpoint/');
resource.getList().then(function(data){
$scope.records = data;
});
}
控制器中没有元信息,如果可用记录超过 100 条,我该如何分页?
我想你可以简单地调用:
RestangularProvider.addResponseExtractor(function(data, operation, what, url, response) {
if (operation === "getList") {
data._resultmeta = {
"count": response.count,
"next": response.next,
"previous": response.previous
};
return data;
}
return response;
});
和
var page = 2;
var resource = Restangular.all('myapp/api/dataendpoint/');
resource.getList({page: page}).then(function(data){
console.log(data._resultmeta.next ? 'there is more pages' : 'You reach the end');
});
我不常和 Rectangular
但 Django Rest Framework support pagination from query parameter