Remove the collapse/expand function if the parent element doesn't have child

Viewed 1110

I'm using the Nestable code (https://dbushell.com/Nestable/). In the list, the user can drag and drop the items until level 2. Basically, the item can have a father, in maximum.

If the item has child, this item show the expand and collapse functions. When the item does not have child, this item doesn't show the expand and collapse functions.

And I have created a function to remove items.

So, when I delete the child item and the parent item doesn't have any child, the collapse and expand functions don't disappear. These functions should be disappear.

JSFiddle

enter image description here enter image description here

The code:

<div class="dd" id="nestable">
            <ol class="dd-list outer">
                <li class="dd-item" data-id="1">
                    <button class='dd-action pull-right' type='button' data-action='remove' title='Remove'><i class='icon-remove'></i></button>
                    <div class="dd-handle">Item 1</div>
                </li>
                <li class="dd-item" data-id="2">
                    <button class='dd-action pull-right' type='button' data-action='remove' title='Remove'><i class='icon-remove'></i></button>
                    <div class="dd-handle">Item 2</div>
                    <ol class="dd-list">
                        <li class="dd-item" data-id="4">
                            <button class='dd-action pull-right' type='button' data-action='remove' title='Remove'><i class='icon-remove'></i></button>
                            <div class="dd-handle">Item 4</div>
                        </li>
                        <li class="dd-item" data-id="9">
                            <button class='dd-action pull-right' type='button' data-action='remove' title='Remove'><i class='icon-remove'></i></button>
                            <div class="dd-handle">Item 9</div>
                        </li>
                        <li class="dd-item" data-id="10">
                            <button class='dd-action pull-right' type='button' data-action='remove' title='Remove'><i class='icon-remove'></i></button>
                            <div class="dd-handle">Item 10</div>
                        </li>
                    </ol>
                </li>
                <li class="dd-item" data-id="11">
                    <button class='dd-action pull-right' type='button' data-action='remove' title='Remove'><i class='icon-remove'></i></button>
                    <div class="dd-handle">Item 11</div>
                </li>
                <li class="dd-item" data-id="12">
                    <button class='dd-action pull-right' type='button' data-action='remove' title='Remove'><i class='icon-remove'></i></button>
                    <div class="dd-handle">Item 12</div>
                </li>
            </ol>
        </div>

Javascript:

$(document).ready(function()
{

    var updateOutput = function(e)
    {
        var list   = e.length ? e : $(e.target),
            output = list.data('output');
        if (window.JSON) {
            output.val(window.JSON.stringify(list.nestable('serialize')));
        } else {
            output.val('JSON browser support required for this demo.');
        }
    };

    // activate Nestable for list 1
    $('#nestable').nestable({
        group: 1,
        expandBtnHTML   : '<button class="dd-action" data-action="expand" type="button"><i class="icon-chevron-right"></i></button>',
        collapseBtnHTML : '<button class="dd-action" data-action="collapse" type="button"><i class="icon-chevron-down"></i></button>',
        customActions   : {
            'remove'    : function(item,button) {
                            item.remove();
                        }
        }
    })
    .on('change', updateOutput);

    // output initial serialised data
    updateOutput($('#nestable').data('output', $('#nestable-output')));

    $('#nestable-menu').on('click', function(e)
    {
        var target = $(e.target),
            action = target.data('action');
        if (action === 'expand-all') {
            $('.dd').nestable('expandAll');
        }
        if (action === 'collapse-all') {
            $('.dd').nestable('collapseAll');
        }
    });

});
0 Answers
Related