2009-11-16 63 views
0

有人可以讓我在Flex中的ArrayCollection中移動元素嗎?移動ArrayCollection中的元素 - FLEX

我有一個排序對象的ArrayCollection。

現在我需要將一行移動到ArrayCollection的末尾。

爲了說明,

arrayCollection = ["Cars","Other","Trucks"]; 

這ArrayCollection的排序。現在我需要將'Other'移動到ArrayCollection的末尾。即,我需要的陣列被重組爲

arrayCollection = ["Cars","Trucks","Other"]; 

這是我的代碼,

if(Index != -1){ 
CategoryList.addItem(CategoryList.removeItemAt(Index)); 
trace(CategoryList.source.join());} 

「所屬分類」是長度28的ArrayCollection,具有用於在ArrayCollection的每個對象3點的屬性。

'的removeItem' 做工精細,but'AddItem」拋出這個錯誤,

RangeError: Index '28' specified is out of bounds. at mx.collections::ArrayList/addItemAt()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\collections\ArrayList.as:305] at mx.collections::ListCollectionView/addItemAt()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\collections\ListCollectionView.as:501] at mx.collections::ListCollectionView/addItem()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\collections\ListCollectionView.as:470] at components::Home/creationOver()[C:\Documents and Settings\immanuel\My Documents\Flex Builder 3\Porj\src\components\Home.mxml:113] at components::Home/___Home_Canvas1_creationComplete()[C:\Documents and Settings\immanuel\My Documents\Flex Builder 3\Porj\src\components\Home.mxml:2] at flash.events::EventDispatcher/dispatchEventFunction() at flash.events::EventDispatcher/dispatchEvent() at mx.core::UIComponent/dispatchEvent()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:9298] at mx.core::UIComponent/set initialized()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:1169] at mx.managers::LayoutManager/doPhasedInstantiation()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\managers\LayoutManager.as:718] at Function/ http://adobe.com/AS3/2006/builtin::apply() at mx.core::UIComponent/callLaterDispatcher2()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:8628] at mx.core::UIComponent/callLaterDispatcher()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:8568]

然後我嘗試在特定的位置插入,

CategoryList.addItemAt(CategoryList.removeItemAt(Index), CategoryList.length-1); 

但對此,拋出下面的錯誤,

TypeError: Error #1006: value is not a function. at mx.collections::ListCollectionView/getFilteredItemIndex()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\collections\ListCollectionView.as:564] at mx.collections::ListCollectionView/addItemsToView()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\collections\ListCollectionView.as:896] at mx.collections::ListCollectionView/listChangeHandler()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\collections\ListCollectionView.as:1051] at flash.events::EventDispatcher/dispatchEventFunction() at flash.events::EventDispatcher/dispatchEvent() at mx.collections::ArrayList/internalDispatchEvent()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\collections\ArrayList.as:510] at mx.collections::ArrayList/addItemAt()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\collections\ArrayList.as:311] at mx.collections::ListCollectionView/addItemAt()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\collections\ListCollectionView.as:501] at components::Home/creationOver()[C:\Documents and Settings\immanuel\My Documents\Flex Builder 3\Porj\src\components\Home.mxml:113] at components::Home/___Home_Canvas1_creationComplete()[C:\Documents and Settings\immanuel\My Documents\Flex Builder 3\Porj\src\components\Home.mxml:2] at flash.events::EventDispatcher/dispatchEventFunction() at flash.events::EventDispatcher/dispatchEvent() at mx.core::UIComponent/dispatchEvent()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:9298] at mx.core::UIComponent/set initialized()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:1169] at mx.managers::LayoutManager/doPhasedInstantiation()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\managers\LayoutManager.as:718] at Function/ http://adobe.com/AS3/2006/builtin::apply() at mx.core::UIComponent/callLaterDispatcher2()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:8628] at mx.core::UIComponent/callLaterDispatcher()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:8568]

回答

3
var array:Array = ["Cars", "Other", "Trucks"]; 
pushToEnd(array, 1); 

trace(array.join()); //Cars,Trucks,Other 

/** 
* Removes the item at 'index' and pushes it to the back of the array. 
*/ 

function pushToEnd(array:Array, index:Number):void 
{ 
    array.push(array.splice(index, 1)[0]); 
} 

這是與ArrayCollection

arrayCol.addItem(arrayCol.removeItemAt(index)); 

更新更加容易:工作樣本 - 看到它自己。

<?xml version="1.0" encoding="utf-8"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" 
    creationComplete="create();"> 
    <mx:Button label="push" click="handle();"/> 
    <mx:Script> 
     <![CDATA[ 
      import mx.collections.ArrayCollection; 

      private var ac:ArrayCollection; 

      private function handle():void 
      { 
       ac.addItem(ac.removeItemAt(1)); 
       trace(ac.source.join()); 
      } 

      private function create():void 
      { 
       ac = new ArrayCollection(["asd", "qwe", "zxc", "123"]); 
       trace(ac.source.join()); 
      } 
     ]]> 
    </mx:Script> 
</mx:Application> 
+0

刪除和添加在最後的作品。 – Amarghosh 2009-11-16 08:51:33

+0

感謝您的答覆,並確認它的作品Amarghosh ... 我實際上使用ArrayCollection和方法加入,推,拼接不工作ArrayCollection ... 我只是爲了使我的要求更簡單:) – Immanuel 2009-11-16 09:25:07

+0

數組收集更容易 - 請參閱我的更新。在完成此操作之前,您將不得不將sort屬性重置爲null。 – Amarghosh 2009-11-16 09:52:38