用另一个数组过滤和排序ArrayCollection

时间:2014-01-15 21:55:19

标签: flex

我有一个大约10个项目的数组和一个大约200个项目的数组集合。数组集合将包含那里的前10个项目。

我想过滤数组集合并命令它只显示第一个数组中的项目,并按照第一个数组列出它们的顺序。

<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>

1 个答案:

答案 0 :(得分:1)

您需要为arraycollection应用过滤函数,然后将您的逻辑放入过滤函数。如果过滤器函数返回TRUE,则项目将在arraycollection中可用,否则项目将从arraycollection中删除但在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>