如何获得jQuery可排序列表顺序?

凯维纳布拉汉姆

我正在使用jQuery Sortable来允许用户在页面上拖放元素。当用户拖动div时,我需要获取列表的更新顺序并将其传递给后端。到目前为止,我已经尝试过:

  $( function() {
    $( "#sortable" ).sortable({
        axis: 'y',
        stop: function (event, ui) {
            var data = $(this).sortable('serialize');
            alert(data);
            $.ajax({
                    data: oData,
                type: 'POST',
                url: '/url/here'
            });
    }
    });
    $( "#sortable" ).disableSelection();
  } );

但这会使动画确实不流畅,也不会警告任何数据。每当用户拖放div时,如何获取位置列表?

JSFIDDLE

亚斯

refreshPositions()您可以使用一个函数来返回表示可排序项目的对象。然后,您可以通过.children()对该对象进行调用来获取更新的子级列表

将位置保存到stop事件中的变量中,该事件在完成排序后触发。

我已经更新了您的功能,使其包含以下stop事件:

$("#sortable").sortable({
  stop: function(ev, ui) {
    //Get the updated positions by calling refreshPositions and then .children on the resulting object.
    var children = $('#sortable').sortable('refreshPositions').children();
    console.log('Positions: ');
    //Loopp through each item in the children array and print out the text.
    $.each(children, function() {
        console.log($(this).text().trim());
    });
  }
});

更新的小提琴

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章