Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extjs sorting store by alphanumeric field

I've got a bunch of records whose names are like "Itemtype #", and when they are outputted into a tree view, they're sorted incorrectly, like so:

  • Item 1
  • Item 10
  • Item 11
  • Item 12
  • Item 13
  • Item 2
  • Item 3
  • Item 4

My model fields are defined as follows, and I am sorting on "Name":

fields: [
  { name: 'Id', defaultValue: 0, type: 'int', mapping: 'Id' },
  { name: 'Name', defaultValue: '', type: 'string', mapping: 'Name', sortType: Ext.data.SortTypes.asUCString },
  { name: 'Type', defaultValue: 0, type: 'int', mapping: 'Type' },
  { name: 'CreationDate', type: 'date', mapping: 'CreationDate' }
],

Can anybody point me in the right direction? I'm using extjs 4.0.5

like image 956
Grahame A Avatar asked Dec 02 '22 23:12

Grahame A


2 Answers

In store you have to set up how the data will display by sorters property:

var store = Ext.create('Ext.data.JsonStore', {
  .
  .
  .
  remoteSort: false, //true for server sorting
  sorters: [{
     property: 'Name',
     direction: 'DESC' // or 'ASC'
   }],
  .
  .
  .

})
like image 82
Adidi Avatar answered Dec 16 '22 20:12

Adidi


Try calling TreeStore.sort with a sorter config with a sorting callback. A simple field sort won't do in this case because the naive alpha sort isn't what you want. The example at the top of the Ext.util.Sorter doc shows how to do it in a store, but you can just as easily add it to the sorters param of your model.

like image 33
wes Avatar answered Dec 16 '22 19:12

wes