开发者

How to sort AdvancedDataGrid with hierarchical data?

开发者 https://www.devze.com 2023-02-20 09:22 出处:网络
I have an AdvancedDataGrid with a HierarchicalCollectionView as its dataProvider. When I view the grid with the dataset I\'m working with, and click the header 开发者_运维问答of the column I wish to s

I have an AdvancedDataGrid with a HierarchicalCollectionView as its dataProvider. When I view the grid with the dataset I'm working with, and click the header 开发者_运维问答of the column I wish to sort on, everything works perfectly. It sorts it hierarchically exactly how I would expect it to.

What I want to do now is have the grid already be sorted when it is shown to the user. Is there a way to do this programatically? I can't for the life of me figure out how to do this, and clearly it's possible since the AdvancedDataGrid has this built in.

Edit - BTW, I've tried this:

var myData:HierarchicalCollectionView = new HierarchicalCollectionView(theDataSource);

// Works fine using only the line above and clicking the header to sort.
// This is the part that I tried adding:
var sort:Sort = new Sort();

sort.fields = [new SortField("startDate")];

myData.sort = sort;
myData.refresh();

This appears to do something as far as sorting goes, but it doesn't sort it in the same way as clicking the column header. "startDate" is a property of an object in theDataSource by the way.


Looks like you want to sort dates. Sort can't do that out of the box. You have to use a compareFunction.

If your objects are of type Date it's quite easy:

var sortField:SortField = new SortField("startDate");
sortField.compareFunction = ObjectUtil.dateCompare;

In case your column contains dates as strings you'll have to parse them first (code example from http://blog.flexexamples.com/2007/08/12/sorting-date-columns-in-a-datagrid/):

private function date_sortCompareFunc(itemA:Object, itemB:Object):int
{
    /* Date.parse() returns an int, but
       ObjectUtil.dateCompare() expects two
       Date objects, so convert String to
       int to Date. */
    var dateA:Date = new Date(Date.parse(itemA));
    var dateB:Date = new Date(Date.parse(itemB));
    return ObjectUtil.dateCompare(dateA, dateB);
}

var sortField:SortField = new SortField("startDate");
sortField.compareFunction = date_sortCompareFunc;

Then just use the sortField like you did in your example. That should work fine.


You can create a new advanced data grid sort event and dispatch it on the grid after the hierarchical data is set on it (unfortunately I've had to use a callLater to give the grid time to deal with the collection internally it seems assignments to the dataProvider of the ADG are sometimes asynchronous)

        var advancedDataGridEvent : AdvancedDataGridEvent = new AdvancedDataGridEvent(AdvancedDataGridEvent.SORT, false, true);

        advancedDataGridEvent.columnIndex = columnIndex;
        advancedDataGridEvent.dataField = dataField;

        dispatchEvent(advancedDataGridEvent);

This code is from an extension of ADG so you would want the dispatchEvent to actually be on your instance of the grid if you're not creating an extension.

Also a note from the code:

            //setting sortDescending=true on a column does not work as expected.  so, until a solution
            //is found, this works just as well.  the event that is dispatch just tells the column
            //to reset.  so, one resorts ascending (the default), while a second resorts descending.
            //however, this second is only dispatched if defaultSortDesc is true on the grid.
            if (defaultSortDesc)
            {
                dispatchEvent(advancedDataGridEvent);
            }

It dispatches the event twice to flip the sort.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号