Jquery Sortable Auto Sort

前端 未结 1 2053
隐瞒了意图╮
隐瞒了意图╮ 2020-12-15 11:43

can we sort the jquery sortable at runtime using the id or idx as taken by me in each li. I want it to sort in the run time

here is fiddle . I want it to auto sort f

相关标签:
1条回答
  • 2020-12-15 12:29

    Check this out

    http://jsfiddle.net/wmaqb/2/

    Using the standard jQuery library and the .sort() method you can specify a function to use for sorting your array of objects.

    $('#sort').click(function() {
        var mylist = $('#sortable');
        var listitems = mylist.children('li').get();
        listitems.sort(function(a, b) {
            var compA = parseFloat($(a).attr('id'));
            var compB = parseFloat($(b).attr('id'));
            return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
        });        
        $.each(listitems, function(idx, itm) {
            mylist.append(itm);
        });
    });
    

    Once you got this array sorted, you can simply order them with a .each() cycle

    0 讨论(0)
提交回复
热议问题