jQuery.sortable. change the order by JavaScript

后端 未结 3 1566
广开言路
广开言路 2020-12-31 12:41

How can I change the order of a list once applied by JavaScript jQuery.sortable.

For example:

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-31 13:25

    Like others have mentioned, this has nothing to do with the sortable() functionality. Instead you need to retrieve the items in the list, shift their positions and then clear and re-insert the list items again in their new order:

    // Get your list items
    var items = $('#mylist').find('li');
    
    // The new index order for each item
    var order = [4, 2, 0, 1, 3];
    
    // Map the existing items to their new positions        
    var orderedItems = $.map(order, function(value) {
        return items.get(value);
    });
    
    // Clear the old list items and insert the newly ordered ones
    $('#mylist').empty().html(orderedItems);
    

提交回复
热议问题