【问题标题】:Limit Max Elements in Drag and Drop panel在拖放面板中限制最大元素
【发布时间】:2011-05-06 12:09:23
【问题描述】:

我的网站上有一个可排序面板 (jQuery UI),但需要将每列中的元素数量限制为最多 12 个。

我尝试了一些方法,但似乎无法正常工作。我需要查看“i”是否为 12 或更大,如果是,请不要更新,但我似乎做不到!

任何人有任何建议或可以正确地推动我吗?

下面是jQuery!

function updateWidgetData(){
    var items=[];
    $('.column').each(function(){
        var columnId=$(this).attr('id');
        $('.dragbox', this).each(function(i){
            var collapsed=0;
            if($(this).find('.dragbox-content').css('display')=="none")
                collapsed=1;
            var item={
                id: $(this).attr('ID'),
                collapsed: collapsed,
                order : i,
                column: columnId
            };
            items.push(item);
        });
    });
    var sortorder={ items: items };

    //Pass sortorder variable to server using ajax to save state

    $.post('includes/updatePanels.php', 'data='+$.toJSON(sortorder), function(response){
        if(response=="success")
            $("#console").html('<div class="success">Your preferences have been saved</div>').hide().fadeIn(1000);
        setTimeout(function(){
            $('#console').fadeOut(1000);
        }, 2000);
    });
}

【问题讨论】:

  • 我在我的答案中添加了可排序的内容,这就是实际问题。 :)

标签: jquery-ui


【解决方案1】:

排序

对于连接的 sortable,解决方案是在拖动开始时计算每个 sortable 中的元素,并禁用具有最大允许元素数的元素。我们需要排除当前的可排序对象,以便重新排序其中的项目并允许拖动当前元素。

这里的问题是,如果我们对任何可排序的事件执行上述操作,已经为时已晚,禁用它们不会有任何效果。解决方案是将检查绑定到项目本身的 mousedown 事件,该事件将在可排序对象获得任何控制之前触发。我们还需要在拖动停止时重新启用所有可排序。

看看这个例子,使用&lt;ul&gt; sortables和&lt;li&gt; items,每个sortable中的最大项目数是3:http://jsfiddle.net/qqqm6/10/

$('.sort').sortable({
    revert: 'invalid',
    connectWith: '.sort',
    stop: function(){
        // Enable all sortables
        $('.sort').each(function(){
            $(this).sortable('enable');
        });
    }
});

$('.sort li').mousedown(function(){
    // Check number of elements already in each sortable
    $('.sort').not($(this).parent()).each(function(){
        var $this = $(this);

        if($this.find('li').length >= 3){
            $this.sortable('disable');
        } else {
            $this.sortable('enable');
        }
    });
})

Draggables 和 droppables

原理很简单,解决方法有点棘手,jQuery UI 中应该有一个适当的选项来取消 drop 操作。如果有,但我错过了什么,请告诉我。

无论如何,以下是您在 drop 事件中检查最大计数的方法(本例中最多 4 个):

$('.drag').draggable({
    revert: 'invalid',
    stop: function(){
        // Make it properly draggable again in case it was cancelled
        $(this).draggable('option','revert','invalid');
    }
});

$('.drop').droppable({
    drop: function(event,ui){
        var $this = $(this);

        // Check number of elements already in
        if($this.find('.drag').length >= 4){
            // Cancel drag operation (make it always revert)
            ui.draggable.draggable('option','revert',true);
            return;
        }

        // Put dragged item into container
        ui.draggable.appendTo($this).css({
            top: '0px',
            left: '0px'
        });

        // Do whatever you want with ui.draggable, which is a valid dropped object
    }
});

看看这个小提琴的实际效果:http://jsfiddle.net/qqqm6/

【讨论】:

    猜你喜欢
    • 2022-09-24
    • 1970-01-01
    • 2017-11-01
    • 2018-06-06
    • 1970-01-01
    • 2011-06-09
    • 2022-10-12
    • 2018-09-29
    • 1970-01-01
    相关资源
    最近更新 更多