限制可排序的项目数量(以可拖动作为源)?
我有一个通过 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'
});
我第一次尝试 $(ui.sender).sortable ('cancel');
在 sortreceive
事件中,但因为发送者是一个 draggable
,而不是另一个 sortable
,所以它确实没有cancel
方法,这不起作用(所以 这些和这些问题似乎没有解决我的问题)。我尝试遵循 逻辑它将可拖动和可排序粘合在一起,但我看不出有什么地方可以跳进去并取消“伪造”的停止。
如果有某种视觉反馈,例如鼠标光标变为no-drop
,和/或可排序的背景颜色变化,那就太好了。
上下文:这是尝试回答限制侧边栏中小部件的数量在 WordPress Stack Exchange 上。 WordPress 小部件管理页面有一个容器,其中所有可用小部件设置为可拖动,连接到每个侧边栏的不同可排序容器。我不想修改 核心代码,只需根据需要使用尽可能少的代码对其进行扩展,以防止在“完整”侧边栏上放置另一个小部件。
I have a draggable that is connected via connectToSortable
to multiple sortables. I want to limit the number of items you can put in each sortable. I can do this when you drag from another sortable, but not when you drag from the draggable to the sortable. A simple example (as 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'
});
I first tried $(ui.sender).sortable('cancel');
in the sortreceive
event, but because the sender is a draggable
, not another sortable
, it does not have a cancel
method and this does not work (so these and these questions don't seem to solve my problem). I have tried following the logic that glues the draggable and the sortable together, but I see no place to jump in and cancel the "faked" stop.
It would be great if there was some kind of visual feedback, like the mouse cursor changing to no-drop
, and/or a background color change on the sortable.
Context: This is an attempt to answer Limit number of Widgets in Sidebars on the WordPress Stack Exchange. The WordPress widget administration page has a container with all available widgets set up as a draggable, connected to different sortable containers for each sidebar. I don't want to modify the core code, just extend it with as little code as needed to prevent dropping another widget on a "full" sidebar.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在满足条件时停用连接:
在此处拨弄: http://jsfiddle.net/ZLCDr/1/
You could deactivate the connection when a condition is met:
Fiddle here: http://jsfiddle.net/ZLCDr/1/
只需添加 if 子句来检查可排序中的元素数量,并在达到该数量时取消绑定可拖动事件。
编辑:(现在包括多个列表支持)
在此处检查小提琴JSFiddle
所以我在这里使用的是:不仅可以将选择器传递给 connectWith/connectToSortable 选项,还可以将元素本身传递给 connectWith/connectToSortable 选项。每当“ul”有 6 个元素时,我就给它指定“dontDrop”类,因此将其从连接中排除。
希望这能为您解决问题。
让赏金就这样来吧:D
Just add the if clause to check the number of elements in the sortable and unbind the draggable event when u have reached that number.
EDIT: (Includes multiple list support now)
Check fiddle here JSFiddle
So what I have used here is: One can pass not just selectors but elements themselves to the connectWith/connectToSortable options. Whenever an 'ul' has 6 elements I give it the class 'dontDrop' and it is hence excluded from the connections.
Hope this clears it out for you.
Let that bounty come this way :D