11

I am trying to display a table with a large collection of elements. I want to paginate the table and load only the elements displayed on the current page. Now, the json is loaded with $resource.

I read here that it's a good idea to pass the pagination infos (currentPage, pagesCount and elementsCount) inside the json header.

How can I access to those infos which are in the json header from angular?

Here is the basic js structure:

angular.module('App.services', ['ngResource']).
factory('List', function($resource){
    return $resource('url/of/json/:type/:id', {type:'@type', id:'@id'});
});


angular.module('App.controllers', []).
controller('ListCtrl', ['$scope', 'List', function($scope, List) {
    var $scope.list= List.query({offset: 0, limit: 100, sortType: 'DESC' });
}]);
Community
  • 1
  • 1
François Romain
  • 13,617
  • 17
  • 89
  • 123
  • possible duplicate of [How to handle pagination and count with angularjs resources?](http://stackoverflow.com/questions/16465768/how-to-handle-pagination-and-count-with-angularjs-resources) – JBCP Feb 21 '14 at 20:22

1 Answers1

14

Based on the AngularJS $resource documentation, it should be possible to do this:

List.query({offset: 0, limit: 100, sortType: 'DESC' }, function(data, responseHeaders) {
  // you can access headers here
});
holographic-principle
  • 19,688
  • 10
  • 46
  • 62
  • yes this is the way to display previous page and next page links, but what if i want to display direct links to each page ? i need the (currentPage, PagesCount) infos from the server. Or am i missing something ? – François Romain Jul 04 '13 at 00:31
  • 2
    That can be calculated based on `limit` and `offset`. And you know those. Unless `I` am missing something :) – holographic-principle Jul 04 '13 at 00:46
  • thanks for your answer. to calculate the pagination, you need the `limit` and `offset` and also the total number of elements (`elementCount`) which i would like to pass through the json header. what do you think ? – François Romain Jul 04 '13 at 00:53
  • 1
    working ! i have to pass the parameter to `responseHeaders` like this : `responseHeaders("date")` – François Romain Jul 04 '13 at 09:02