取消拖动可排序项目
绝对常见的可排序案例:
$(function() { $("#sortable").sortable(); }); - Item 1
- Item 2
- Item 3
问题。 需要在某些条件下取消拖动项目并且Andrew Whitaker有一个很好的建议,但是这种方法仅适用于jquery-ui-draggable并且无法进行排序:
$("#sortable").sortable({ start: function() { return false; // will still cause `this.helper is null` } });
对建议会很有帮助。
sort
函数回调对draragable( demo )的拖动进行排序:
$("#sortable").sortable({ sort: function() { if ($(this).hasClass("cancel")) { $(this).sortable("cancel"); } } });
Sortable具有使用sortable('cancel')
调用的“取消”function。
从文档:“取消当前可排序的更改并将其还原到当前排序开始之前的状态。” 请参见http://api.jqueryui.com/sortable/#method-cancel 。
用法示例:
$("#sortable").sortable({ stop: function(e, ui) { if ("I need to cancel this") { $(ui.sender).sortable('cancel'); } } });
像fudgey那样返回false
表示非常适合使动态变得不可靠,但在sortable配置中还有一个cancel
选项 ,它允许你设置静态unsortables:
$("#sortable").sortable({ // this prevents all buttons, form fields, and elemens // with the "unsortable" class from being dragged cancel: ":input, button, .unsortable" });
试试这个例子
$( '#列表1')。排序({ connectWith:'ul' }); $( '#列表2')。排序({ connectWith:'ul', 接收:function(ev,ui){ 如果(ui.item.hasClass( “数字”)) ui.sender.sortable( “取消”); } });