I have an advanced datagrid and I am implementing a custom sort. My adv.data grid slows down on each consequent sorting...and if the nodes are opened, it simply times out.
My data looks like the following (g = grp on ID)*
ID f_name l_name fld1 fld2
(g)
--------------------------------------
1 A1 B1 xyx xyz
A6 B6 xyx xyz
--------------------------------------
2 A3 B3 xyx xyz
A8 B8 xyx xyz
--------------------------------------
When I sort on F_NAME or L_NAME the sorting ArrayCollection should sort and show data as follows(g = grp on ID)*:
ID f_name l_name fld1 fld2
(g)
-----------------------------------------
1(1) A1 B1 xyx xyz
2(2) A3 B3 xyx xyz
-----------------------------------------
1(3) A6 B6 xyx xyz
2(4) A6 B6 xyx xyz
-----------------------------------------
To do this I am employing a sort event which looks like the following
protected function adg1_headerReleaseHandler(event:AdvancedDataGridEvent):void
{
event.preventDefault();
sort_dir = !sort_dir;
var sort:Sort = new Sort();
sort.fields = [new SortField(event.dataField, true,sort_dir),new SortField('ID', true,sort_dir)]
_ds.sort = sort;
_ds.refresh();
adg1.dataProvider = null;
var previousRegId:int = 0;
var counter:int = 0;
for each (var item:DueInVO in _ds) {
if (previousRegId != item.ID) {
counter++;
previ开发者_如何学编程ousRegId = item.ID
}
item.uid = counter;
}
/*
var previousRegId:int = 0;
var counter:int = 0;
var csr:IViewCursor = DataSource.createCursor();
while(!csr.afterLast){
if (previousRegId != csr.current.registration_id) {
counter++;
previousRegId = csr.current.registration_id
}
csr.current.uid = counter;
csr.moveNext();
}
*/
setGrouping(['uid']);
//csr = null;
}
and the setGrouping function looks like the following:
protected function setGrouping(columnNames:Array):void{
var _groupingColumns:Array = [];
var gc:GroupingField;
if (columnNames.length > 0) {
var _groupedData:GroupingCollection2 = new GroupingCollection2();
var _grouping:Grouping = new Grouping();
for (var i:int=0;i<columnNames.length;i++) {
gc = new GroupingField(columnNames[i]);
//gc.numeric = true;
//gc.compareFunction = compareFunc;
//gc.groupingFunction = groupFunc;
_groupingColumns.push(gc);
}
_groupedData.source = _ds;
_grouping.fields = _groupingColumns;
_groupedData.grouping = _grouping
_groupedData.refresh();
adg1.dataProvider = _groupedData;
} else {
adg1.dataProvider = _ds;
}
}
However each consequent request to sort is taking longer and longer to resolve. As stated above, if the nodes of grouping are opned, then there is a high likelyhood that Flash player crashes.
I am guessing there is a mem-leak somewhere but I am not able to put a finger on it.
If anyone requires the full code, I will be happy to provide it..
All help is greatly appreciated
Instead of doing this, you should simply provide a sortCompareFunction to your ADG column. You don't have to capture the header click event and all that. It's already done for you. All you have to do is specify how your comparison for a particular column will play out.
I got it!... Robusto thanks for pointing me in somewhat right direction. The problem with your suggestion in my case is that I am generating columns from a config file, so I can make this grid-component reusable. So because of that: 1. I am using a labelFunction 2. column.sortCompareFunction = myfunc callback simply doesn't work as quoted from Adobe API documentation.
However, as you said, I was writing convoluted code. I needed to add gc.compareFunction = myCompare; to the following code (compare from above):
protected function setGrouping(columnNames:Array):void{ _groupingColumns = [];
if (columnNames.length > 0) {
_groupedData = new GroupingCollection2();
_grouping = new Grouping();
for (var i:int=0;i<columnNames.length;i++) {
var gc:GroupingField = new GroupingField(columnNames[i]);
gc.descending = _sort_direction;
gc.compareFunction = myCompare;
_groupingColumns.push(gc);
}
_groupedData.source = _ds;
_grouping.fields = _groupingColumns;
_groupedData.grouping = _grouping
_groupedData.refresh();
_data = _groupedData;
} else {
_data = _ds;
}
setGridColumns();
}
Once that was done I just had to listen to the sort event in the AdvancedDataGrid listener and call the setGrouping([abc]) from there, as follows:
protected function adg1_sortHandler(event:AdvancedDataGridEvent):void
{
_sortField = event.dataField;
if (temp.length == 0) {
temp.push(DefaultGroupColumn);
}
setGrouping(temp)
_sort_direction = !_sort_direction;
I hope this helps someone! AAP
精彩评论