Search code examples
javascriptsortingextjsslicejsonstore

Records.slice doesn't slice sorted store


I have a jsonstore.

var speedDialStore = new Ext.data.JsonStore({
                pageSize: 4,
                proxy   : {
                    type: 'ajax',
                    url: 'speedDialListByHospitalID.html',
                    actionMethods: {
                        create: 'POST', read: 'POST', update: 'POST', destroy: 'POST'
                    },
                    reader: {
                        type: 'json',
                        root: 'results',
                        totalProperty: 'total',
                    }
                },
                fields      : [{name:"id"},{name:"code"},{name:"name"},{name:"description"},{name:"hospitalID"},{name: "buildingID"},{name: "floorID"},{name:"alarmID"},{name:"notificationFrequency"},{name:"maxCount"},{name:"start"}],
                listeners: {
                  load : function (store, records, success, operation, options) {
                    if(records != null){
                        store.loadData(records.slice(records[0].get('start'),records[0].get('start')+4));
                    }
                  } 
                }

            });

And a grid that uses this store.

My problem is when grid is sorted i need to sort all the store and just show first 4 element for the first page and second 4 element for the second page so on..

listeners:{
                        'sortchange': function(ct, column, direction, eOpts ){
                                Ext.getCmp('speedDialPaging').moveFirst();
                                speedDialStore.sort(column.dataIndex, direction);
                        }
                    },

after the slice method works, grid still shows the unsorted 4 elements. But if i add alert message or breakpoint at slice method i see that the store is sorted properly. Therefore (i think) i need to modify slice method or find any other way to solve this problem.. Server is sending all the data. We have to solve this problem here not server side.


Solution

  • change store listener like this,

    listeners: {
                      load : function (store, records, success, operation, options) {
                        if(records != null && records.length > 0){
                            function compare(a,b) {
                              if (a.get(sortIndex) < b.get(sortIndex))
                                 return -1;
                              if (a.get(sortIndex) > b.get(sortIndex))
                                return 1;
                              return 0;
                            }
                            records.sort(compare);
                            if(sortDirection == "DESC"){
                                records.reverse();
                            }
                            store.loadData(records.slice(records[0].get('start'),records[0].get('start')+4));
                        }
                      } 
                    }
    

    and sortchange listener like this,

     sortIndex = column.dataIndex;
     sortDirection = direction;
    

    it works!