AdvancedDataGrid Column Sorting Question
I have am using an mx:advanceddatagrid and I running into a problem with grouped column sorting. A screenshot of what my grid looks like is here: http://i.stack.imgur.com/waAi7.png
I have the following kind of data:
PRODUCT_ID PROD_NAME CATEGORY TYPE SubType SubType_descr
1 p_1 c1_y t1
t1_s1 sub_1*
t1_s2 sub_2*
t1_s4 sub_4*
t1_s5 sub_5*
2 p_2 c1_x t1
t1_s1 sub_1
t1_s2 sub_2
t1_s3 sub_3
3 p_3 c1_x t1
t1_s1 sub_1
t1_s2 sub_2*
t1_s3 sub_3*
Now, the dataset is of the type arrayCollection. I manually move it into GroupingCollection2 when a setter fires the Binding Event;
protected function setGrouping(columnNames:Array):void{
_groupingColumns = [];
if (columnNames.length > 0) {
var _groupedData:GroupingCollection2 = new GroupingCollection2();
var _grouping:Grouping = new Grouping();
for (var i:int=0;i<columnNames.length;i++) {
var gc:GroupingField = new GroupingField(columnNames[i]);
_groupingColumns.push(gc);
}
_groupedData.source = _ds;
_grouping.fields = _groupingColumns;
_groupedData.grouping = _grouping
_groupedData.refresh();
_data = _groupedData;
} else {
_data = _ds;
}
setGridColumns();
}
Although I am grouping on Product_Id, i achieve the above look by a labelFunction as shown below:
col.labelFunction = function (item:Object,column:AdvancedDataGridColumn):String {
if (item.hasOwnProperty('GroupLabel') && item.GroupLabel != null) {
if (item.children.length > 0) {
return item.children[0][column.dataField];
}
else {
return "";
}
}
else {
return "";
}
}
The data I recieve from the server is of the kind shown below
PRODUCT_ID PROD_NAME CATEGORY TYPE SubType SubType_descr
-------------------------------------------------------------------
1 p_1 c1_y t1 t1_s1 sub_1*
2 p_2 c1_y t1 t1_s2 sub_2*
3 p_3 c1_y t1 t1_s3 sub_3*
-------------------------------------------------------------------
4 p_4 c1_x t1 t1_s1 sub_1*
5 p_5 c1_x t1 t1_s2 sub_2*
6 p_6 c1_x t1 t1_s3 sub_3*
In short I am taking all the common column values (Product, Category, Type) and putting them in the first row, all the changing values in the sub-rows (sub_type, subtype_descr)
I would like to sort this gird on Prod_Name, Category and type columns as well but sonething like the following is not working, even though the underlying data gets sorted.
public var _sort_direction:Boolean = false;
protected function adg1_headerReleaseHandler(event:AdvancedDataGridEvent):void
{
var sort:Sort = new Sort();
sort.fields = [new SortField(event.dataField, true, _sort_direction)];
_ds.sort = sort;
_ds.refresh();
if (DefaultGroupColumn != null && temp[0] != DefaultGroupColumn) {
temp.push(DefaultGroupColumn);
开发者_StackOverflow社区 }
setGrouping(temp);
adg1.validateNow();
_sort_direction = !_sort_direction;
}
all help is really appreciated.
The answer to this was so simple, but Adobe documentation and some other blogs sent me on a wild goose chase...
for (var i:int=0;i<columnNames.length;i++) {
var gc:GroupingField = new GroupingField(columnNames[i]);
gc.compareFunction = positionCompareFunction;
gc.descending = sort_dir;
_groupingColumns.push(gc);
}
A colleague (thanks Samitt) helped me figure this out. Just add gc.descending and the grouping will sort on the grouped column.
精彩评论