如何在Restangular和Django Rest Framework中添加分页?

时间:2022-06-12 02:51:28

In DRF I have added pagination limit to 100 'PAGINATE_BY': 100, since Restangular expects results in array form, I had to use the below meta extractor function in my angular app module

在DRF中,我将分页限制添加到100'PAGINATE_BY':100,因为Restangular期望数组形式的结果,我必须在我的角度应用程序模块中使用以下元提取器函数

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;
    });
});

and my controller looks like

我的控制器看起来像

app.controller('DataCtrl',function($scope, Restangular){

    var resource = Restangular.all('myapp/api/dataendpoint/');
        resource.getList().then(function(data){
        $scope.records = data;
    });    
}

Meta info is not available in controller, how do I paginate if there are more than 100 records available?

控制器中没有元信息,如果有超过100条记录,我该如何分页?

1 个解决方案

#1


2  

I suppose you could simply call:

我想你可以简单地打电话:

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;
});

and

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');
});

I'm not usual with Rectangular but Django Rest Framework support pagination from query parameter

我不常使用Rectangular,但Django Rest Framework支持查询参数的分页

#1


2  

I suppose you could simply call:

我想你可以简单地打电话:

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;
});

and

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');
});

I'm not usual with Rectangular but Django Rest Framework support pagination from query parameter

我不常使用Rectangular,但Django Rest Framework支持查询参数的分页