如何使用actionscript在Flex中分离基于字符串的arraycollection

时间:2014-01-21 09:20:14

标签: actionscript-3 flex flex4.5

我想在flex中基于字符串分离arraycollection。 我的数组收集项看起来像{[(name:aa),(name:bb),(name:ae),(name:cc),(name:bd)}], 现在我想基于'a'和所有'a'项来分离这个arraycollection,以便在单独的arraycollection中添加。

一样的Arraycollection声明
[Bindable]
public var arr:ArrayCollection=new ArrayCollection([{name:"cards/a.png"},{name:"cards/b.png"},{name:"cards/ac.png"},{name:"af.png"},{name:"ad.png"},{name:"cards/bb.png"}]);

1 个答案:

答案 0 :(得分:0)

您的arraycollection结构应为new ArrayCollection([{name:"aa"},{name:"bb"}]); “(”它应该是“{”。

    

    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
         import mx.collections.ArrayCollection;
         import mx.controls.Alert;
         import mx.utils.ObjectUtil;

        public var wordCollection:ArrayCollection = new ArrayCollection([{name:"aa"}, {name:"bb"}, 
        {name:"ae"}, {name:"cc"}, {name:"bd"}]);

        public var charACollection:ArrayCollection = new ArrayCollection();
        public var charBCollection:ArrayCollection = new ArrayCollection();
        public var charCCollection:ArrayCollection = new ArrayCollection();

        private function createCompleteHandler():void
        {
            wordCollection.filterFunction = splitByChars;
            wordCollection.refresh();

            trace(ObjectUtil.toString(charACollection));
            trace(ObjectUtil.toString(charBCollection));
            trace(ObjectUtil.toString(charCCollection));
        }

        private function splitByChars(item:Object):Boolean
        {
            var char:String = item.name.toString().charAt(0);

            if (char.toLowerCase() == "a")
            {
                charACollection.addItem(item);
            }
            else if (char.toLowerCase() == "b")
            {
                charBCollection.addItem(item);
            }
            else if (char.toLowerCase() == "c")
            {
                charCCollection.addItem(item);
            }

            return true;
        }


        ]]>
    </fx:Script>
</s:Application>