【问题标题】:Detect unacceptable drag drop in jQuery-ui droppable在 jQuery-ui droppable 中检测不可接受的拖放
【发布时间】:2014-06-26 09:46:53
【问题描述】:

我正在使用jQuery-uidraggabledroppable。我的主要container 应该只接受plugin-containers,它们也是接受插件的droppables。下面是我的代码sn-p的代码:

$("#main").droppable({
                activeClass: "ui-state-default",
                hoverClass: "ui-state-hover",
                accept: ".plugin-container",
                drop: function( event, ui ) {
                    $( this ).find( ".placeholder" ).remove();
                    $("<div></div>").html(ui.draggable.html()).appendTo(this).droppable({ accept: '.plugin', drop: function (event, ui) { $('<div></div>').html(ui.draggable.html()).appendTo(this); } }).sortable();
                }

});

问题是当一个插件被拖入主容器时,我想:

  1. Hightlight 可以删除插件的插件容器
  2. 如果插件被放在主容器中,则会显示错误消息

但可放置的 overdrop 方法仅在拖动的项目可接受时才会触发,并且不会因不可接受的拖动而触发(在本例中为 .plugin)。我可以在这里做什么?

这里是fiddle

【问题讨论】:

  • 请创建一个小提琴!
  • @RahulGupta 我加了小提琴

标签: javascript jquery jquery-ui jquery-ui-draggable jquery-ui-droppable


【解决方案1】:

我想我已经找到您问题的确切解决方案 !!

看看这个FIDDLE

代码:

$("#main").droppable({
    activeClass: "ui-state-default",
    hoverClass: "ui-state-hover",

    //accept: ".plugin-container",
    drop: function( event, ui ) {

        if(ui.draggable.hasClass('plugin'))
        {
            alert('This element cannot be dropped here !');
            $(ui.draggable).draggable({ revert: true });
        }   
        else if(ui.draggable.hasClass('plugin-container'))
        {
            //console.log('context = ');
            //console.log(ui.draggable.context);

            $( this ).find( ".placeholder" ).remove();
            $("<div></div>").addClass('plugin-container')
            .html(ui.draggable.html())
            .appendTo(this)
            .droppable({ 
                accept: '.plugin',
                greedy:true,//this will prevent the parent droppables from receiving the droppable object
                drop: function (event, ui) { 
                    $(ui.draggable).draggable({ revert: false });
                    $('<div></div>')
                    .addClass('plugin')
                    .html(ui.draggable.html())
                    .appendTo(this); 
                } 
            }).sortable();
        }
    }

});

$(".menu div").draggable({
     appendTo: "body",
     helper: "clone"
});

【讨论】:

  • 非常感谢。实际上这是一个示例,有很多插件类型,每个容器都可以接受不同类型的插件。我就这样管理它,但实际项目略有不同。再次感谢。
  • 很高兴为您服务!不过真的是下了不少功夫!但毕竟我设法找到了问题的精确解决方案!
【解决方案2】:

您可以让所有的可拖动元素都可以接受,然后在您的 over and drop 方法中过滤想要/不需要的 div 并显示错误消息。

drop: function( event, ui ) {
var dropped = ui.draggable.attr('class');
if (dropped == ".plugin") {
  // show alert
} else {

}

还有 'revert' 选项可用于将拖动的元素恢复到其原始位置。

【讨论】:

  • 我想过这个,但感觉不太对劲。我想我可能会错过一个更好的选择。 ;)
猜你喜欢
  • 2011-04-26
  • 1970-01-01
  • 2021-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-26
  • 1970-01-01
相关资源
最近更新 更多