2014-01-15 57 views
0

我有一個約10個項目的數組和約200個項目的數組集合。數組集合將包含該處的前10個項目。篩選和排序ArrayCollection由另一個陣列

我想過濾的陣列收集並責令只顯示第一陣列中的第一陣列列出它們的順序選擇項目

<s:ArrayCollection id="baseballCardCollection" > 
    <fx:Object name="Bill"/> 
    <fx:Object name="Jill"/> 
    <fx:Object name="Phil"/> 
    <fx:Object name="Luke"/> 
    <fx:Object name="Duke"/> 
    <fx:Object name="Zach"/> 
    <fx:Object name="John"/> 
    <fx:Object name="Don"/> 
    <fx:Object name="Ron"/> 
    <fx:Object name="Anne"/> 
    <fx:Object name="Mark"/> 
    <fx:Object name="Clark"/> 
</s:ArrayCollection> 

篩選和排序,以便通過:

<fx:Array> 
    <fx:String>Zach</fx:String> 
    <fx:String>Anne</fx:String> 
    <fx:String>John</fx:String> 
    <fx:String>Mark</fx:String> 
    <fx:String>Luke</fx:String> 
</fx:Array> 

目標:

<s:ArrayCollection id="baseballCardCollection" > 
    <fx:Object name="Zach"/> 
    <fx:Object name="Anne"/> 
    <fx:Object name="John"/> 
    <fx:Object name="Mark"/>  
    <fx:Object name="Luke"/>   
</s:ArrayCollection> 
+0

第二陣列的項目總是會出現在第一個?如果是這樣,因爲你沒有建立第三個數組?因爲當你這樣做的時候,你只需要做一點搜索算法,這將是你直接加載新陣列所需的相同特性的結尾。 – matilu

回答

1

您需要申請過濾器功能的ArrayCollection的ñ把你的邏輯進入過濾功能。項目將在ArrayCollection中使用,如果過濾器函數返回TRUE,否則項目將從ArrayCollection中刪除,但仍處於avaliable ArrayCollection.source財產即baseballCardCollection.source

<fx:Script> 
    <![CDATA[ 
     import mx.collections.Sort; 
     import mx.collections.SortField; 
     import mx.events.FlexEvent; 

     protected function windowedapplication1_creationCompleteHandler(event:FlexEvent):void 
     {  
      fitlerByPersonArray(); 
      sortItemsByName(); 
     } 

     private function fitlerByPersonArray():void {    
      baseballCardCollection.filterFunction = filterByPersonAndAddOrder; 
      baseballCardCollection.refresh() 
     } 

     private function sortItemsByName():void{ 
      var srt:Sort = new Sort(); 
      var orderField:SortField = new SortField("order"); 
      orderField.numeric = true; 

      srt.fields = [orderField]; //Order by 

      baseballCardCollection.sort = srt; 
      baseballCardCollection.refresh(); 
     }   

     private function filterByPersonAndAddOrder(item:Object):Boolean 
     { 
      var index:int = personArray.indexOf(item.name); 

      if(index > -1){ 
       item.order = index; //Create new property called Order then sort with the help of order property 
       return true; 
      } 

      return false;     
     } 

    ]]> 
</fx:Script> 
<fx:Declarations> 
    <!-- Place non-visual elements (e.g., services, value objects) here --> 
    <s:ArrayCollection id="baseballCardCollection" > 
     <fx:Object name="Bill"/> 
     <fx:Object name="Jill"/> 
     <fx:Object name="Phil"/> 
     <fx:Object name="Luke"/> 
     <fx:Object name="Duke"/> 
     <fx:Object name="Zach"/> 
     <fx:Object name="John"/> 
     <fx:Object name="Don"/> 
     <fx:Object name="Ron"/> 
     <fx:Object name="Anne"/> 
     <fx:Object name="Mark"/> 
     <fx:Object name="Clark"/> 
    </s:ArrayCollection> 

    <fx:Array id="personArray"> 
     <fx:String>Zach</fx:String> 
     <fx:String>Anne</fx:String> 
     <fx:String>John</fx:String> 
     <fx:String>Mark</fx:String> 
     <fx:String>Luke</fx:String>  
    </fx:Array> 

</fx:Declarations> 

<s:List dataProvider="{baseballCardCollection}" labelField="name" width="200" height="400">  
</s:List> 
+0

數組的種類很重要。這不是按字母順序排列,而是基於我們用來過濾的數組。 –

+0

請參閱最新的答案。 –