2010-11-26 64 views
0

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

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

我不明白我在做什么错,所以一些帮助,将不胜感激。

在此先感谢,

回答

0

我不完全确定它为什么会发生。然而,它看起来像sort继承至其子集...

是否有可能通过otherValuevaluechildData的elmenents排序?在这种情况下,您可以使用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,你的对象相应的比较。