jQuery UI sortable: determining in what order the items are

后端 未结 5 922
深忆病人
深忆病人 2020-12-13 20:22

Here is an interesting use of JavaScript: reordering items with drag and drop. The implementation itself in my page works fine, but is there a way to determine in which orde

5条回答
  •  既然无缘
    2020-12-13 20:28

    HTML

    • Item 1
    • Item 2
    • Item 3
    • Item 4
    • Item 5

    JQUERY

     $("#sortable").sortable(
                {
                    update: function () {
                    var strItems = "";
    
                    $("#sortable").children().each(function (i) {
                        var li = $(this);
                        strItems += li.attr("id") + ':' + i + ',';
                    });
    
                    updateSortOrderJS(strItems); <--- do something with this data
    
                    }
                });
    

    strItems will look like (new-item-order:item-id)

    0,49:1,365:2,50:3,364:4,366:5,39:6

    then you can parse it into an update functions something like

    List eachTask = new List(itemsList.Trim().Split(new char[] { ',' }));

    then

    String[] item = i.Split(new Char[] { ':' });

提交回复
热议问题