简体   繁体   English

JQuery UI:取消Droppable Drop可排序

[英]JQuery UI: Cancel Sortable upon Droppable Drop

I am using JQuery 1.5.1 and JQuery UI 1.8.11. 我正在使用JQuery 1.5.1和JQuery UI 1.8.11。

I have added sortable for a number of items - the task here is to allow drag to sort, this all works fine. 我已经添加了许多项目的可排序 - 这里的任务是允许拖动排序,这一切都很好。

But I also want to incorporate droppable, so that the item can be dropped onto a "copy me" area - the task there will be to duplicate the item (I will work that bit out later) 但我也希望合并droppable,以便将项目放到“复制我”区域 - 任务将复制项目(我稍后会工作)

Problem is the droppable target is at the bottom of the sortable list (I do not want to move this) and once the drop occurs the sortable item moves to the bottom of the list. 问题是可放置目标位于可排序列表的底部(我不想移动它),一旦发生丢弃,可排序项目就会移动到列表的底部。

What I want to do is cancel this sort when the drop event fires. 我想要做的是在drop事件触发时取消此类。

You can see my problem in action here (just drag "Item 1" onto the "Drop to Copy Item" area and you will see the sort does not get cancelled) 您可以在此处查看我的问题 (只需将“项目1”拖到“删除到复制项目”区域,您将看到排序未被取消)

As you can see I have tried the following in the droppable "drop" event (suggested from JQuery UI Docs) but it does not seem to work... 正如您所看到的,我在droppable“drop”事件中尝试了以下内容(从JQuery UI Docs建议),但它似乎不起作用......

$(this).sortable('cancel');

I am also open to any other recommendations on how to achieve this "drop to copy" effect I am looking for. 我也对如何实现我正在寻找的“复制”效果的任何其他建议持开放态度。

Thanks 谢谢

OK, so I have worked out a solution which does the job. 好的,所以我找到了一个完成这项工作的解决方案。

the cancel code does work if you have it in the "stop" event of the sortable function. 如果您在可排序函数的“停止”事件中有取消代码,它将起作用。 However, it will only apply once the "revert" has completed. 但是,只有在“恢复”完成后才会应用。 The problem is that I was trying to copy/revert the element from the droppable "drop" event and this was too early. 问题是我试图从droppable“drop”事件中复制/恢复元素,这太早了。

The solution is to wait for the "stop" event to complete, and to achieve this I had to create a "awaiting copy" flag, to be checked in the "stop" event. 解决方案是等待“停止”事件完成,为了实现这一点,我必须创建一个“等待副本”标志,以便在“停止”事件中进行检查。

Here is an example 这是一个例子

It still doesn't feel right (UX-wise) but it works correct, and you could always set revert to false on the sortable function to get a slightly better feel. 它仍然感觉不对(UX-wise)但它的工作正确,你总是可以在可排序函数上设置revert为false以获得更好的感觉。

The code from the example is as follows... 示例中的代码如下......

var itemCount = 3;
var awaitingCopy = false;

$(init);

function init() {
    $("#Items").sortable({
        revert: true,
        placeholder: "ItemPlaceHolder",
        opacity: 0.6,
        start: StartDrag,
        stop: StopDrag
    });

    $("#CopyItem").droppable({
        hoverClass: "CopyItemActive",
        drop: function(event, ui) {
            awaitingCopy = true;
        }
    });

    $("#NewItem").click(function(e) {
        e.preventDefault();
        itemCount++;
        var element = $("<div class='Item'>Item " + itemCount + "</div>");
        $("#Items").append(element);
        element.hide().slideDown(500);
    });
}

function CopyItem(element) {
    awaitingCopy = false;
    var clone = element.clone();
    $("#Items").append(clone);
    clone.hide().slideDown(500);
}

function StartDrag() {
    $("#NewItem").hide();
    $("#CopyItem").show();
}

function StopDrag(event, ui) {
    if (awaitingCopy) {
        $(this).sortable('cancel');
        CopyItem($(ui.item));
    }
    $("#NewItem").show();
    $("#CopyItem").hide();
}

Anyway, hopefully this will help others who want the same kind of effect... no stealing my design though ;) 无论如何,希望这会帮助那些想要同样效果的人......虽然没有偷我的设计;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM