使用 dataProvider 作为 GroupingCollection 在 AdvancedDataGrid 中保留多列排序
我的 XML 对象中有三个属性:姓氏、名字和年龄。 我的示例 XML 如下所示:
<dataXML>
<info last="Abc" first="Def" age="20"/>
<info last="Abc" first="Hij" age="10"/>
<info last="Xyz" first="Klm" age="25"/>
<info last="Xyz" first="Opq" age="64"/>
<info last="Xyz" first="Rst" age="08"/>
</dataXML>
我正在使用 Grouping Collection 和 AdvancedDataGrid 来显示数据。 我的问题是保留多列排序。 刷新发生后,用户选择的排序顺序消失,网格仅按第一列排序。 因此,假设用户对表格进行了排序,首先按“年龄”升序,然后按“姓名”降序;刷新发生后,网格再次按“名称”升序排序。 我不希望刷新事件更改排序顺序,只应该刷新数据。
提前致谢。
PS 我不能使用任何其他数据类型(如 ArrayCollection)来存储数据。
我的部分代码如下所示:
<mx:Script>
<![CDATA[
[Bindable]
private var dataXML:XMLListCollection = new XMLListCollection();
private function refresh(data:Object):void
{
dataXML.source = XML(data.result.value).info;
gc.refresh();
adGrid.dataProvider = gc;
adGrid.validateNow();
adGrid.dataProvider.refresh();
}
private function nameCompareFunction(a:XML, b:XML):int
{
return ObjectUtil.stringCompare(a.attribute("last") + a.attribute("first"), b.attribute("last") + b.attribute("first"));
}
private function valueSortCompareFunction(a:XML, b:XML):int
{
return ObjectUtil.numericCompare(Number(a.attribute("age")), Number(b.attribute("age")));
}
]]>
</mx:Script>
<Control:AdvancedDataGrid id="adGrid">
<Control:dataProvider>
<mx:GroupingCollection id="gc" source="{dataXML}">
<mx:grouping>
<mx:Grouping>
<mx:GroupingField name="@last" compareFunction="nameCompareFunction"/>
</mx:Grouping>
</mx:grouping>
</mx:GroupingCollection>
</Control:dataProvider>
<Control:columns>
<mx:AdvancedDataGridColumn id="ADGCName" dataField="@first" headerText="Name" wordWrap="true"/>
<mx:AdvancedDataGridColumn id="ADGCAge" dataField="@age" headerText="Age" sortCompareFunction="valueSortCompareFunction"/>
</Control:columns>
</Control:AdvancedDataGrid>
I have three attributes in my XML object: last name, first name, and age.
My sample XML looks like:
<dataXML>
<info last="Abc" first="Def" age="20"/>
<info last="Abc" first="Hij" age="10"/>
<info last="Xyz" first="Klm" age="25"/>
<info last="Xyz" first="Opq" age="64"/>
<info last="Xyz" first="Rst" age="08"/>
</dataXML>
I am using Grouping Collection and AdvancedDataGrid to show the data.
My problem is to preserve multi-column sort.
After a refresh happens, the user selected sorting order goes away, and the grid gets sorted by first column only.
So suppose, user has sorted the table, first ascending by "Age" and then descending by "Name"; after a refresh happens, the grid again gets sorted ascending by "Name".
I don't want the refresh event to change the sort order, only the data should get refreshed.
Thanks in advance.
P.S. I can't use any other datatype like ArrayCollection, to store the data.
Part of my code looks as follows:
<mx:Script>
<![CDATA[
[Bindable]
private var dataXML:XMLListCollection = new XMLListCollection();
private function refresh(data:Object):void
{
dataXML.source = XML(data.result.value).info;
gc.refresh();
adGrid.dataProvider = gc;
adGrid.validateNow();
adGrid.dataProvider.refresh();
}
private function nameCompareFunction(a:XML, b:XML):int
{
return ObjectUtil.stringCompare(a.attribute("last") + a.attribute("first"), b.attribute("last") + b.attribute("first"));
}
private function valueSortCompareFunction(a:XML, b:XML):int
{
return ObjectUtil.numericCompare(Number(a.attribute("age")), Number(b.attribute("age")));
}
]]>
</mx:Script>
<Control:AdvancedDataGrid id="adGrid">
<Control:dataProvider>
<mx:GroupingCollection id="gc" source="{dataXML}">
<mx:grouping>
<mx:Grouping>
<mx:GroupingField name="@last" compareFunction="nameCompareFunction"/>
</mx:Grouping>
</mx:grouping>
</mx:GroupingCollection>
</Control:dataProvider>
<Control:columns>
<mx:AdvancedDataGridColumn id="ADGCName" dataField="@first" headerText="Name" wordWrap="true"/>
<mx:AdvancedDataGridColumn id="ADGCAge" dataField="@age" headerText="Age" sortCompareFunction="valueSortCompareFunction"/>
</Control:columns>
</Control:AdvancedDataGrid>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在刷新之前(或当排序选项完全更改时)存储当前排序选项时,它们实际上位于数据提供程序上。
var objDp:HierarchicalCollectionView = ucADG.dataProvider as HierarchicalCollectionView;
在本例中,当前的 SortField 对象位于 objDp.sort.fields 上。
刷新集合后只需执行相反的操作即可。例如:
before the refresh (or when the sorting options are changed at all) store the current sort options, they are actually on the dataprovider.
var objDp:HierarchicalCollectionView = ucADG.dataProvider as HierarchicalCollectionView;
the current SortField objects are on objDp.sort.fields in this case.
after the collection is refreshed just do the opposite. For example:
我遇到了同样的问题,根据 JtheGeek 和 rocksoccer 的答案,我能够保留排序顺序。
我的设置有点不同。我有一个直接绑定到
AdvancedDataGrid.dataProvider
的var _xmlListData:XMLListCollection
。我的刷新功能只是这样做:并且它有效。所以,我想知道在调用
gc.refresh()
之前对XMLListCollection
执行相同的操作是否会产生相同的结果? IE:I had this same problem, and based on JtheGeek's and rocksoccer's answers, I was able to preserve the sort order.
My set-up is a bit different. I have a
var _xmlListData:XMLListCollection
that is bound directly toAdvancedDataGrid.dataProvider
. My refresh function simply does this:And it works. So, I wonder if doing the same thing to your
XMLListCollection
before callinggc.refresh()
would have the same results? I.e.:我认为 JTtheGeek 的答案不太正确。当您对一列进行排序时,该代码才起作用。因为除了降序之外,排序时还需要记住不同列之间的优先级。
其实很简单,创建一个临时排序对象来存储dataProvider中的排序,然后在应用新的dataProver后恢复它,然后刷新它。
I think JTtheGeek's anwser is not quite correct. The code would just work when you sort on one column. Because except the descending order, you also need to remember the priority between different columns in sorting.
In fact, it is so easy, create a temp sort object to store the sort in the dataProvider, and then restore it after you apply the new dataProver, then refresh it.