B L Praveen
B L Praveen

Reputation: 2010

Sortable Jquery Ui sort array of objects

I have Itinerary saved in an variable in the format below

let Itinerary = [{placeid:'123',address:'Bordman'},
                 {placeid:'124',address:'Bardman'},
                 {placeid:'125',address:'Burdman'}]

I am using jquery ui sort to sort the itinerary listed using html ul tag shown as below

<ul id="cab_itenary">
<li class="unsortable">Head</li>
<li>Bordman</li>
<li>Bardman</li>
<li>Burdman</li>
<li class="unsortable">Footer</li>
<ul>

When I move the itinerary item li I need the Itinerary variable to be sorted. Which I pass the Itinerary variable to the ajax request

$(function() {
        $("#cab_itenary").sortable({

            cancel: ".unsortable",
            start: function(evt, ui) {
                ui.item.data('pos', ui.item.index());
            }

            ,
            stop: function(evt, ui) {
                moveIndex(ui.item.data('pos'), ui.item.index())
            }
        });
});

function moveIndex(fromIndex, toIndex) {
    const element=itenary.splice(fromIndex, 1)[0];

    console.log(element);

    itenary.splice(toIndex, 0, element);


}

How do I sort array of objects If I know the index?

jsfiddle

Upvotes: 0

Views: 24

Answers (0)

Related Questions