Flex 拖拽删除:检测所有数据何时从源移动到目标

发布于 2024-08-06 11:54:42 字数 2410 浏览 1 评论 0原文

我有两个 mx:TileList 控件,用于批量编辑对象。第一个包含所有可用数据的集合,第二个包含当前批次。两者都绑定到 ArrayCollections,并且使用 TileList 控件的本机拖放功能,当在一个 ArrayCollections 之间拖动对象时,数据会从一个 ArrayCollections 移动到另一个他们。

我需要更改 currentState 以显示 &当批次计数从 0 变为 nn 变为 0 项时,重置批次操作控件。根据文档,我会我认为我应该监听 dragComplete 事件,但我的测试表明,它不是在数据从源 ArrayCollection 中删除并添加到目标 ArrayCollection 后触发,而是在这两个事件之间(一致)触发行动。

两个列表都与此类似:

<mx:TileList 
    id="srcList" 
    dragEnabled="true" 
    dropEnabled="true" 
    dragMoveEnabled="true" 
    dataProvider="{images}"
    dragComplete="handleDragComplete(event)"
    allowMultipleSelection="true"
/>

这是 handleDragComplete 函数的源代码:

private function handleDragComplete(e:DragEvent):void{
    trace(e.dragInitiator.name + '.dragComplete: batch.length=' + batch.length.toString());
    trace(e.dragInitiator.name + '.dragComplete: images.length=' + images.length.toString());
    if (batch.length > 0){
        currentState = 'show';
    }else{
        currentState = '';
    }
}

最后,这是运行代码的一些示例输出。这些都是一个接一个地运行。

情况 1:

应用程序在第一个列表中加载 10 个对象,但批次为空。我将 1 个对象从源列表拖到批处理列表。

srcList.dragComplete: batch.length=1
srcList.dragComplete: images.length=10

(预期:1,9)

显然,该对象已添加到批处理 ArrayCollection 中,但未从源中删除。

情况 2

现在,我将第二个对象拖入批次中。

srcList.dragComplete: batch.length=2
srcList.dragComplete: images.length=9

(预期:2,8)

首先,我们可以看到 images.length 发生了变化,这表明我从源列表拖动到批处理列表的对象在 DragComplete 事件触发后被删除。

这次发生了同样的事情:新对象被添加到批处理 ArrayCollection (batch.length=2),dragComplete 事件触发(运行这些跟踪),然后该对象从源中删除数组集合。

情况 3:

现在,我将把批处理列表中的两个图像拖回到源列表中的原始位置。

batchList.dragComplete: batch.length=2
batchList.dragComplete: images.length=10

(预期:0,10)

我们可以看到,batch.length 没有减少,但源图像数组又回到了原来的长度 10。

问题:我做错了什么吗?还有其他我可以收听的事件吗? (注意:我尝试了 DragExit DragDrop,只是为了确定,它们的行为如下预期的,但不是我需要的。)或者还有其他方法来获取我想要的数据吗?或者...我在 SDK 中发现了错误吗?

I have two mx:TileList controls that I'm using to allow editing of objects in batch. The first contains a collection of all available data, and the 2nd contains the current batch. Both are bound to ArrayCollections, and using the native drag-n-drop functionality of the TileList control the data is moved from one ArrayCollection to the other when an object is dragged between them.

I need to change the currentState to show & reset the batch manipulation controls when the batch count goes from 0 to n or n to 0 items. Based on the documentation, I would have thought that I should listen to the dragComplete event, but my testing shows that instead of firing after the data has been removed from the source ArrayCollection and added to the destination ArrayCollection, it fires (consistently) between these two actions.

Both lists are similar to this:

<mx:TileList 
    id="srcList" 
    dragEnabled="true" 
    dropEnabled="true" 
    dragMoveEnabled="true" 
    dataProvider="{images}"
    dragComplete="handleDragComplete(event)"
    allowMultipleSelection="true"
/>

And here's the source of the handleDragComplete function:

