Reddy
Reddy

Reputation: 1497

jQuery Droppable - Add CSS class if Dropped Area has more than 3 <li> Elements

I am using jQuery Droppable plugin.

If my Dropped Area reaches more than 3 <li> elements, I want to add a CSS Class "stackFavs", otherwise same class has to be removed.

It is happening when I am trying with below code only for Drag n Drop Functionality:.. but I also have onClick Method as well which it is not working in this scenario.

if($('.header-favorites .h-droped-list li').length > 3) {
    $(".h-droped-list").addClass("stackFavs");
}

FIDDLE


jQuery

/* Menu Items Drag n Drop to create Short Cuts in Favorites Bar */
$(document).ready(function(){

    $('.rp-draggable li').not('li.pd-dropdown').each(function (i) {
        $(this).attr('uuid', + i);
    });

    /* jQuery Droppable */
    $(function() {
        $( ".mn-items .rp-draggable li" ).not('li.pd-dropdown').draggable({
            helper: "clone",
            placeholder: "ui-state-highlight",
        });
        $( ".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).addClass("addedToFav").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"),
            id = $target.attr("uuid");

        if(!$target.hasClass("addedToFav")){
            $target.addClass("addedToFav").clone().appendTo($dropedList);
            $dropedList.find(".placeholder").hide();
        }else{
            $dropedList
            .find("li")
            .each(function(index, item) {
                var $elem = $(item);

                if($elem.attr("uuid") == id){
                    $elem.remove();
                    $target.removeClass("addedToFav"); 
                }

                if($dropedList.children().length == 1){
                    var $lastItem = $($dropedList.children()[0]);
                    $lastItem.hasClass("placeholder") && $lastItem.show();
                }
            })
        }

    });

    /* 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"),
            $catalog = $("#catalog ul"),
            id = $target.attr("uuid"),
            $dropList = $target.parent("ul");

        $target.remove();

        $catalog
        .find("li")
        .each(function(index, item){
            var $elem = $(item);

            if($elem.attr("uuid") == id)
                $elem.removeClass("addedToFav");
        })

        if($dropList.children().length == 1){
            var $lastItem = $($dropList.children()[0]);
            $lastItem.hasClass("placeholder") && $lastItem.show();
        }

    });



});

HTML


<div class="mn-items">
    <h2>Drag</h2>
    <ul class="rp-draggable">
        <li>Item 1 <i class="fa fa-star-o"></i></li>
        <li>Item 2 <i class="fa fa-star-o"></i></li>
        <li>Item 3 <i class="fa fa-star-o"></i></li>
        <li>Item 4 <i class="fa fa-star-o"></i></li>
        <li>Item 5 <i class="fa fa-star-o"></i></li>
        <li>Item 6 <i class="fa fa-star-o"></i></li>
    </ul>
</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>

Reference Screenshot - Up to 3 Elements Reference Screenshot - Till 3 Elements

Reference Screenshot - More than 3 Elements Reference Screenshot - More than 3 Elements

Upvotes: 1

Views: 1357

Answers (2)

Irvin Dominin
Irvin Dominin

Reputation: 30993

I made up some fixes to let code work as expected.

A similar code (look at the not placeholder selector):

if ($('.header-favorites .h-droped-list li:not(.placeholder)').length > 3) {
    $(".h-droped-list").addClass("stackFavs");
} else {
    $(".h-droped-list").removeClass("stackFavs");
}

is fired in:

  • drop event
  • star click event
  • remove click event

I have added the catalog id to the main div or the delete wan not working and speeded up the remove bt removeing the each and using an attr selector:

$catalog.find("li[uuid='"+id+"']").removeClass("addedToFav");

Demo: http://jsfiddle.net/j8xu2tvL/

Upvotes: 1

Mohamed-Yousef
Mohamed-Yousef

Reputation: 24001

when append elements to droppable div check for the number of li appended

if($('.h-droped-list li').length <= 3){
   // if number less or equal 3 
   // $('.h-droped-list li').removeClass('.class_bigger_than_three').addClass('.class_less_than_three');
}else{
   // if number bigger than 3 
   // $('.h-droped-list li').removeClass('.class_less_than_three').addClass('.class_bigger_than_three');
}

in your case you can use .addClass() and removeClass() methods to convert between classes

Upvotes: 1

Related Questions