One thing left in my project was to make the two divs draggable and droppable simultaneously. Now I have the working code of divs that can be dragged and dropped but for example I can drag a div from target area and drop it on users area but I cant seem to figure out a way to drag those divs from users and assign to different users.
$(document).ready(function(){
$(".dragable").draggable({
cancel: "a.ui-icon",
revert: true,
helper: "clone",
cursor: "move",
revertDuration: 0
});
$('.droppable').droppable({
accept: ".dragable",
activeClass: "ui-state-highlight",
drop: function( event, ui ) {
// clone item to retain in original "list"
var $item = ui.draggable.clone();
$(this).addClass('has-drop').append($item);
}
});
});
/
One thing left in my project was to make the two divs draggable and droppable simultaneously. Now I have the working code of divs that can be dragged and dropped but for example I can drag a div from target area and drop it on users area but I cant seem to figure out a way to drag those divs from users and assign to different users.
$(document).ready(function(){
$(".dragable").draggable({
cancel: "a.ui-icon",
revert: true,
helper: "clone",
cursor: "move",
revertDuration: 0
});
$('.droppable').droppable({
accept: ".dragable",
activeClass: "ui-state-highlight",
drop: function( event, ui ) {
// clone item to retain in original "list"
var $item = ui.draggable.clone();
$(this).addClass('has-drop').append($item);
}
});
});
http://jsfiddle/coder880/TpbZk/31/
Share Improve this question edited Jan 29, 2016 at 13:45 Rory McCrossan 338k41 gold badges320 silver badges351 bronze badges asked Jan 29, 2016 at 13:30 CoderCoder 2192 gold badges5 silver badges11 bronze badges 8- What do you want to achieve? – Gavriel Commented Jan 29, 2016 at 13:33
- I have updated your fiddle: jsfiddle/TpbZk/33 – hetasbo Commented Jan 29, 2016 at 13:37
- when you say "assign", do you mean to move the divs instead of copying them? – Gavriel Commented Jan 29, 2016 at 13:40
- @Gavriel i want to drag the div(clone of the div) from target and drop it on user(single or multiple) and then if user wants he can drag a div from user1 and drop it on user 2 or 3 or 4 or anyother and vicerversa. – Coder Commented Jan 29, 2016 at 13:45
- no no just the clone of the div should be assigned to different user – Coder Commented Jan 29, 2016 at 13:46
1 Answer
Reset to default 6The issue is because once the item is dragged/dropped it is then cloned. This clone does not have the draggable()
plugin instantiated on it. You need to call draggable()
on the $item
again. Try this:
var draggableOptions = {
cancel: "a.ui-icon",
revert: true,
helper: "clone",
cursor: "move",
revertDuration: 0
}
$(".dragable").draggable(draggableOptions);
$('.droppable').droppable({
accept: ".dragable",
activeClass: "ui-state-highlight",
drop: function(event, ui) {
var $item = ui.draggable.clone();
$item.draggable(draggableOptions);
$(this).addClass('has-drop').append($item);
}
});
Updated fiddle
it should only be cloned when its from target otherwise it should move it.
To achieve this you need to remove the helper: 'clone'
option in the cloned draggable element and maintain a flag on the element to determine whether it is a brand new clone or has been dragged previously and moved again. To do this you could use a class, something like this:
$(".dragable").draggable({
cancel: "a.ui-icon",
revert: true,
helper: "clone",
cursor: "move",
revertDuration: 0
});
$('.droppable').droppable({
accept: ".dragable",
activeClass: "ui-state-highlight",
drop: function(event, ui) {
var $item = $(ui.draggable)
if (!$item.hasClass('clone')) {
$item = $item.clone().addClass('clone');
$item.draggable({
cancel: "a.ui-icon",
revert: true,
cursor: "move",
revertDuration: 0
});
}
$(this).addClass('has-drop').append($item);
}
});
Example fiddle