Jquery draggable and droppable, switch to originalPosition another draggable - javascript

I'm working with Jquery UI draggable and droppable.
I have a container that accepts all the divs of the .category1 class, and what I have to do is:
If I put a div into the container that already has another div, revert the old div back to the
original position.
This is the code I have:
$(document).ready(function () {
$(function () {
$("#Part1").draggable({ revert: "invalid", snap: "#Part1Container", snapMode: "inner"
});
$("#Part1Container").droppable({
accept: ".category1",
drop: function (event, ui) {
//Some other logic
}
}
});
And the Html
<div id="Part1" class="ui-widget-content category1"></div>
<div id="Part2" class="ui-widget-content category1"></div>
I want the Part1 to be back to the original position if I insert Part2 in the container, and viceversa. And this has to work for n divs of the same class.
Any suggestions?
Tnx in advance :)

I solved it by assigning it a class when it goes into the container the first time, and then delete it when moving another div
$(".inCategory2").animate({
top: 0,
left: 0
}).removeClass("inCategory2");
ui.draggable.addClass("inCategory2");

Related

How can I make draggable elements droppable instead of appending?

I have a simple drag n drop it is working fine as I expected
but,
Problem:
The only problem is the elements are appending
What I want:
I don't want to append the elements I just want that when I drag n drop the element
they appear as normally happen.
My Code:
$( "#editorDesignView" ).droppable({
accept: '.textTemplate',
drop: function( event, ui ) {
var html = '<div id="" style="background: #eee; width: 80%; margin: 10px auto; padding: 10px;"><p contenteditable="true" style="padding: 5px;">'+ui.draggable.text()+'.</p></div>';
$(html).appendTo(this).hide().slideDown();
}
});
Here is my fiddle.
UPDATE:
I don't want to remove or want the same element as in left side. I just want that when I drop the element I can drop it anywhere
UPDATED FIDDLE:
Fiddle. Please check the console for the result. I want the result to be printed on droppable are without appending it.
you want a same representation on right column, as in left. And you can drop in anywhere at right column.
$(function(){
$(".textTemplate").draggable({
helper: "clone",
zIndex: 2500,
});
$( "#editorDesignView" ).droppable({
accept: '.textTemplate',
drop: function( event, ui ) {
console.log(ui.draggable.text(), event, ui);
var html = '<div>'+ui.draggable.text()+'</div>';
$(html).appendTo(this).hide().slideDown();
}
});
});
Here is my fiddle

jQuery draggable isn't draggable after being dropped

I have two divs, each containing a list of quantities and items. The items are draggable and the div containing the items is droppable. If an item of the same name appears in that div, that item cannot be dropped on that div again. Eventually I will be controlling the number of items that are moved, but I'm just trying to get this part to work for now.
The problem I'm having is that once I drop an item on the div, I can no longer drag it. I've tried adding draggable() to the new item that I am creating, but that just made things weird.
The exact same code worked when I was adding an li to a ul, but I wanted to move just the name of the item and not the quantity, so I am using divs instead of li.
Here is a jsfiddle of my code: http://jsfiddle.net/imjustabill/eJ4KH/
And here are the pertinent parts
$(function () {
$("#inventory1 .item").draggable({
appendTo: "body",
helper: "clone"
});
$("#inventory2").droppable({
drop: function (event, ui) {
var item = ui.draggable.attr("data-item");
var qty = ui.draggable.attr("data-qty");
var itemDesc = ui.draggable.text();
if (!$(this).find("[data-item='"+item+"']").length) {
$("<div class='qty'></div>").text(qty).appendTo(this);
$("<div class='item ui-draggable' data-qty='"+qty+"' data-item='"+item+"'></div>").text(itemDesc).appendTo(this);
}
}
});
$("#inventory2 .item").draggable({
appendTo: "body",
helper: "clone"
});
$("#inventory1").droppable({
drop: function (event, ui) {
var item = ui.draggable.attr("data-item");
var qty = ui.draggable.attr("data-qty");
var itemDesc = ui.draggable.text();
if (!$(this).find("[data-item='"+item+"']").length) {
$("<div class='qty'></div>").text(qty).appendTo(this);
$("<div class='item ui-draggable' data-qty='"+qty+"' data-item='"+item+"'></div>").text(itemDesc).appendTo(this);
}
}
});
});
<div id="inventory1">
<h4>Inventory 1</h4>
<div class="qty">7</div><div class="item" data-qty="7" data-item="item1">Item 1</div>
<div class="qty">5</div><div class="item" data-qty="5" data-item="item2">Item 2</div>
<div class="qty">2</div><div class="item" data-qty="2" data-item="item3">Item 3</div>
<div class="qty">9</div><div class="item" data-qty="9" data-item="item4">Item 4</div></div>
<div id="inventory2">
<h4>Inventory 2</h4>
<div class="qty">4</div><div class="item" data-qty="4" data-item="item1">Item 1</div>
<div class="qty">2</div><div class="item" data-qty="2" data-item="item5">Item 5</div></div>
Any ideas? Thank you.
The Problem is, that the Draggable calls it's destroy() on drop so you loose your dragging ability. One dirty way is to make your dropped element draggable again:
$("#inventory2").droppable({
drop: function (event, ui) {
var item = ui.draggable.attr("data-item");
var qty = ui.draggable.attr("data-qty");
var itemDesc = ui.draggable.text();
if (!$(this).find("[data-item='"+item+"']").length) {
$("<div class='qty'></div>").text(qty).appendTo(this);
$("<div class='item ui-draggable' data-qty='"+qty+"' data-item='"+item+"'></div>").text(itemDesc).draggable({appendTo: "body", helper: "clone"}).appendTo(this);
}
}
});
But it won't be droppable again.
Do you have to use draggable/droppable?
Just use jQueryUI's own Sortable with connected lists. Its already perfectly implemented to jQueryUI:
jQueryUI Sortable with connected lists

dragged image if not re-sizing and dragging with in the droppable area after dropping ?

i'm just trying to drag a clone of a image to div once its dropped then i want that clone to be draggable and resizable but with containment in that div only. When i make cloned image its working fine se here Jfiddle!!.
HTML
<div class="option" id="f">
<img class="options" src="http://lorempixel.com/90/90/" alt=""/>
</div>
<div class="lame">
<img src="http://placehold.it/350x150" alt=""/>
</div>
Jquery
$(function() {
$( ".options" ).draggable({ cursor: "pointer",opacity: 0.6,helper: "clone"
});
$(".lame").droppable({ accept:".options",drop: function(event, ui) {
$.ui.ddmanager.current.cancelHelperRemoval = true;
$(".ui-draggable").hover(function() {
$(this).addClass('fix');
$(this).draggable({containment: ".lame"});
}, function() {
$(this).removeClass('fix');
});
}
});
});
but when add resizable to it the draggable stops working and image moves down. you can see it here Link . Also on fiddle image didn't move down see !!
but draggable is also not working here also. please tell me how to fix this or either do this in correct way using latest Jquery-ui Version.
I was just looking into your request from the previous question and I saw that you had asked a new question.
Here is what I came up with.
HTML
<div class="option" id="f" style="display:inline-block;">
<img class="options" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTpqrkAF5pPbe8N9fko9gLyZalAyeSm4p-dyU72YD3FuvmDHCW4" alt=""/>
</div>
<br>
<div class="lame" style="display:inline-block;">
<img src="http://placehold.it/350x150" alt=""/>
</div>
Notice the style="display:inline-block;" this is important so that your .option and .lame div's do not span accross the whole screen.
jQuery
$(function() {
$( ".option" ).draggable({ cursor: "pointer",opacity: 0.6,helper: "clone"});
$(".lame").droppable({
accept:".option",drop: function(event, ui) {
$.ui.ddmanager.current.cancelHelperRemoval = true;
$(ui.helper).draggable({cursor : "pointer",opacity: 0.6,containment :".lame"});
$(ui.helper).find('.options').resizable({containment : ".lame"});
}
});
});
Instead of calling .draggable on .options we are calling it on .option the container div.
Once the image has been dropped in .lame we need to make .option (or div) draggable. And the .options or image re sizable. Seems to work pretty good when we do it this way.
Take a look at the example.
http://jsfiddle.net/7kkW3/2/
In your hover event, try making the parent draggable instead of the img itself.
When the jquery ui makes an img tag resizable it wraps it inside another div causing some problems.
$(".ui-draggable").hover(function()
{
$(this).addClass('fix');
$(this).parent().draggable({containment: ".lame"});
$(this).resizable({containment: ".lame"});
}

jQuery UI Sortable connected list css issues

I am using jquery ui sortable with connected lists. I have 2 problems with one most important requirement.
Items goes behind the other list while dragging li.ui-splitselect-item
Dragging item to right(when mouse is too right) creates Horizontal scroll
IMPORTANT: Lists ul.ui-splitselect-list should have overflow-y:auto; So Header of lists stay fixed
and only list items are scrolled
NOTE:
I previous asked this question on STACKOVERFLOW but didnt notice my important requirement was missing from solution so i opened question again with clarity.
JSFIDDLE: http://jsfiddle.net/bababalcksheep/z67Td/
HTML Mockup:
<div class="ui-splitselect ui-helper-clearfix ui-widget ui-widget-content" style="height:200px;" >
<div class="ui-widget-content ui-splitselect-selected">
<div class="ui-widget-header ui-helper-clearfix">
List1
</div>
<ul id="sortable1" class="ui-splitselect-list" style="height: 332px;">
<li class="ui-splitselect-item ui-state-default">
<a class='ui-splitselect-handle-drag'><span class='ui-icon ui-icon-carat-2-n-s'></span></a>
<span class="ui-splitselect-handle-select">TEST-List1</span>
<a class="ui-splitselect-handle-move" href="#"><span class="ui-icon ui-icon-plus"></span></a>
</li>
</ul>
</div>
<div class="ui-widget-content ui-splitselect-available" >
<div class="ui-widget-header ui-helper-clearfix">
List2
</div>
<ul id="sortable2" class="ui-splitselect-list" style="height: 332px;">
</ul>
</div>
</div>
CSS:
.ui-splitselect{font-size:.8em;width:100%!important;text-align:center;margin:0 auto;padding:0}
.ui-splitselect ul{-moz-user-select:none}
.ui-splitselect .ui-widget-header{border:none;font-size:11px}
.ui-splitselect-selected{height:100%!important;float:left;border:none;width:50%;margin:0;padding:0}
.ui-splitselect-available{height:100%!important;width:49.4%;float:left;border-top:none;border-bottom:none;border-right:none;margin:0;padding:0}
.ui-splitselect-list{height:92%!important;position:relative;list-style:none;overflow:auto;margin:0;padding:0}
.ui-splitselect-item{cursor:default;line-height:20px;height:20px;font-size:11px;list-style:none;display:list-item;white-space:nowrap;overflow:hidden;margin:1px;padding:0}
.ui-splitselect-item.ui-sortable-helper{z-index:99999}
.ui-splitselect-handle-select{float:left}
.ui-splitselect-handle-drag{float:left;height:20px;border-top:0;border-bottom:0;cursor:pointer;margin:0 10px 0 5px;padding:2px 5px}
.ui-splitselect-handle-move{text-decoration:none;cursor:pointer;float:right;height:20px;border-top:0;border-bottom:0;margin:0 5px 0 10px;padding:2px 5px}
JS:
$("#sortable1, #sortable2").sortable({
connectWith: ".ui-splitselect-list",
containment: ".ui-splitselect",
scroll: false,
placeholder: "ui-state-highlight ui-splitselect-item"
}).disableSelection();
Try adding appendTo: document.body and helper: clone options for sortable, like this:
$("#sortable1, #sortable2").sortable({
appendTo: document.body,
helper: "clone",
connectWith: ".ui-splitselect-list",
containment: ".ui-splitselect",
scroll: false,
placeholder: "ui-state-highlight ui-splitselect-item"
}).disableSelection();
Js fiddle: http://jsfiddle.net/XpP25/2/
Trick is in creating sorting helper that clones your original element, and then appending it to body element to resolve zIndex issues. All helpers are automatically removed after stop event of draggable and sortable, so it shouldn't mess your code :)
Hope it helps.
I was having a similar issue with nested sortable items. I have multiple sortable containers, that make up a grid, then widgets inside of those sortable containers. Inside of each widget there is a nested sortable container which takes up the width and height. Inside of the nested sortables I can have multiple nested-widgets, spanning the width of the nested sortable.
Here is how one of those sortables would look:
<div class="sortable">
<div class="widget">
<div class="nested-sortable">
<div class="nested-widget"></div>
</div>
</div>
</div>
Now, once I started dragging my nested sortable item, it would go behind every outer widget except for the one containing it. I tried changing the z-index of the items on sort start and no luck. I needed something like jQuery UI's Draggable stack option, which "stacks" the currently dragged item over all other items of the same class.
Here is jQuery UI's draggable stack function on github at line 800.
So, my friend and I modified that code a bit, and called it on the "outer" widget.
$.fn.stack = function () {
// where $(this) == '.widget'
var o = $(this).closest('.sortable').siblings('.sortable').find('.widget');
// create an array of all the widgets
var group = $(o).sort(function(a,b) {
// compare the each set of widgets
return (parseInt($(a).css("z-index"),10) || 1) - (parseInt($(b).css("z-index"),10) || 1);
});
if (!group.length) { return; }
var min = parseInt($(group[0]).css('z-index')) || 1; // first widget's z-index
$(group).each(function(i) {
// increase each widget's z-index by min + $(this) widget's index value
$(this).css('z-index', min + i);
});
// make currently dragged widget's z-index min + length of group
$(this[0]).css('z-index', min + group.length);
}
Then call in sortable start:
$('.nested-sortable').sortable({
start: function(event, ui) {
$(this).closest('.widget').stack();
}
});
Here is my codepen for reference: Nested Grid System

Draggable on nested Droppable

following this example.
<div id="containers">
<div class="frame">
<div class="popup">Popup</div>
<div class="object">Object 1</div>
<div class="object">Object 2</div>
</div>
</div>
I want each object to be draggable to popup.
once inside the popup, have the possibility to draggable again for the frame
The problem is the second time I do it.
When i try draggable object to droppable popup, nothing happenz..
Any Ideas?
here my code.
http://jsfiddle.net/PtLLf/49/
The reason is because by default, when an element is dropped on nested droppables, each droppable will receive the element.
So in your case the drop of the inner is fired, but the outer div is fired too and get the element in itself.
You can set greedy option to true, any parent droppables will not receive the element. The drop event will still bubble normally, but the event.target can be checked to see which droppable received the draggable element.
Ref: http://api.jqueryui.com/droppable/#option-greedy
Code:
$('#containers .frame .popup').droppable({
activeClass: "ui-state-default",
hoverClass: "ui-state-hover",
accept: '.object',
greedy: true ,
drop: function (event, ui) {
$(ui.draggable).addClass("insidePopup");
ui.draggable.detach().appendTo($(this));
}
});
$('#containers .frame').droppable({
activeClass: "ui-state-default",
hoverClass: "ui-state-hover",
accept: '.insidePopup',
greedy: true ,
drop: function (event, ui) {
ui.draggable.detach().appendTo($(this));
$(ui.draggable).removeClass("insidePopup");
}
});
$('#containers .frame .object').draggable({
helper: 'clone',
containment: "document"
});
Demo: http://jsfiddle.net/IrvinDominin/dVFgn/

Categories

Resources