JQuery UI Sortable 接收前禁用更新功能

发布于 2024-10-30 18:36:17 字数 745 浏览 2 评论 0原文

我正在使用 jquery ui 处理一个列表,您可以在其中排序,然后也在另一个列表之间排序。我正在使用更新进行内部排序,效果很好。当我在两者之间排序时,我只想调用接收函数而不是更新。目前,更新被调用,然后接收被调用。在列表之间排序时有什么方法可以跳过更新调用吗?

<script>
            $ = jQuery
            $(function() {
                $( "#sortable1).sortable({
                    connectWith: ".connectedSortable",
                    placeholder: "ui-state-highlight",
                    update: function(event, ui) {processSortWithin(ui.item.attr("id"), ui.item.index()); },
                    receive: function(event, ui){ 
                        processSortBetween(ui.item.attr("id"), ui.item.index(),ui.sender.attr("id"));
                    }
                }).disableSelection();
            });

        </script>

I'm using jquery ui to process a list that you can sort within and then also sort between another list. I'm using the update for the sorting within and that works fine. When I sort between I just want to call the receive function and not the update. Currently, update gets called and then receive gets called. Is there any way to skip the update call when sorting between lists?

<script>
            $ = jQuery
            $(function() {
                $( "#sortable1).sortable({
                    connectWith: ".connectedSortable",
                    placeholder: "ui-state-highlight",
                    update: function(event, ui) {processSortWithin(ui.item.attr("id"), ui.item.index()); },
                    receive: function(event, ui){ 
                        processSortBetween(ui.item.attr("id"), ui.item.index(),ui.sender.attr("id"));
                    }
                }).disableSelection();
            });

        </script>

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

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

发布评论

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

评论(3

心意如水 2024-11-06 18:36:17

答案来自: http://forum.jquery.com/topic/sortables-更新回调和连接

update: function(e,ui) {
    if (this === ui.item.parent()[0]) {
        //your code here
    }
}

Answer from: http://forum.jquery.com/topic/sortables-update-callback-and-connectwith

update: function(e,ui) {
    if (this === ui.item.parent()[0]) {
        //your code here
    }
}
神爱温柔 2024-11-06 18:36:17

我今天遇到了类似的问题,我找到的唯一解决方案是引入一个标志变量,该变量在 update 事件期间设置,并在 stop 事件期间检查。

在您的示例中,您使用 receive 事件,该事件将在从其他列表接收新元素的列表上触发,因此应将其设置在 $(".connectedSortable").sortable( ) 选项。

以下是我区分是否排序(在一个列表内,在 stop 中处理)或移动(在两个列表之间,在 receive 中处理)的方法:

$(function() {
    position_updated = false; //helper flag for sortable below

    $(".sortable").sortable({
        connectWith: ".sortable",
        update: function(event, ui) {
            position_updated = !ui.sender; //if no sender, set sortWithin flag to true
        },
        stop: function(event, ui) {
            if (position_updated) {
                processSortWithin(ui.item.attr("id"), ui.item.index());
                position_updated = false;
            }
        },
        receive: function(event, ui) {
            processSortBetween(ui.item.attr("id"), ui.item.index(),ui.sender.attr("id"));
        }
    }).disableSelection();
});

function processSortWithin(id, position) {
    alert("sort within");
}

function processSortBetween(id, position, sender_id) {
    alert("sort between");
}

工作示例:http://jsfiddle.net/myLff/2/

I had a similar problem today, and the only solution I found was to introduce a flag variable that is set during update event, and checked during stop event.

In your example, you use receive event which will be fired on the list that receives new element from some other list, so it should be set inside $(".connectedSortable").sortable() options.

Here is my way to distinguish whether to sort (within one list, processed in stop) or to move (between two lists, processed in receive):

$(function() {
    position_updated = false; //helper flag for sortable below

    $(".sortable").sortable({
        connectWith: ".sortable",
        update: function(event, ui) {
            position_updated = !ui.sender; //if no sender, set sortWithin flag to true
        },
        stop: function(event, ui) {
            if (position_updated) {
                processSortWithin(ui.item.attr("id"), ui.item.index());
                position_updated = false;
            }
        },
        receive: function(event, ui) {
            processSortBetween(ui.item.attr("id"), ui.item.index(),ui.sender.attr("id"));
        }
    }).disableSelection();
});

function processSortWithin(id, position) {
    alert("sort within");
}

function processSortBetween(id, position, sender_id) {
    alert("sort between");
}

Working example: http://jsfiddle.net/myLff/2/

゛清羽墨安 2024-11-06 18:36:17

试试这个:

update: function(e,ui) {
   if (!ui.sender) {
       //your code here
   }
}

Try this:

update: function(e,ui) {
   if (!ui.sender) {
       //your code here
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文