private function handleDragComplete(e:DragEvent):void{
    trace(e.dragInitiator.name + '.dragComplete: batch.length=' + batch.length.toString());
    trace(e.dragInitiator.name + '.dragComplete: images.length=' + images.length.toString());
    if (batch.length > 0){
        currentState = 'show';
    }else{
        currentState = '';
    }
}

And lastly, here's some example output from running the code. These are all run one after the other.

Case 1:

The application loads with 10 objects in the first list and the batch is empty. I dragged 1 object from the source list to the batch list.

srcList.dragComplete: batch.length=1
srcList.dragComplete: images.length=10

(Expected: 1,9)

Clearly, the object has been added to the batch ArrayCollection but not removed from the source.

Case 2:

Now, I'll drag a 2nd object into the batch.

srcList.dragComplete: batch.length=2
srcList.dragComplete: images.length=9

(Expected: 2,8)

Firstly, we can see that images.length has changed, showing that the object that I dragged from the source list to the batch list was removed AFTER the dragComplete event fired.

The same thing happens this time: The new object is added to the batch ArrayCollection (batch.length=2), the dragComplete event fires (running these traces), and then the object is removed from the source ArrayCollection.

Case 3:

Now, I'll drag both images from the batch list back to their original location in the source list.

batchList.dragComplete: batch.length=2
batchList.dragComplete: images.length=10

(Expected: 0,10)

We can see that batch.length hasn't gone down, but the source images array is back at its original length of 10.

QUESTION: Am I doing something wrong? Is there another event I could listen for? (Note: I tried both DragExit and DragDrop, just to be sure, and those behave as expected, but are not what I need.) Or is there another way to get the data that I want? Or... have I found a bug in the SDK?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

·深蓝 2024-08-13 11:54:42

您可以监听目标 ArrayCollections 上的 collectionChange 事件吗?

batch.addEventListener(CollectionEvent.COLLECTION_CHANGE, handleBatchCollectionChange);

Can you listen for a collectionChange event on your target ArrayCollections?

batch.addEventListener(CollectionEvent.COLLECTION_CHANGE, handleBatchCollectionChange);
━╋う一瞬間旳綻放 2024-08-13 11:54:42

这是另一个对我很有效的解决方案。

private function dragBeginHandler():void {
    stage.addEventListener(MouseEvent.MOUSE_UP, dragFinishHandler);
    trace("Drag/drop started, Drag-completion listener added");
}

private function dragFinishHandler():void {
    stage.removeEventListener(MouseEvent.MOUSE_UP, dragFinishHandler);
    trace("Drag/drop finished, Drag-completion listener removed");

    //do or print whatever you want to do here, add/remove will be done by now
    trace('dragComplete: batch.length=' + batch.length.toString());
    trace('dragComplete: images.length=' + images.length.toString());

    if (batch.length > 0){
            currentState = 'show';
    }else{
            currentState = '';
    }
}

<mx:TileList 
        id="srcList" 
        dragStart="dragBeginHandler()"
        dragEnabled="true" 
        dropEnabled="true" 
        dragMoveEnabled="true" 
        dataProvider="{images}"
        dragComplete="handleDragComplete(event)"
        allowMultipleSelection="true"
/>

Here is another solution that worked for me well.

private function dragBeginHandler():void {
    stage.addEventListener(MouseEvent.MOUSE_UP, dragFinishHandler);
    trace("Drag/drop started, Drag-completion listener added");
}

private function dragFinishHandler():void {
    stage.removeEventListener(MouseEvent.MOUSE_UP, dragFinishHandler);
    trace("Drag/drop finished, Drag-completion listener removed");

    //do or print whatever you want to do here, add/remove will be done by now
    trace('dragComplete: batch.length=' + batch.length.toString());
    trace('dragComplete: images.length=' + images.length.toString());

    if (batch.length > 0){
            currentState = 'show';
    }else{
            currentState = '';
    }
}

<mx:TileList 
        id="srcList" 
        dragStart="dragBeginHandler()"
        dragEnabled="true" 
        dropEnabled="true" 
        dragMoveEnabled="true" 
        dataProvider="{images}"
        dragComplete="handleDragComplete(event)"
        allowMultipleSelection="true"
/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文