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

前端之家收集整理的这篇文章主要介绍了angularjs – 如何在Restangular和Django Rest Framework中添加分页?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在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;
    });
});

我的控制器看起来像

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,url,response) {
  if (operation === "getList") {
      data._resultMeta = {
          "count": response.count,"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');
});

我不常用矩形但Django Rest Framework support pagination from query parameter

猜你在找的Angularjs相关文章