Updating sort order during sort change event - jQuery UI

こ雲淡風輕ζ 提交于 2021-02-07 18:53:33

问题


I want the value of the list element to be the index of the sorted position during sort event.

This value should update automatically during sort change event.

        <script type="text/javascript">
        $(function() {
            $('#sortable').sortable({
                start : function(event, ui) {
                    var start_pos = ui.item.index();
                    ui.item.data('start_pos', start_pos);
                },
                change : function(event, ui) {
                    var start_pos = ui.item.data('start_pos');
                    var index = ui.placeholder.index();

                    if (start_pos < index) {
                        $('#sortable li:nth-child(' + index + ')').html(index-2);
                    } else {
                        $('#sortable li:eq(' + (index + 1) + ')').html(index + 1);
                    }
                },
                update : function(event, ui) {
                    var index = ui.item.index();
                    $('#sortable li:nth-child(' + (index + 1) + ')').html(index);

                },
                axis : 'y'
            });
        });


    </script>

I created a fiddle http://jsfiddle.net/jagan2explore/4mcpq/

to explain my requirement.

If i move 1'st element to 5th position all other elements values are updated rightly, If i move 5th to 1'st the value updates accordingly.

Suppose if i move a list element from 1 to 5 & from 5 to 2 without leaving (during single sort event ), the values are not updated accordingly.

Am i missing something??

Any help would be greatly appreciated. Thanks in advance


回答1:


Try this:

update : function(event, ui) {
    var index = ui.item.index();
    var start_pos = ui.item.data('start_pos');

    //update the html of the moved item to the current index
    $('#sortable li:nth-child(' + (index + 1) + ')').html(index);

    if (start_pos < index) {
        //update the items before the re-ordered item
        for(var i=index; i > 0; i--){
            $('#sortable li:nth-child(' + i + ')').html(i - 1);
        }
    }else {
        //update the items after the re-ordered item
        for(var i=index+2;i <= $("#sortable li").length; i++){
            $('#sortable li:nth-child(' + i + ')').html(i-1);
        }
    }
},

Demo: jsfiddle




回答2:


I updated your fiddle with another approach, it seems to work as expected:

            $(function() {
            $('#sortable').sortable({
                start : function(event, ui) {
                    var start_pos = ui.item.index();
                    ui.item.data('start_pos', start_pos);
                },
                change : function(event, ui) {
                    var start_pos = ui.item.data('start_pos');
                    var index = ui.placeholder.index();


                    cIndex = (start_pos < index) ? index-2 : index-1;
                    ui.placeholder.prevAll('li').each(function(){
                        $this = $(this);
                        if($this.is(ui.item)) {return;}
                        $this.html(cIndex);
                        cIndex--;
                    });

                    cIndex = (start_pos < index) ? index : index+1;
                    ui.placeholder.nextAll('li').each(function(){
                        $this = $(this);
                        if($this.is(ui.item)) return;
                        $this.html(cIndex)
                        cIndex++;
                    });

                    ui.item.html((start_pos < index) ? index-1 : index);
                },
                axis : 'y'
            });
        });

Demo Here's the fiddle !



来源:https://stackoverflow.com/questions/16082519/updating-sort-order-during-sort-change-event-jquery-ui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!