【问题标题】:jQuery Drag and Droppable - Hide placeholder if droppable area has li elementsjQuery Drag and Droppable - 如果可放置区域有 li 元素,则隐藏占位符
【发布时间】:2015-04-30 07:51:38
【问题描述】:

我正在使用 jQuery Droppable 插件。

如果 Dropped <ul> 有一个或多个元素,我想隐藏一个占位符。它适用于拖放功能。最重要的是,我还实现了 click 方法。它还将通过从 Dragged 容器中单击星形图标来克隆 <li> 元素。但是在这种方法中,我无法隐藏占位符。


FIDDLE

请检查下面的代码:::

HTML

<div class="mn-items">
  <h2>Drag</h2>
  <div id="catalog">
      <ul class="rp-draggable">
        <li class="one">Item 1 <i class="fa fa-star-o"></i></li>
        <li class="two">Item 2 <i class="fa fa-star-o"></i></li>
        <li class="three">Item 3 <i class="fa fa-star-o"></i></li>
        <li class="four">Item 4 <i class="fa fa-star-o"></i></li>
        <li class="five">Item 5 <i class="fa fa-star-o"></i></li>
        <li class="six">Item 6 <i class="fa fa-star-o"></i></li>
      </ul>
  </div>
</div>

<div class="header-favorites">
  <h2>Drop Here...</h2>
    <ul class="h-droped-list">
      <li class="placeholder">Placeholder (hides if it has items)</li>
    </ul>
</div>

jQuery

$(document).ready(function(){

    /* jQuery Droppable */
    $(function() {
        $( ".mn-items .rp-draggable li" ).draggable({
            appendTo: "body",
            helper: "clone"
        });
        $( ".header-favorites ul" ).droppable({
            activeClass: "ui-state-default",
            hoverClass: "ui-state-hover",
            accept: ":not(.ui-sortable-helper)",
            drop: function( event, ui ) {
                $( this ).find( ".placeholder" ).remove();
                $(ui.draggable).clone().appendTo(this);
            }
        }).sortable({
            items: "li:not(.placeholder)",
            sort: function() {
                $( this ).removeClass( "ui-state-default" );
            }
        });
    });


    /* Click Star Icon to Add to Drop Here Container */
    $('ul.rp-draggable li .fa-star-o').click(function(){
        $(this).closest('li').clone().appendTo('.h-droped-list');
        $(this).closest('li').toggleClass('addedToFav');
    });

    /* Click Close Icon to Remove from Drop Here Container */
    $("ul.h-droped-list").on('click','li .fa-star-o',function(){
        $(this).closest('li').remove();
    });

    /* Hide Placeholder if it has Items, Show if it is empty */
    if($(".header-favorites ul li:not(.placeholder)").length > 0) {
        $(".header-favorites ul li.placeholder").hide();
    }else{
        $(".header-favorites ul li.placeholder").show();
    }


});

【问题讨论】:

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


    【解决方案1】:

    我会给你一个链接到Fiddle。我做了一些可能对您有所帮助的更新,但您仍有一些工作要做。如果您需要,我稍后会帮助您。

    js

    $(document).ready(function(){
    
    /* jQuery Droppable */
    $(function() {
        $( ".mn-items .rp-draggable li" ).draggable({
            appendTo: "body",
            helper: "clone"
        });
        $( ".header-favorites ul" ).droppable({
            activeClass: "ui-state-default",
            hoverClass: "ui-state-hover",
            accept: ":not(.ui-sortable-helper)",
            drop: function( event, ui ) {
                $( this ).find( ".placeholder" ).hide();
                $(ui.draggable).clone().appendTo(this);
            }
        }).sortable({
            items: "li:not(.placeholder)",
            sort: function() {
                $( this ).removeClass( "ui-state-default" );
            }
        });
    });
    
    
    /* Click Star Icon to Add to Drop Here Container */
    $('ul.rp-draggable li .fa-star-o').click(function(){
        var $target = $(this).closest("li"),
            $dropedList = $(".h-droped-list");
    
        if(!$target.hasClass("addedToFav")){
            $target
              .addClass("addedToFav")
              .clone()
              .appendTo($dropedList);
    
            $dropedList
             .find(".placeholder")
             .hide();
        }else{
            //do what you want
        }
    
    });
    
    /* Click Close Icon to Remove from Drop Here Container */
    $("ul.h-droped-list").on('click','li .fa-star-o',function(){
        var $target = $(this).closest("li"),
            $dropList = $target.parent("ul");
    
        $target.remove();
    
    
    
        if($dropList.children().length == 1){
            var $lastItem = $($dropList.children()[0]);
            $lastItem.hasClass("placeholder") && $lastItem.show();
        }
    
    });
    
    /* Hide Placeholder if it has Items or Show if it is empty */
    if($(".header-favorites ul li:not(.placeholder)").length > 0) {
        $(".header-favorites ul li.placeholder").hide();
    }else{
        $(".header-favorites ul li.placeholder").show();
    }
    

    });

    【讨论】:

    • 谢谢@Forin...这对我很有帮助...但是如何通过从 Droppable 容器中删除相同的元素从 Draggable 容器中删除 addedToFav css 类...
    • 优秀的@Florin ..这正是我想要的....感谢您的努力,我已经赞成您的回答并接受了...非常感谢...。请再提供一点帮助,我也想要相同的功能(从 Drop Container 中删除相关元素),当我单击 Drag Container 中的收藏夹链接时...实际上它就像 Toggle on Click star icon from Drag 容器
    • 好的。现在,您可以在单击星形图标时删除项目。另外,当你放下一个物品时,他会被“addToFav”保持一致。 Fiddle
    • 真棒@Florin,我从没想过会比这更好......非常感谢......你是最好的:)
    猜你喜欢
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 2012-05-16
    • 1970-01-01
    • 2011-07-15
    • 2011-09-17
    • 2017-08-15
    相关资源
    最近更新 更多