headkit
headkit

Reputation: 3327

sencha touch :: how to make store sorter ignore if first letter is small or capitalized

I wonder how to make store sorters ignore if the first letter of a name is small or capitalized? I want to have the list items with the same first letter be grouped together or at least having the small first letter beneath the capitalized and not like it is initially first having a list ob capitalized names and the following the list of small first letters.

I found something like

var sorter = new Ext.util.Sorter({
     property : 'myField',
     sorterFn: function(o1, o2) { // compare o1 and o2 } 
});

var store = new Ext.data.Store({
     model: 'myModel',
     sorters: [sorter] 
});

store.sort();

which could be usefull.

thanx!

edit: this seems to work:

var mySubStore = app.stores.myStore.findRecord('id', recordID).websites();               
    app.stores.myStore.findRecord('id', recordID).websites().getGroupString =  function(instance) {
        return instance.get('name')[0].toUpperCase();
    };

var sorter = new Ext.util.Sorter({
     property : 'name',
     sorterFn: function(o1, o2) {
        var v1 = this.getRoot(o1).get(this.property).toLowerCase(),
            v2 = this.getRoot(o2).get(this.property).toLowerCase();
        return v1 > v2 ? 1 : (v1 < v2 ? -1 : 0);
     },
    clear: function(){}
});
//mySubStore.sorters = [sorter];
mySubStore.sort(sorter);

Upvotes: 1

Views: 4879

Answers (2)

Stuart
Stuart

Reputation: 3258

Based on the 'defaultSorterFn' defined in the Ext.util.Sorter class you could just alter it to always compare the values as lowercase (or uppercase)

var sorter = new Ext.util.Sorter({
     property : 'myField',
     sorterFn: function(o1, o2) {
        var v1 = this.getRoot(o1).get(this.property).toLowerCase(),
            v2 = this.getRoot(o2).get(this.property).toLowerCase(); 

        return v1 > v2 ? 1 : (v1 < v2 ? -1 : 0);
     } 
});

var store = new Ext.data.Store({
     model: 'myModel',
     sorters: [sorter] 
});

store.sort();

Just to let you know, this is completely untested. :)

Upvotes: 3

Kai Huppmann
Kai Huppmann

Reputation: 10775

Best solution to me seems to put another property in the model and use it from the store to group and sort it like this:

//Model
Person = Ext.regModel('Person',
{
  fields:
  [
    {name: 'id', type:'int'},
    {name: 'name', type:'string'},
    {
      name: 'sortAndGroupName',
      convert: function(value, record)
      {
        return record.get('name').toUpperCase();
      }
    }
   ]
});

//Store
PersonStore = Ext.regStore('PersonStore',
{
  model: 'Person',
  sorters: [{ property: 'sortAndGroupName', direction: 'ASC'}],
  getGroupString : function(record)
  {
    return record.get('sortAndGroupName')[0];
  }
});

Upvotes: 2

Related Questions