Hessius
Hessius

Reputation: 1404

Sort, group list in Sencha Touch 2.1 by first letter after hyphen if present

I have dataset to be used in a list using Sencha Touch 2.1 in which most items begin with a prefix (E.g. P-, S-, CSV- etc.). Some, however, do not.

The prefix is not relevant for sorting and grouping the list (as the prefix is not what users will be looking for). If all items had prefixes and all prefixes were single letters I would just sort by third letter. As this is not the case I really have no idea how to continue.

Thus I want to set up a conditional grouping and sorting function along the lines of: If {name} contains hyphen: sort/group by first letter after first hyphen, else: sort by first letter

Also, some of the names will be identical without the prefix (e.g P-Albumin, U-Albumin) if the rest of the string is identical I'd want the rows with "Albumin" to be sorted their prefixes. Also, even with prefixes some strings would be the same and another field e.g. "age" would differ so

{ name: 'P-Albumin', age: '40 - 50' },
{ name: 'P-Albumin', age: '20 - 30' },
{ name: 'CSV-Albumin', age: '30' },
{ name: 'ASAT', age: '30'},

Would be grouped together under the letter A, and sorted so that the row with age equalling 20 - 30 would precede the one equalling 40 - 50, and the row with the prefix CSV- in turn would precede them both and ASAT remaining last.

I appreciate any tips, pointers, help, advice I can get.

Upvotes: 0

Views: 1299

Answers (1)

Lukas K.
Lukas K.

Reputation: 861

For sorting by names you can create a converted field in the model.

Ext.define('MyApp.model.MyModel', {
extend : 'Ext.data.Model',
config : {
    fields : ['name', 'age', 
        {
        name : 'formattedName',
        type : 'string',
        convert : function(v, record) {
            var unformattedName = record.get('name');
            if (unformattedName.indexOf("-") != -1) {
                return unformattedName.substring(unformattedName
                        .indexOf("-"));
            } else {
                return unformattedName;
            }

        }
    }]
}
});

Now you can set the sort-priority in your store config

Ext.define('MyApp.store.MyStore', {
extend : 'Ext.data.Store',
config : {
    model : "MyApp.model.MyModel",
    sorters: 'formattedName, name, age'
    ...
}
});

The store first looks at the formatted name without the prefix, then the complete name including the prefix and finally the age.

Upvotes: 3

Related Questions