torke1
torke1

Reputation: 351

How do I get backbone-pageable to return correct length?

Hello almighty community,

I am trying to use bbGrid together with backbone-pageable. I have managed to get paging to work in the sense that I can send in page and per_page to the server and bbGrid shows the right number of items and page number. But I can not get paging to work in the grid, since my collection does not return the correct value for length.

Here is my collection:

define([ 
    'backbone-pageable',
    'models/DeliveryModel' 
], function(BackbonePageable, DeliveryModel) {

    var DeliveryCollection = Backbone.PageableCollection.extend({

        model : DeliveryModel,

        url : '/UIServices/rs/material/v1/deliveries',

        // Parse server response to handle total number of items
        parseState: function (resp, queryParams, state, options) {
            return {totalRecords : parseInt(options.xhr.getResponseHeader("X-Result-Counter"))};
        }
    });

    return DeliveryCollection;
});

I expect collection.length to return the value of X-Result-Counter, but it always returns the value of per_page. What have I missed?

Upvotes: 0

Views: 1521

Answers (2)

torke1
torke1

Reputation: 351

After a lot of examination, I have come to the conclusion that the combination bbGrid and backbone-pageable is not a good idea. You could probably hack bbGrid to work with server-side paging, but a better solution is to change grid implementation.

I decided to switch to Backgrid, which works pretty much the same as bbGrid. I had to build my own search boxes for the header columns and my own selector for selecting number of items per page, but in all that was just a few lines of code.

So my recommentation is: If you need server side pagination, sorting and searching; choose Backgrid before bbGrid!

Upvotes: 0

gbsice
gbsice

Reputation: 541

collection.length returns the length of the models contained in the collection. In this case, it's the per_page length because only the models of the current page are stored in the collection.

If you want want to retrieve the total length, you should use collection.state.totalRecords.

Upvotes: 2

Related Questions