Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery draggable / sortable on dynamically-created elements

After some blood, sweat and luckily no tears I've managed to create a drag and drop system which fits my needs.

There are only 2 things that are almost triggering my tears...

Here's the jsfiddle

The problem is in these lines of code, but can't find it:

if (dropped === original) {

    $("#dropzone").append('<li class="placeholder" data-id="" data-order="" data-content=""></li>');

    init();

}

$(".remove").click(function() {

    var removable = $(this).parent();

    if (dropped > original) {

        removable.remove();
        removable.removeClass("dropped");
        removable.removeClass("ui-droppable-disabled");

    } else {

        removable.empty();
        removable.removeClass("dropped");
        removable.removeClass("ui-droppable-disabled");

    }

    init();

});

So now the explanation and my goal:

There are 5 days and on default the placeholders will dynamicly increase with the number of days. If the max limit of placeholders is filled, another one will be appended. Now, after the not-default placeholder is appended and I delete a previous filled placeholder, I can't allow it to be droppable again.

Difficult to explain, but see the demo above ^

Extra: I would like to be able to drag items between those placeholders. But can't find a way either.

Thanks for the help!

like image 461
Jeroen Bellemans Avatar asked Jun 06 '26 15:06

Jeroen Bellemans


1 Answers

You don't seem to reactivate the droppable on delete. And also, destroy them on drop might make you need to recreate them. You could use disable on drop and enable when deleting. Like this:

drop: function (event, ui) {
   var dragging = ui.draggable.clone().find("img").remove();
   $(this).append(dragging).addClass("dropped");
    $(this).droppable('disable');

And later:

if (dropped > original) {
     $(this).parent().droppable('enable')
    removable.remove();
    removable.removeClass("dropped");
    removable.removeClass("ui-droppable-disabled");
       } else {
    $(this).parent().droppable('enable');               
    removable.empty();
    removable.removeClass("dropped");
    removable.removeClass("ui-droppable-disabled");
    }

http://jsfiddle.net/opmd46t2/3/

like image 99
Julien Grégoire Avatar answered Jun 08 '26 05:06

Julien Grégoire



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!