如何对具有分层数据的AdvancedDataGrid进行排序?

发布于 2024-10-28 12:19:36 字数 705 浏览 5 评论 0 原文

我有一个 AdvancedDataGrid,其 dataProvider 为 HierarchicalCollectionView。当我查看包含我正在使用的数据集的网格并单击我想要排序的列的标题时,一切都运行良好。它完全按照我的预期按层次结构对其进行排序。

我现在想做的是当网格显示给用户时已经对其进行排序。有没有办法以编程方式执行此操作?我一生都无法弄清楚如何做到这一点,显然这是可能的,因为 AdvancedDataGrid 内置了这个。

编辑 - 顺便说一句,我已经尝试过这个:

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();

这似乎做了一些事情就排序而言,但它的排序方式与单击列标题的方式不同。顺便说一下,“startDate”是 theDataSource 中对象的一个​​属性。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光是把杀猪刀 2024-11-04 12:19:36

看起来您想要对日期进行排序。 Sort 无法立即执行此操作。您必须使用 compareFunction

如果您的对象是 Date 类型,则非常简单:

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

如果您的列包含日期作为字符串,您必须首先解析它们(代码示例来自 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;

然后只需使用 sortField 就像您在示例中所做的那样。那应该可以正常工作。

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.

待"谢繁草 2024-11-04 12:19:36

您可以创建一个新的高级数据网格排序事件,并在设置分层数据后将其分派到网格上(不幸的是,我不得不使用 callLater 来给网格时间来在内部处理集合,似乎分配给ADG 的 dataProvider 有时是异步的)

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

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

        dispatchEvent(advancedDataGridEvent);

此代码来自 ADG 的扩展,因此如果您不创建扩展,您可能希望dispatchEvent 实际上位于网格实例上。

代码中还有一个注释:

            //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);
            }

它分派事件两次以翻转排序。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文