限制可排序的项目数量(以可拖动作为源)?

发布于 2024-11-19 09:23:06 字数 1745 浏览 2 评论 0原文

我有一个通过 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 技术交流群。

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

发布评论

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

评论(2

柠北森屋 2024-11-26 09:23:06

您可以在满足条件时停用连接:

$("#sortable").bind( "sortreceive", function(event, ui) {
    if ($( "#sortable li" ).length > 3){
        $( "#draggable li" ).draggable( "option", "connectToSortable", false );
    }
});

在此处拨弄: http://jsfiddle.net/ZLCDr/1/

You could deactivate the connection when a condition is met:

$("#sortable").bind( "sortreceive", function(event, ui) {
    if ($( "#sortable li" ).length > 3){
        $( "#draggable li" ).draggable( "option", "connectToSortable", false );
    }
});

Fiddle here: http://jsfiddle.net/ZLCDr/1/

天煞孤星 2024-11-26 09:23:06

只需添加 if 子句来检查可排序中的元素数量,并在达到该数量时取消绑定可拖动事件。

编辑:(现在包括多个列表支持)

$('.draggable').draggable({revert:true,helper:'clone',connectToSortable: '.sortable'});

$('.sortable').sortable({
    connectWith:'.sortable',
    receive: function(ui) {
        if($(this).children().length >=6) {
            $(this).children().addClass('filled');
            $(this).addClass('dontDrop');
            $('.sortable').sortable('option', 'connectWith',$('.sortable').not('.dontDrop'));
            $('.draggable').draggable('option', 'connectToSortable',$('.sortable').not('.dontDrop'));
        }else {
            $(this).children().removeClass('filled');
        }
        add_delete();
    }
});
function add_delete() {
    $('.delete').remove();
    $('.sortable>li').append('<span class="delete">--</span>');
    $('.delete').unbind('click').click(function(){
        if($(this).parent().siblings().length <=5) {//for atmost 6 children in sortable
            $(this).parent().parent().children().removeClass('filled');
            $(this).parent().parent().removeClass('dontDrop');
            console.log($('.sortable').not('.dontDrop'));
            $('.sortable').sortable('option', 'connectWith',$('.sortable').not('.dontDrop'));
            $('.draggable').draggable('option', 'connectToSortable',$('.sortable').not('.dontDrop'));
        }
        $(this).parent().hide('slow').remove();
    });
}
add_delete();

在此处检查小提琴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)

$('.draggable').draggable({revert:true,helper:'clone',connectToSortable: '.sortable'});

$('.sortable').sortable({
    connectWith:'.sortable',
    receive: function(ui) {
        if($(this).children().length >=6) {
            $(this).children().addClass('filled');
            $(this).addClass('dontDrop');
            $('.sortable').sortable('option', 'connectWith',$('.sortable').not('.dontDrop'));
            $('.draggable').draggable('option', 'connectToSortable',$('.sortable').not('.dontDrop'));
        }else {
            $(this).children().removeClass('filled');
        }
        add_delete();
    }
});
function add_delete() {
    $('.delete').remove();
    $('.sortable>li').append('<span class="delete">--</span>');
    $('.delete').unbind('click').click(function(){
        if($(this).parent().siblings().length <=5) {//for atmost 6 children in sortable
            $(this).parent().parent().children().removeClass('filled');
            $(this).parent().parent().removeClass('dontDrop');
            console.log($('.sortable').not('.dontDrop'));
            $('.sortable').sortable('option', 'connectWith',$('.sortable').not('.dontDrop'));
            $('.draggable').draggable('option', 'connectToSortable',$('.sortable').not('.dontDrop'));
        }
        $(this).parent().hide('slow').remove();
    });
}
add_delete();

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文