PDA

View Full Version : Sort column by dynamic column value



Misiu
18 Jan 2014, 12:55 PM
In my application I have 2 stores: first for employees and second for events.
Employee models has those fields: id, name, department.
Event model has those fields: id, uid, start, end, status.

I've created grid that displays start and end fields from event and name from employee.
2p8

Bottom grid uses event store as source and has column named "Employee", I use renderer to get name of employee from employee store:



renderer: function(value, metaData, record) {
var s = Ext.data.StoreManager.lookup('employeeStore');
var r = s.findRecord('id', record.raw.uid);
return r.raw.name;
}


First column is sortable but by 'uid' field not by actual name.
How can I sort bottom grid by employee name (alphabetically)?
Is this possible without additional event-employe store?

Misiu
24 Jan 2014, 8:05 AM
I managed to do this using doSort method:



doSort: function(state) {
/*CURRENT GRID STORE*/
var store = Ext.data.StoreManager.lookup('localEventStore');
/*CURENT COLUMN FIELD*/
var field = this.getSortParam();
/*STORE WITH EMPLOYEES*/
var s2 = Ext.data.StoreManager.lookup('employeeStore');
/*LOOKUP FIELD*/
var s2_field = 'id';
store.sort({
property: field,
direction: state,
sorterFn: function(v1, v2) {
v1 = s2.findRecord(s2_field, v1.raw[field]).raw.name;
v2 = s2.findRecord(s2_field, v2.raw[field]).raw.name;
return v1 > v2 ? 1 : (v1 < v2 ? -1 : 0);
}
});
}


But I have problem with initial sort. If I set state manager all other columns sort is saved, but column with custom sort isn't. After loading my application I see arrow indicating order of sort, but column isn't sorted!
How can I fix this?