0

I'm using nestedsortable jQuery (http://mjsarfatti.com/sandbox/nestedSortable/). In the list, the user can drag and drop the items until level 3. Basically, the item can have father and grandfather.

If the item don't have child, it's going to append the span element, that include the input file. And if the item is the father or grandfather, cannot show the input file element. So, when I drag and drop an item within another item (father), this is works. But when I drag and drop an item within the item dropped previously, the input file don't desappears.

JSFiddle

I've posted 5 images to understand better.

For example: I have this list

I arrast the item 5 and item 7 to item 6. The item 6 is father of items 5 and 7. The file button appears to theses items because they don't have child.

When I drag and drop the item 5 out from item 6, the button file desappears to this item 5.

When I drag and drop the item 5 to item 7, in item 5, the button file appears (that's correct! But in the item 7, the button file don't desappears. In fact, this button would be desappears!

And if I drag and drop the item 7 to raiz, the button don't desappears.

The code:

<ol class="sortable">
        <li id="list_1"><div><span class="disclose"><span></span></span>Item 1<span title="Click to delete item." data-id="1" class="deleteMenu ui-icon ui-icon-closethick">X</span></div>
            <ol>
                <li id="list_2"><div><span class="disclose"><span></span></span>Sub Item 1.1<span title="Click to delete item." data-id="2" class="deleteMenu ui-icon ui-icon-closethick">X</span></div>
                    <ol>
                        <li id="list_3"><div><span class="disclose"><span></span></span>Sub Item 1.2<span title="Click to delete item." data-id="3" class="deleteMenu ui-icon ui-icon-closethick">X</span></div>
                    </ol>
            </ol>
        <li id="list_4"><div><span class="disclose"><span></span></span>Item 2<span title="Click to delete item." data-id="4" class="deleteMenu ui-icon ui-icon-closethick">X</span></div>
        <li id="list_5"><div><span class="disclose"><span></span></span>Item 3<span title="Click to delete item." data-id="5" class="deleteMenu ui-icon ui-icon-closethick">X</span></div>
            <ol>
                <li id="list_6" class="mjs-nestedSortable-no-nesting"><div><span class="disclose"><span></span></span>Sub Item 3.1 (no nesting)<span title="Click to delete item." data-id="6" class="deleteMenu ui-icon ui-icon-closethick">X</span></div>
                <li id="list_7"><div><span class="disclose"><span></span></span>Sub Item 3.2<span title="Click to delete item." data-id="7" class="deleteMenu ui-icon ui-icon-closethick">X</span></div>
                    <ol>
                        <li id="list_8"><div><span class="disclose"><span></span></span>Sub Item 3.2.1<span title="Click to delete item." data-id="8" class="deleteMenu ui-icon ui-icon-closethick">X</span></div>
                    </ol>
            </ol>
        <li id="list_9"><div><span class="disclose"><span></span></span>Item 4<span title="Click to delete item." data-id="9" class="deleteMenu ui-icon ui-icon-closethick">X</span></div>
        <li id="list_10"><div><span class="disclose"><span></span></span>Item 5<span title="Click to delete item." data-id="10" class="deleteMenu ui-icon ui-icon-closethick">X</span></div>
    </ol>

Javascript:

$('ol.sortable').nestedSortable({
        forcePlaceholderSize: true,
        handle: 'div',
        helper: 'clone',
        items: 'li',
        opacity: .6,
        placeholder: 'placeholder',
        revert: 250,
        tabSize: 25,
        tolerance: 'pointer',
        toleranceElement: '> div',
        maxLevels: 3,
        isTree: true,
        expandOnHover: 700,
        startCollapsed: true,
        stop: function(event, ui) {
            //get item id
            var id = $(ui.item).attr('id');
            var res = id.substr(5);

            //check the item have child
            if($('#list_'+res).find('ol').length != 0) {
                //The item have child!
                $(this).parent('span.file_upload').remove();
            } else {
                //The item don't have child!
                //check the item have father
                if ($('#list_'+res).parent().is('ol.sortable')) {
                    //The item don't have father
                    $('#list_'+res).find('span.file_upload').remove();
                } else {
                    //The item have father!
                    //avoid multiple button file (only one!)
                    if ($('#list_'+res).find('span.file_upload').length == 0) {
                        //insert the file button
                        $('#list_'+res).append(
                            '<span class="file_upload" title="Click to upload a file." id="res" class="deleteMenu">'
                            + '<input name="upload_file" type="file" class="new_file" />'
                            + '</span>');
                    }
                }
            }
        }

    });
marcelps
  • 309
  • 2
  • 16
  • The end tag on some of the `
  • ` elements are not optional in this case: https://stackoverflow.com/a/20550925/66580 E.g. `list_1`, `list_2`.
  • – Majid Fouladpour May 29 '17 at 21:48
  • @MajidFouladpour, I put end tag but it's didn't solved. Thank you – marcelps May 29 '17 at 22:00
  • Have tried setting breakpoints and stepping through the code as it executes? I would also add a `console.log(this);` above `$(this).parent('span.file_upload').remove();` to make sure `this` is what you assume it is. – Majid Fouladpour May 29 '17 at 22:23
  • @MajidFouladpour, in the console log, if the item have child, this is printed:
      . And if the item don't have child, don't print. Any suggestions?
    – marcelps May 30 '17 at 15:41
  • @MajidFouladpour, I've put $('#list_'+res).parent().find('span.file_upload').remove(); but the problem is happening. – marcelps May 30 '17 at 15:51
  • Maybe you are adding the file upload input to a slightly different node than you assume, it is still rendered such that from the looks you cannot tell it is misplaced; then when trying to remove it, you look under the parent it was supposed to be added and nothing is found to remove ... If you had this somewhere accessible on the web, I could probe further. – Majid Fouladpour May 30 '17 at 16:34
  • @MajidFouladpour, I've put the complete code in JSFiddle to check. Thanks! – marcelps May 30 '17 at 16:34
  • @MajidFouladpour, so I've checked in console, and the command $(this).parent('span.file_upload').remove() calls the main parent, that is ol.sortable. That is correct, this is works but when I have an grandparent, the command returns ol.sortable too. In fact, should be return the li parent of this node. Have you agreed? – marcelps May 30 '17 at 16:42