我有一个 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.
发布评论
评论(2)
看起来您想要对日期进行排序。
Sort
无法立即执行此操作。您必须使用compareFunction
。如果您的对象是
Date
类型,则非常简单:如果您的列包含日期作为字符串,您必须首先解析它们(代码示例来自 http://blog.flexexamples.com/2007/08/12/sorting-date-columns-in-a-datagrid/):
然后只需使用
sortField 就像您在示例中所做的那样。那应该可以正常工作。
Looks like you want to sort dates.
Sort
can't do that out of the box. You have to use acompareFunction
.If your objects are of type
Date
it's quite easy: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/):
Then just use the
sortField
like you did in your example. That should work fine.您可以创建一个新的高级数据网格排序事件,并在设置分层数据后将其分派到网格上(不幸的是,我不得不使用 callLater 来给网格时间来在内部处理集合,似乎分配给ADG 的 dataProvider 有时是异步的)
此代码来自 ADG 的扩展,因此如果您不创建扩展,您可能希望dispatchEvent 实际上位于网格实例上。
代码中还有一个注释:
它分派事件两次以翻转排序。
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)
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:
It dispatches the event twice to flip the sort.