Hany
Hany

Reputation: 1

jQuery Sortable How to get new parent data-id

I am using jQuery sortable to manage multi-level categories. I have searched online for an answer on how to get the new parent id of the dragged item. Any help is appreciated.

http://johnny.github.io/jquery-sortable

This what I have so far

var group = $("ol.cat-list").sortable({
group: 'serialization',
delay: 500,
onDrop: function ($item, container, _super) {
    // console.log($item.data('id'));// drop Source

    //get the new parent id // target

    var data = group.sortable("serialize").get();

    var jsonString = JSON.stringify(data);

    $('#nestable-output').text(jsonString);
    _super($item, container);
}
});

HTML

<ol class="cat-list dd-list">
<li class="dd-item dd3-item" data-id="1">
    <div class="dd-handle dd3-handle"></div>
    <div class="dd3-content">Women</div>
    <ol class="dd-list">
        <li class="dd-item dd3-item" data-id="2">
            <div class="dd-handle dd3-handle"></div>
            <div class="dd3-content">Apparel</div>
        </li>
    </ol>
</li>
<li class="dd-item dd3-item" data-id="3">
    <div class="dd-handle dd3-handle"></div>
    <div class="dd3-content">Lingerie</div>
</li>
</ol>

Upvotes: 0

Views: 677

Answers (1)

Oleksii Zemliakov
Oleksii Zemliakov

Reputation: 135

You can find a parent by the item and get its id. Try this code inside the onDrop handler:

console.log($item.closest('.cat-list').data('id'));

Upvotes: 1

Related Questions