Create kids with nested sortable

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/

+4
1

, , . , $(ul:empty) , ul .

$("div.tree ul").sortable({
  items: "li",
  connectWith: "ul",
  placeholder: "placeholder",
  toleranceElement: "> a",
  opacity: 0.5,
  sort: function(event, ui) {
    $("ul.ui-sortable:empty").remove();
    var prev_li = ui.placeholder.prev("li");

    if (prev_li.length) {

      if (ui.helper.position().left > prev_li.position().left + 50) {

        // If the current element has some empty ui sortables
        prev_li.find("ul:not(:has(li))").remove();

        // If the li has no ul 
        if (!prev_li.children("ul").length 

            //or if the ul contains the element that we are currently dragging then we append the new ul
            || prev_li.children("ul").find('li').first().hasClass('ui-sortable-helper')) {

            // If there any other new elements in the list, remove them
            $("ul.new:empty").remove();

            //append hint
            prev_li.append("<ul class='new'></ul>");
        }
      }
    }
  },
  out: function(event, ui) {
    // As we move out of a drop zone we clean it from old uls
    var prev_li = ui.placeholder.prev("li");
    prev_li.find('ul.new').remove()
    prev_li.find('ul.ui-sortable:empty').remove()
  },
  stop: function(event, ui) {
    // We drop our element into the new ul 
    $('ul.new').removeClass('new').append(ui.item);

    // Clean the whole list of empty ul's
    $("ul:empty").remove();
    $("ul.ui-sortable:empty").remove();
  }
}).disableSelection();

: https://jsfiddle.net/Lsn6aj22/3/

, !

+2

Source: https://habr.com/ru/post/1678915/


All Articles