Flex嵌套的ArrayCollections排序

时间:2010-11-26 10:38:00

标签: flex actionscript-3 actionscript

我有一个嵌套在另一个中的ArrayCollection。我需要按字段对子集合进行排序。 我尝试了以下代码。它适用于父集合,但对子集合没有任何作用。

    // The Data 

public class Data
{
  public var value:String;
  public var otherValue:String;
  public var childrenData:ArrayCollection; // An array collection containing Data objects.
} 

// The sorting function 

private function sortData():void
{
  var records:ArrayCollection() = bla bla (init the collection with some Data objects).

  records.sort = new Sort();
  records.sort.fields = [
    new SortField('value', true, false)
  ];

  for each (var data:Data in records){
    sortChildren(data);
  }
  records.refresh();   
}

private function sortChildren(data:Data):void
{
  if (data.childrenData != null) {
    var srt:Sort = new Sort();
    srt.fields = [
      new SortField('otherValue', true, false)
    ];
    data.childrenData.sort = srt;
    data.childrenData.refresh();
  }  
}

我无法弄清楚我做错了什么,所以一些帮助将不胜感激。

提前致谢,

1 个答案:

答案 0 :(得分:0)

我不确定为什么会这样。但是,看起来sort继承到其子集合......

是否可以按childData value排序otherValue中的行人?在这种情况下,您可以使用IHierarchicalCollectionView对所有元素进行排序。看一下下面的例子:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx" initialize="init()">

    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import mx.collections.HierarchicalCollectionView;
            import mx.collections.HierarchicalData;
            import mx.collections.IHierarchicalCollectionView;
            import mx.collections.Sort;
            import mx.collections.SortField;

            private var records:ArrayCollection = new ArrayCollection([
                new Data("S", null, new ArrayCollection([
                    new Data(null, "R", null),
                    new Data(null, "B", null),
                    new Data(null, "L", null)
                ])),
                new Data("A", null, new ArrayCollection([
                    new Data(null, "P", null),
                    new Data(null, "O", null)
                ])),
                new Data("X", null, new ArrayCollection([
                    new Data(null, "C", null),
                    new Data(null, "F", null),
                    new Data(null, "E", null)
                ]))
                ]);

            [Bindable]
            private var hierarchicalView:IHierarchicalCollectionView;

            private function init():void
            {
                var hierarchicalRecords:HierarchicalData = new HierarchicalData(records);

                // you don't need this if your field is named 'children' which is the default...
                hierarchicalRecords.childrenField = "childrenData";

                hierarchicalView = new HierarchicalCollectionView(hierarchicalRecords);
                sortData(hierarchicalView);
            }

            private function sortData(view:IHierarchicalCollectionView):void
            {
                var x:ArrayCollection;
                view.sort = new Sort();
                view.sort.fields = [new SortField('value', true, false), new SortField('otherValue', true, false)];
                view.refresh();
            }
        ]]>
    </fx:Script>

    <s:layout>
        <s:HorizontalLayout horizontalAlign="center" verticalAlign="middle"/>
    </s:layout>

    <mx:AdvancedDataGrid designViewDataType="tree" height="80%" dataProvider="{hierarchicalView}"
                         creationComplete="event.currentTarget.expandAll()">
        <mx:columns>
            <mx:AdvancedDataGridColumn dataField="value"/>
            <mx:AdvancedDataGridColumn dataField="otherValue"/>
        </mx:columns>
    </mx:AdvancedDataGrid>
</s:Application>

另一种选择是编写一个compareFunction来相应地比较你的对象。