使用 dataProvider 作为 GroupingCollection 在 AdvancedDataGrid 中保留多列排序

发布于 2024-08-14 12:54:44 字数 2018 浏览 6 评论 0原文

我的 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 技术交流群。

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

发布评论

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

评论(3

抱着落日 2024-08-21 12:54:44

在刷新之前(或当排序选项完全更改时)存储当前排序选项时,它们实际上位于数据提供程序上。
var objDp:HierarchicalCollectionView = ucADG.dataProvider as HierarchicalCollectionView;

在本例中,当前的 SortField 对象位于 objDp.sort.fields 上。

刷新集合后只需执行相反的操作即可。例如:

var objDP:HierarchicalCollectionView = ucADG.dataProvider as HierarchicalCollectionView;
            if(objDP != null)
            {
                var objSort:Sort = new Sort();
                objSort.fields = [ new SortField("SomeField", true, bUseDescending) ];
                objDP.sort = objSort;
                objDP.refresh();
            }

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:

var objDP:HierarchicalCollectionView = ucADG.dataProvider as HierarchicalCollectionView;
            if(objDP != null)
            {
                var objSort:Sort = new Sort();
                objSort.fields = [ new SortField("SomeField", true, bUseDescending) ];
                objDP.sort = objSort;
                objDP.refresh();
            }
晚风撩人 2024-08-21 12:54:44

我遇到了同样的问题,根据 JtheGeek 和 rocksoccer 的答案,我能够保留排序顺序。

我的设置有点不同。我有一个直接绑定到 AdvancedDataGrid.dataProvidervar _xmlListData:XMLListCollection。我的刷新功能只是这样做:

var sort:Sort = _xmlListData.sort;
_xmlListData.source = data as XMLList;
_xmlListData.sort = sort;

并且它有效。所以,我想知道在调用 gc.refresh() 之前对 XMLListCollection 执行相同的操作是否会产生相同的结果? IE:

private function refresh(data:Object):void
  {
    var sort:Sort = dataXML.sort;
    dataXML.source = XML(data.result.value).info;
    dataXML.sort = sort;
    gc.refresh();
    adGrid.dataProvider = gc;
    adGrid.validateNow();
    adGrid.dataProvider.refresh();
  }

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 to AdvancedDataGrid.dataProvider. My refresh function simply does this:

var sort:Sort = _xmlListData.sort;
_xmlListData.source = data as XMLList;
_xmlListData.sort = sort;

And it works. So, I wonder if doing the same thing to your XMLListCollection before calling gc.refresh() would have the same results? I.e.:

private function refresh(data:Object):void
  {
    var sort:Sort = dataXML.sort;
    dataXML.source = XML(data.result.value).info;
    dataXML.sort = sort;
    gc.refresh();
    adGrid.dataProvider = gc;
    adGrid.validateNow();
    adGrid.dataProvider.refresh();
  }
快乐很简单 2024-08-21 12:54:44

我认为 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.

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