1

We are using backbone.paginator;backgrid;backgrid-filter ;backgrid-paginator it works great we have small issue when trying sort - at server side mode -the grid by one (or more) of its columns

For Server side with are using Spring with org.springframework.data.web.PageableHandlerMethodArgumentResolver

When debugging the app it seems that the order from client side does NOT get parsed well on spring side and it falls to the default value

Two questions:

  1. Does any one know what is the parameters that should be sent to PageableHandlerMethodArgumentResolver?
  2. How can Backbone.PageableCollection can be tweak in order to achieve that?

Our Initialize Paginator Code

   Backbone.PageableCollection.extend({
            state : {
                pageSize : 15,
                firstPage : 0, /* Spring paging is 0-based */
            },

            mode : "server",
        
            /**
             * override default queryParams to fit spring pageable var names
             */
            queryParams : {
                    pageSize:"size",
                    totalRecords:"totalElements",
                    sortKey:"sort",
                    order:"sort_order",
                    directions  : { "-1": "asc", "1": "desc" }
            },
  })

P.S:

It seems that spring expect to get the data as array separated by comma instead regular. any idea how to it with backbone.paginator? or how to change spring to be able to parse paginator to support several of sorting parameters.

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
oak
  • 2,898
  • 2
  • 32
  • 65

1 Answers1

1

After doing some digging at Spring source PageableHandlerMethodArgumentResolver and at SortHandlerMethodArgumentResolver.java (can be found here) it Seems that Spring expect sort parameters to be with the format of

sort="column1,column2,column3...,order"&sort="column4,column5,...,order"

It seems that backbone.paginator does not support multiply ordering but in order to make paginator to support SortHandlerMethodArgumentResolver with singal ordering, one can override sync function to setup the vars.

Example

var sync = Backbone.PageableCollection.prototype.sync;

Backbone.PageableCollection.prototype.sync = function(method, model,
        options) {
    options.type = 'POST' // this help with unicode on tomcat and can be removed..

    /**
     * @override sync Add some code to support parsing of sorting parameters
     *           by SortHandlerMethodArgumentResolver
     */
    if (!options.data) {
        sync.apply(this, arguments)
    }

    var sortKey = _.result(this.queryParams,"sortKey");

    if (!sortKey in options.data) {
        sync.apply(this, arguments)
    }

    options.data[STR_SPRING_SORT] = options.data[sortKey]
    var orderKey = _.result(this.queryParams,"order");

    var STR_SPRING_SORT = "sort";
    var STR_SEPERATOR = ",";

    if (orderKey  in options.data) {
        options.data[STR_SPRING_SORT] = options.data[STR_SPRING_SORT] + STR_SEPERATOR
                + options.data[orderKey]
        if (sortKey !== STR_SPRING_SORT) {
            delete options.data[sortKey];
        }

        delete options.data["order"];
    }

    sync.apply(this, arguments);

};

Remark

One can force spring to change the comma and the sort string.

oak
  • 2,898
  • 2
  • 32
  • 65