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

时间:2013-04-18 12:06:33

标签: jquery jquery-ui jquery-ui-sortable

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

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

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

解释我的要求。

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

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

我错过了什么吗?

非常感谢任何帮助。提前致谢

2 个答案:

答案 0 :(得分:15)

试试这个:

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

答案 1 :(得分:1)

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

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

<强>演示 Here's the fiddle !

相关问题