代码之家  ›  专栏  ›  技术社区  ›  Amir

jQuery UI可排序,如何确定更新事件中的当前位置和新位置?

  •  42
  • Amir  · 技术社区  · 16 年前

    <ul id="sortableList">
         <li>item 1</li>
         <li>item 2</li>
         <li>item 3</li>
    </ul>
    

    我已经接通了电话 update: function(event, ui) { } 但我不确定如何获得元素的原始和新位置。如果我将项目3移动到项目1上方,我希望原始位置为 (以0为基础的索引)和要删除的项目3的新位置 0 .

    6 回复  |  直到 11 年前
        1
  •  96
  •   Rush Frisby    13 年前
    $('#sortable').sortable({
        start: function(e, ui) {
            // creates a temporary attribute on the element with the old index
            $(this).attr('data-previndex', ui.item.index());
        },
        update: function(e, ui) {
            // gets the new and old index then removes the temporary attribute
            var newIndex = ui.item.index();
            var oldIndex = $(this).attr('data-previndex');
            $(this).removeAttr('data-previndex');
        }
    });
    
        2
  •  19
  •   kameny    13 年前

    调用更新函数时,ui.item.sortable尚未更新,但ui元素已在视觉上移动。
    这允许您在更新功能中获取旧位置和新位置。

       $('#sortable').sortable({    
            update: function(e, ui) {
                // ui.item.sortable is the model but it is not updated until after update
                var oldIndex = ui.item.sortable.index;
    
                // new Index because the ui.item is the node and the visual element has been reordered
                var newIndex = ui.item.index();
            }    
    });
    
        3
  •  10
  •   Frankie    16 年前

    您有几种可能检查旧位置和新位置。我会将它们放入数组中。

    $('#sortable').sortable({
        start: function(e, ui) {
            // puts the old positions into array before sorting
            var old_position = $(this).sortable('toArray');
        },
        update: function(event, ui) {
            // grabs the new positions now that we've finished sorting
            var new_position = $(this).sortable('toArray');
        }
    });
    

    然后你就可以很容易地提取你需要的东西。

        4
  •  5
  •   jondavidjohn    14 年前

    我在寻找同一问题的答案。根据Frankie的贡献,我能够获得开始和结束的“订单”。我在使用变量作用域时遇到了一个问题,所以我只是将它们存储为.data()而不是本地变量:

    $(this).data("old_position",$(this).sortable("toArray"))
    

    $(this).data("new_position",$(this).sortable("toArray"))
    

    现在可以这样调用它(从更新/结束函数):

    console.log($(this).data("old_position"))
    console.log($(this).data("new_position"))
    

        5
  •  5
  •   nicmwenda    11 年前

    这对我有用

    $('#sortable').sortable({
    start: function(e, ui) {
        // puts the old positions into array before sorting
        var old_position = ui.item.index();
    },
    update: function(event, ui) {
        // grabs the new positions now that we've finished sorting
        var new_position = ui.item.index();
    }
    });
    
        6
  •  1
  •   lonestar    10 年前

    这对我有用,

    $('#app').sortable({
        handle: '.handle',
    
        start: function(evt, ui){
            $(ui.item).data('old-ndex' , ui.item.index());
        },
    
        update: function(evt, ui) {
            var old_index = $(ui.item).data('old-ndex');
            var new_index = ui.item.index();
    
            alert('old_index -'+old_index+' new index -'+new_index);
    
        }
    });