I'm trying to get what I saw some plugins like this one , but I can't get it to work correctly.
I want to be able to create new children from a nested sort, for example: when I drag node "4", I would like to put it under node "xx2" and a little to the right. In this action, the placeholder must be indented on the right so that it creates a hint of a new branch.
I managed to create a hint, but I can’t get into this new space.
IMPORTANT: I know that a possible hack for everyone is liempty ul, but I need to have a structure as shown below. Therefore, any new children are created "on the fly."
HTML
<div class="tree">
<ul>
<li>
<a>
<label>
<span>A</span>
</label>
</a>
<ul>
<li>
<a>
<label>
<span>1</span>
</label>
</a>
<ul>
<li>
<a>
<label>
<span>x</span>
</label>
</a>
<ul>
<li>
<a>
<label>
<span>xx1</span>
</label>
</a>
</li>
<li>
<a>
<label>
<span>xx2</span>
</label>
</a>
</li>
</ul>
</li>
</ul>
</li>
<li>
<a>
<label>
<span>2</span>
</label>
</a>
</li>
</ul>
</li>
<li>
<a>
<label>
<span>B</span>
</label>
</a>
<ul>
<li>
<a>
<label>
<span>3</span>
</label>
</a>
</li>
<li>
<a>
<label>
<span>4</span>
</label>
</a>
</li>
<li>
<a>
<label>
<span>5</span>
</label>
</a>
</li>
</ul>
</li>
<li>
<a>
<label>
<span>C</span>
</label>
</a>
<ul>
<li>
<a>
<label>
<span>6</span>
</label>
</a>
<ul>
<li>
<a>
<label>
<span>y</span>
</label>
</a>
<ul>
<li>
<a>
<label>
<span>yy1</span>
</label>
</a>
<ul>
<li>
<a>
<label>
<span>yyy1</span>
</label>
</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
Js
$("div.tree ul").sortable({
items: "li",
connectWith: "ul",
placeholder: "placeholder",
toleranceElement: "> a",
opacity: 0.5,
sort: function( event, ui )
{
$("ul:empty").remove();
var prev_li = ui.placeholder.prev("li");
if(prev_li.length)
{
if(ui.helper.position().left > prev_li.position().left + 50)
{
if(!prev_li.children("ul").length)
{
prev_li.append("<ul class='new'></ul>");
}
}
}
},
stop: function( event, ui )
{
$("ul.new:empty").remove();
}
}).disableSelection();
CSS
.placeholder { height: 20px; border: 1px dotted red;}
ul.new {border: 1px dashed blue; height: 20px;}
FIDDLE: https://jsfiddle.net/wa614ago/5/