07 Mar 2012

Custom sort names EXTJS 4 grids

I build grids all the time in EXTJS 4 and they are usually dealing with a ton of data so server side sorting is a must. EXTJS sends the header name of the grid column and and the sort direction to the server by default. That’s all fine and dandy but most of the time my column header names differ from the names I need to send to the server for proper sorting. So here is what I came up with.

First I add a config called customSort to my column configs. The customSort will be the value that is sent to the server when clicking the grid column header.

var gridColumns = [{
    text: 'Id',
    dataIndex: 'id',
    width: 75
},{
    text: 'Queue Cat',
    dataIndex: 'queue_cat',
    width: 100,
    customSort: 'DocumentQueueCategory.name'
},{
    text: 'Queued to Customer',
    dataIndex: 'queued_to_customer',
    flex: 1,
    customSort: 'User.lastname'
},{
    text: 'Locked By',
    dataIndex: 'locked_by',
    width: 150,
    customSort: 'LockedBy.lastname'
},{
    text: 'Last Act. Admin',
    dataIndex: 'last_activity_admin',
    width: 150,
    customSort: 'LastActAdmin.lastname'
},{
    text: 'Created',
    dataIndex: 'created',
    width: 110,
    format: 'm/d/y g:i a',
    xtype: 'datecolumn'
},{
    text: 'Modified',
    dataIndex: 'modified',
    width: 110,
    format: 'm/d/y g:i a',
    xtype: 'datecolumn'
}];

Then in the store for my grid I add some code to the before load listener to check for a customSort param on each column. If the param is found the store will send the customSort value to the server, if no customSort value is found it will send the column header name as normal.

listeners: {
  beforeload: function (store, options) {
    if(store.sorters.items[0]){
      var oldSortParam = store.sorters.items[0].property;
      for(var i=0; i < gridColumns.length; i++) {
        var currentCol = gridColumns[i];
        if(currentCol.sortable && currentCol.customSort && 
           currentCol.dataIndex == oldSortParam) {
             store.sorters.items[0].property = 
             currentCol.customSort;
             break;
        }
     }
    }
  }
}
05 Mar 2012

Welcome to my blog

So I finally got around to finishing my new site. It has been way too long since I have been writing so I have a lot to write about. I will be doing some screencasts also. My day job has me doing a lot of Cake PHP, Sencha EXTJS, and some really cool hardware intergration stuff. At night I have been focused on mobile web and Ruby/Ruby on Rails development. Most of my posts will be related to development, but I will probably include some posts about cars and beers too.

TAGS: Daniel

A Little About Me...

I am a professional software engineer, currently living in Florida. When I am not in front of a computer, I enjoy working on cars, Volkswagens to be specific. I am a dog lover, I have a red Min Pin named Scrappy Doo. Dunkin' Douhnuts Coffee helps me get shit done. I am also an avid beer drinker, and I prefer IPAs the hoppier the better. I like to take pictures too, although I don't have as much time to dedicate to it as I wish I did.

Categories