PDA

View Full Version : Grid grouping and sorting



extjs1111_user
6 Sep 2010, 1:47 PM
Hi,

I am using Ext 3.x . Is there any way to sort the grouped column as well as content of group column like in example http://dev.sencha.com/deploy/dev/examples/grid/grouping.html .I want to sort on column Industry as well,which is the groupField .I am not using remoteSort as well as remoteGroup.If I do need to override the sort function of groupingStore plz do tell me how to do it .... Any help will be deeply appreciated..

Condor
6 Sep 2010, 10:37 PM
I don't understand the question. A GroupingStore always sorts on the groupField first and then on the sortInfo.field.

extjs1111_user
7 Sep 2010, 6:11 AM
thanks condor for your reply,
My question is in the example http://dev.sencha.com/deploy/dev/examples/grid/grouping.html
i want the industry column sorted as well like when i click on industry SERVICES recordset as a whole should come first,currently it is doing nothing i.e. its not getting sorted.

Thanks,

plalx
21 Sep 2010, 4:36 AM
Hi,

I have noticed the same problem when using local sorting and grouping... sorting on the group by column doesn't sort the groups like it should do. The order stays static. I understand that when sorting on other columns, it does sort first on grouping field and then on the other sort field, meaning that data is getting sorted within its group only, but when changing the sort order of the group by column, it should sort the groups.

*Note: When using remote sorting and grouping, everything works fine since the groupDir param is being passed.

Should we consider this a bug?

plalx
21 Sep 2010, 8:19 AM
I have created this fix to allow sorting the groups by sorting the group column.

*Note: Grouping direction is always set back to default in case of a new grouping operation.

Here's the code:



Ext.override(Ext.data.GroupingStore, {
sort : function(fieldName, dir) {

if (this.remoteSort) {
return Ext.data.GroupingStore.superclass.sort.call(this, fieldName, dir);
}

var sorters = [];

//cater for any existing valid arguments to this.sort, massage them into an array of sorter objects
if (Ext.isArray(arguments[0])) {

sorters = arguments[0];

} else if (fieldName == undefined) {
//we preserve the existing sortInfo here because this.sort is called after
//clearGrouping and there may be existing sorting
sorters = this.sortInfo ? [this.sortInfo] : [];
} else {
//TODO: this is lifted straight from Ext.data.Store's singleSort function. It should instead be
//refactored into a common method if possible
var field = this.fields.get(fieldName);
if (!field) return false;

var name = field.name,
sortInfo = this.sortInfo || null,
sortToggle = this.sortToggle ? this.sortToggle[name] : null;

if (!dir) {
if (sortInfo && sortInfo.field == name) { // toggle sort dir
dir = (this.sortToggle[name] || 'ASC').toggle('ASC', 'DESC');
} else {
dir = field.sortDir;
}
}

this.sortToggle[name] = dir;
this.sortInfo = {field: name, direction: dir};

sorters = [this.sortInfo];
}

//add the grouping sorter object as the first multisort sorter
if (this.groupField) {

var groupDir = this.groupDir;

//Allow sorting groups
if (this.groupField === this.sortInfo.field)
groupDir = this.sortInfo.direction;

//We need to set the grouping direction according to the last sort that occured on
//the grouping field so that we don't break grouping while sorting on other fields.
//!Note: We avoid doing this during the first sort of a grouping action, otherwise
//it's overwriting the default grouping order.
else if (!Ext.isArray(arguments[0]))
groupDir = this.multiSortInfo.sorters[0].direction;

sorters.unshift({direction: groupDir, field: this.groupField});
}

return this.multiSort.call(this, sorters, dir);
}
});

jratcliff
5 Oct 2010, 8:30 AM
Hi,

I have noticed the same problem when using local sorting and grouping... sorting on the group by column doesn't sort the groups like it should do. The order stays static. I understand that when sorting on other columns, it does sort first on grouping field and then on the other sort field, meaning that data is getting sorted within its group only, but when changing the sort order of the group by column, it should sort the groups.

*Note: When using remote sorting and grouping, everything works fine since the groupDir param is being passed.

Should we consider this a bug?

Yes, I would say it's a bug. I just upgraded an app from Ext 2.2 to Ext 3.2.1 and I'm seeing this issue. In Ext 2.2 the group is sorting on the direction you want (ASC or DESC) but in Ext 3.2.1 the group is always sorted ASC. Also, in Ext 2.2 if you click on the column header of a column that is also grouped, the the group will sort. In Ext 3.2.1 nothing happens. I see you have an override for this so I'll try that out.

jratcliff
5 Oct 2010, 9:39 AM
@scarsick - your override worked! Thanks again. Also thanks for mentioning the groupDir param. My initial sort of my groupField would not sort in descendig order (again, worked fine in Ext 2.2) but it appears with Ext 3.x you must also set the groupDir param as well. That param isn't documented but I did find it in the source.

Thanks again!
Jack