ember-cli-pagination icon indicating copy to clipboard operation
ember-cli-pagination copied to clipboard

how to paginate remote API (Redmine) with total elements instead of total pages?

Open tommyblue opened this issue 9 years ago • 3 comments

I'm trying to paginate results from Redmine API. Redmine adds in the response the total_count meta attribute which contains the total number of elements, not the number of pages.

Is there a way to calculate pages as computed property using this field and the perPage attribute?

tommyblue avatar Aug 05 '15 10:08 tommyblue

Thanks for taking the time to log an issue. My apologies for taking so long to respond.

I'm currently triaging the backlog of issues, but I will get back to this in the next couple days. Short answer is no, there is not currently a dead simple way to do this, but it is a worthwhile thing to think about adding.

mharris717 avatar Aug 20 '15 20:08 mharris717

@tommyblue Could you please give this a try for me.

Assuming your route looks something like this:

import Ember from 'ember';
import RouteMixin from 'ember-cli-pagination/remote/route-mixin';

export default Ember.Route.extend(RouteMixin, {
  model: function(params) {
    return this.findPaged('todo',params);
  }
});

Add a paramMapping value to turn total_count into a total_pages value that the lib can use

model: function(params) {
  var mappingFunc = function(ops) {
    return Math.ceil(ops.rawVal/ops.perPage);
  };
  params.paramMapping = {total_pages: ["total_count",mappingFunc]};
  // .... whatever else you do here
  return this.findPaged('todo',params);
}

This assumes that Redmine returns the perPage value in the response. If you try this and get an error, for testing purposes try replacing ops.perPage with a hardcoded value.

return Math.ceil(ops.rawVal/10);

If this works for you, I'll add a way to do this in a more pleasing way.

mharris717 avatar Aug 26 '15 18:08 mharris717

:+1: This worked for getting this addon to work well with a jsonapi-resources backend

Dhaulagiri avatar Feb 08 '16 21:02 Dhaulagiri