在 ArrayCollection 中移动元素 - FLEX

Posted

技术标签:

【中文标题】在 ArrayCollection 中移动元素 - FLEX【英文标题】:Move elements in an ArrayCollection - FLEX 【发布时间】:2010-12-16 23:09:23 【问题描述】:

有人可以引导我在 flex 的 ArrayCollection 中移动一个元素吗?

我有一个排序对象的 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());

'CategoryList' 是一个长度为 28 的 ArrayCollection,ArrayCollection 中的每个对象都有 3 个属性。

'RemoveItem' 工作正常,但'AddItem' 抛出此错误,

RangeError:指定的索引“28”超出范围。 在 mx.collections::ArrayList/addItemAt()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\collections\ArrayList.as:305] 在 mx.collections::ListCollectionView/addItemAt()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\collections\ListCollectionView.as:501] 在 mx.collections::ListCollectionView/addItem()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\collections\ListCollectionView.as:470] 在 components::Home/creationOver()[C:\Documents and Settings\immanuel\My Documents\Flex Builder 3\Porj\src\components\Home.mxml:113] 在 components::Home/___Home_Canvas1_creationComplete()[C:\Documents and Settings\immanuel\My Documents\Flex Builder 3\Porj\src\components\Home.mxml:2] 在 flash.events::EventDispatcher/dispatchEventFunction() 在 flash.events::EventDispatcher/dispatchEvent() 在 mx.core::UIComponent/dispatchEvent()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:9298] 在 mx.core::UIComponent/set initialized()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:1169] 在 mx.managers::LayoutManager/doPhasedInstantiation()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\managers\LayoutManager.as:718] 在函数/http://adobe.com/AS3/2006/builtin::apply() 在 mx.core::UIComponent/callLaterDispatcher2()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:8628] 在 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:错误 #1006:值不是函数。 在 mx.collections::ListCollectionView/getFilteredItemIndex()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\collections\ListCollectionView.as:564] 在 mx.collections::ListCollectionView/addItemsToView()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\collections\ListCollectionView.as:896] 在 mx.collections::ListCollectionView/listChangeHandler()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\collections\ListCollectionView.as:1051] 在 flash.events::EventDispatcher/dispatchEventFunction() 在 flash.events::EventDispatcher/dispatchEvent() 在 mx.collections::ArrayList/internalDispatchEvent()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\collections\ArrayList.as:510] 在 mx.collections::ArrayList/addItemAt()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\collections\ArrayList.as:311] 在 mx.collections::ListCollectionView/addItemAt()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\collections\ListCollectionView.as:501] 在 components::Home/creationOver()[C:\Documents and Settings\immanuel\My Documents\Flex Builder 3\Porj\src\components\Home.mxml:113] 在 components::Home/___Home_Canvas1_creationComplete()[C:\Documents and Settings\immanuel\My Documents\Flex Builder 3\Porj\src\components\Home.mxml:2] 在 flash.events::EventDispatcher/dispatchEventFunction() 在 flash.events::EventDispatcher/dispatchEvent() 在 mx.core::UIComponent/dispatchEvent()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:9298] 在 mx.core::UIComponent/set initialized()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:1169] 在 mx.managers::LayoutManager/doPhasedInstantiation()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\managers\LayoutManager.as:718] 在函数/http://adobe.com/AS3/2006/builtin::apply() 在 mx.core::UIComponent/callLaterDispatcher2()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:8628] 在 mx.core::UIComponent/callLaterDispatcher()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:8568]

【问题讨论】:

【参考方案1】:
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>

【讨论】:

感谢您的回复,并确认它有效 Amarghosh...我实际上使用的是 ArrayCollection,而 join、push 和 splice 方法在 ArrayCollection 上不起作用...我只是想让我的要求更简单:) 使用数组收集更容易 - 请参阅我的更新。不过,在执行此操作之前,您必须将 sort 属性重置为 null。 我试过这个.. 但是删除和添加一个项目会出现“超出范围”异常.. 在做这件事之前你检查过if(index &lt; arrayCol.length)吗? 是的..删除项目不是问题..添加时出现异常..我什至尝试在第 0 位添加..仍然给出相同的异常...数组长度在删除时会减少,并且不会在“addItem”上自动增加?

以上是关于在 ArrayCollection 中移动元素 - FLEX的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Flex 中对 ArrayCollection 进行排序

添加到 ArrayCollection 时,教义未链接到拥有对象

在 Symfony 中通过 ajax 为 ArrayCollection 数据传递 POST json 对象

Symfony 2,未定义的变量,在构造函数中初始化为 ArrayCollection 的受保护成员通过错误,它是未定义的

将arrayCollection从flex传递给java

Symfony ArrayCollection 与 PersistentCollection