I'm using JQuery UI for drag and drop tree nodes and its working fine.
I need to improvise it in terms of much more user interactive, When i drag any element it shows the same element name, you can see in the image (Test 3 I've dragged to down).
But my requirement is when I drag any tree node it should show the image specified, instead of same text. Is it possible?
My draggable function as follows, what changes i need to do to achieve the requirement.
$("li a").draggable({
revert: "invalid",
containment: "document",
helper: "clone",
cursor: "move",
start: function(event, ui) {
var draggedId = event.target.id;
}
});
As was suggested, you can make a special helper. Here is a working example: https://jsfiddle.net/Twisty/ja1635y9/
HTML
<ul class="tree">
<li class="top">Liabilities
<ul>
<li class="sub"><a href="https://www.eliteediting.com/images/get-free-instant-quote-small-button-hover.png">Test 0</a></li>
<li class="sub"><a href="https://www.eliteediting.com/images/get-free-instant-quote-small-button-hover.png">Test 1</a></li>
<li class="sub"><a href="https://www.eliteediting.com/images/get-free-instant-quote-small-button-hover.png">Test 2</a></li>
<li class="sub"><a href="https://www.eliteediting.com/images/get-free-instant-quote-small-button-hover.png">Test 3</a></li>
</ul>
</li>
</ul>
JQuery
$(function() {
$(".sub a").draggable({
revert: "invalid",
containment: "document",
helper: function(e, ui) {
var $img = $(this).attr("href");
var $d = $("<div>");
$d.css({
"width": "250px",
"height": "48px",
"background-image": "url(" + $img + ")",
"background-repeat": "no-repeat",
"padding": "12px 0"
});
var $text = $("<span>" + $(this).text() + "</span>");
$text.css({
"display": "block",
"color": "#fff",
"text-align": "center",
"width": "100%"
});
$d.append($text);
return $d;
},
cursor: "move",
start: function(event, ui) {
var draggedId = event.target.id;
}
}).click(function(e) {
e.preventDefault();
});
});
The function creates a div
and sets the background, nice and tidy. I then added the text to a span
inside, so I could position it easier. Since it is seen as a link, I suppressed the click
event.
Use helper: function() for that. It allows for a helper element to be used for dragging display.
helper: function() {
return $("<div></div>").append($( '<img src="http://www.flooringvillage.co.uk/ekmps/shops/flooringvillage/images/request-a-sample--547-p.jpg" width="100">' ));
}
Working Demo
Reference