在排序更改事件期间更新排序顺序- jQuery UI



我希望list元素的值是排序事件中排序位置的索引。

此值应在排序更改事件期间自动更新。

        <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>

我创造了一个小提琴http://jsfiddle.net/jagan2explore/4mcpq/

解释我的要求。

如果我将第一个元素移动到第5个位置,所有其他元素的值都被正确更新。如果我将第5移到1'st,则值相应更新。

假设我将一个列表元素从1移动到5 &从5到2而不离开(在单个排序事件期间),值不会相应地更新。

我错过了什么吗?

任何帮助都将非常感激。提前感谢

试试这个:

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);
        }
    }
},

演示:jsfiddle

我用另一种方法更新了你的小提琴,它似乎如预期的那样工作:

            $(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'
            });
        });

小提琴来了!

最新更新