1. #1
    Sencha User
    Join Date
    Mar 2007
    Posts
    411
    Answers
    5
    Vote Rating
    17
    jweber will become famous soon enough

      1  

    Default Unanswered: Custom sort function, using association's data

    Unanswered: Custom sort function, using association's data


    I have a store, whose model contains some associations. I'd like to sort the store on one of the associated model's fields. (I don't want to sort the associated models themselves; I just want to use them to sort their owner records.)

    For example, this store contains a set of Person records, each with an associated Address. I want to sort the Persons by their address.street field:
    Code:
    Ext.define('Address', {
    	extend: 'Ext.data.Model',
    	config: {
    		fields: [{
    			name: 'street'
    		}]
    	}
    });
    
    Ext.define('Person', {
    	extend: 'Ext.data.Model',
    	config: {
    		associations: [{
    			type: 'hasOne',
    			model: 'Address',
    			name: 'address'
    		}]
    	}
    });
    
    Ext.define('People', {
    	extend: 'Ext.data.Store',
    	config: {
    		model: 'Person'
    		...
    	}
    });
    Is this possible? Where would I define the sort function?

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    36,791
    Answers
    3465
    Vote Rating
    833
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    This isn't possible AFAIK. The associated data doesn't live in that store.
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  3. #3
    Sencha User
    Join Date
    Mar 2007
    Posts
    411
    Answers
    5
    Vote Rating
    17
    jweber will become famous soon enough

      0  

    Default


    But it's readable from the store, right? Is it only possible to sort on the store's own fields? How would you deal with a calculated field? For example, you might be displaying something in a DataView that's a combination of multiple fields from a record, and you want to sort on the displayed value.

    If this isn't possible now, I think it would be a very useful addition. Something where you can define a sorter function that's given the entire record, not just a single field.

  4. #4
    Sencha User
    Join Date
    Mar 2007
    Posts
    411
    Answers
    5
    Vote Rating
    17
    jweber will become famous soon enough

      0  

    Default


    Actually, it looks like it is possible:

    Code:
    peopleStore.sort([{
    	direction: 'DESC',
    	sorterFn: function(person1, person2) {
    		var value1 = o1.getAddress().get('street'));
    		var value2 = o2.getAddress().get('street'));
    		return value1 > value2 ? 1 : (value1 < value2 ? -1 : 0);
    	}
    }]);
    This seems to work, unless I'm missing something.

  5. #5
    Sencha User
    Join Date
    Mar 2007
    Posts
    411
    Answers
    5
    Vote Rating
    17
    jweber will become famous soon enough

      0  

    Default


    In case anyone's interested, I hacked together a Store subclass that makes this easier. It uses some private Ext methods, though, so use at your own risk:

    Code:
    Ext.define('CustomSortStore', {
    	extend: 'Ext.data.Store',
    	config: {
    		sortFns: {}
    	},
    	updateSortTypes: function() {
    		var data = this.data,
    			sortFns = this.getSortFns(),
    			updated = false;
    
    		data.getSorters().each(function(sorter) {
    			var property = sorter.getProperty();
    
    			if (!sorter.isGrouper && property && sortFns[property] !== undefined) {
    				sorter.setSorterFn(sortFns[property]);
    				sorter.updateSortFn();
    				updated = true;
    			}
    		});
    		if (updated) {
    			data.updateSortFn();
    		}
    		this.callParent(arguments);
    	}
    });
    This allows you to do this:
    Code:
    Ext.define('People', {
    	extend: 'CustomSortStore',
    	config: {
    		sortFns: {
    			street: function(person1, person2) {
    				var value1 = o1.getAddress().get('street'));
    				var value2 = o2.getAddress().get('street'));
    				return value1 > value2 ? 1 : (value1 < value2 ? -1 : 0);
    			}
    		},
    		model: 'Person'
    	},
    	...
    });
    ...
    peopleStore.sort('street');

Thread Participants: 1

Tags for this Thread

Turkiyenin en sevilen filmlerinin yer aldigi xnxx internet sitemiz olan ve porn sex tarzi bir site olan mobil porno izle sitemiz gercekten dillere destan bir durumda herkesin sevdigi bir site olarak tarihe gececege benziyor. Sitenin en belirgin ozelliklerinden birisi de Turkiyede gercekten kaliteli ve muntazam, duzenli porno izle siteleri olmamasidir. Bu yuzden iste. Ayrica en net goruntu kalitesine sahip adresinde yayinlanmaktadir. Mesela diğer sitelerimizden bahsedecek olursak, en iyi hd porno video arşivine sahip bir siteyiz. "The Best anal porn videos and slut anus, big asses movies set..." hd porno faketaxi