4

Requirement

Every time grid data is sorted - before the event is executed I want to change the store extraParams with the values of new sort properties. Like If I am sorting a column Name in DESC direction - before the event is executed I want to overwrite extraParams of store with dataIndex of Name column and direction property DESC.

My store also has remoteSort property set to true.

I am using ExtJS 4.2.

Problem

I tried sortchange event listener on grid but it is executed after the data API is called and records are loaded. What I would like to have is something like beforesortchange.

This all with remoteSort : true.

Next problem is if i call this.getStore().load(); from sortchange then my data api is called twice, which does not make sense.

Code

Grid listener:

sortchange: function(ct, column, direction, eOpts) {
    this.getStore().getProxy().extraParams = {
        'sort'  : column.dataIndex,
        'dir'   : direction
    }
    //  load() will call the data api again once the data loading is over
    //this.getStore().load();
}

I tried following grid listeners also but either I dont get new grid sort parameters or they are not called at all: beforeload, beforesync, beforeprefetch, load.

References

https://stackoverflow.com/questions/12338407/custom-function-call-after-extjs-4-grid-sort/12338906#12338906

Community
  • 1
  • 1
user1402647
  • 480
  • 4
  • 9

1 Answers1

4

Use the beforeload event to change the extraParam object before it is sent:

listeners: {
    beforeload: function(store, operation, eOpts){
        if(store.sorters && store.sorters.getCount())
        {
            var sorter = store.sorters.getAt(0);
            store.getProxy().extraParams = {
                'sort'  : sorter.property,
                'dir'   : sorter.direction
            };
        }
    }
}
Reimius
  • 5,694
  • 5
  • 24
  • 42
  • right, it sends sort and pagination parameters to the server. but `sort` parameter is in a array format and not as per my need. i have access to apis which can not be modified and need parameters in different format (sort=column, dir=asc|desc). – user1402647 Jul 17 '13 at 15:27
  • thanks a lot, it really worked like a charm :) but with slight change, extraParams is property of proxy hence the line `store.extraParams` should be `store.proxy.extraParams`. have modified your code. – user1402647 Jul 17 '13 at 19:08
  • if there is a need not to send sort parameter you can easily set sortParam: undefined – fen1ksss Jun 24 '15 at 13:29