【发布时间】:2011-07-09 18:34:38
【问题描述】:
我有一个通过connectToSortable 连接到多个 可排序对象的可拖动对象。我想限制您可以放入每个可排序的项目数量。当您从另一个可排序对象中拖动时,我可以这样做,但当您从可拖动对象拖动到可排序对象时则不行。一个简单的例子(如JSBin):
$( ".sortable" ).sortable({
connectWith: ".sortable"
});
$( ".sortable" ).bind( "sortreceive", function(event, ui) {
// This will not work because the sender is a draggable, which has no "cancel" method
if ( 4 < $( this ).sortable( 'toArray' ).length ) {
$(ui.sender).sortable('cancel');
}
});
$( "#draggable li" ).draggable({
connectToSortable: ".sortable",
helper: 'clone'
});
我首先在sortreceive 事件中尝试了$(ui.sender).sortable('cancel');,但是因为发件人是draggable,而不是另一个sortable,所以它没有cancel 方法并且这不起作用(所以@987654322 @ 和 these 问题似乎不能解决我的问题)。我曾尝试关注the logic that glues the draggable and the sortable together,但我看不到任何地方可以跳入并取消“假”停止。
如果有某种视觉反馈会很棒,比如鼠标光标变为no-drop,和/或可排序的背景颜色发生变化。
上下文:这是在 WordPress Stack Exchange 上回答 Limit number of Widgets in Sidebars 的尝试。 WordPress 小部件管理页面有一个容器,其中所有可用的小部件都设置为可拖动的,连接到每个侧边栏的不同可排序容器。我不想修改the core code,只需根据需要使用尽可能少的代码对其进行扩展,以防止在“完整”侧边栏上放置另一个小部件。
【问题讨论】:
标签: jquery jquery-ui-sortable jquery-ui-draggable