小编Emm*_*lay的帖子

jQuery-UI 可排序 - 更新后同步数组(模型)

假设我有一个包含数据的数组,它可能来自 Ajax(但这里不需要这样做)。

使用该数组生成 UL 元素的内容,并使用 jQuery-UI 使该 UL 可排序

在客户端对其进行排序之后,我想保持数组的顺序与 UL 同步。

有没有一种优雅的方式来做到这一点?

var locations = [
  {name: 'point 0', location: [50.8674162,4.3772933]},
  {name: 'point 1', location: [50.8135113,4.3247394]},
  {name: 'point 2', location: [50.8771732,4.3544551]},
  {name: 'point 3', location: [50.8460485,4.3664706]}
];

function generateUl() {
  var content = '';
  for(var i in locations) {
    content += '<li>'+ locations[i].name +' ('+ locations[i].location.toString() +')</li>';
  }
  $('#points').html(content);

}

$(document).ready(function() {
  generateUl();
  $('#points').sortable({
    update: function(event, ui) {
      //$('#points li').each( function(e) {
      //});
      
      // so, I would like to …
Run Code Online (Sandbox Code Playgroud)

javascript arrays jquery jquery-ui

3
推荐指数
1
解决办法
1281
查看次数

标签 统计

arrays ×1

javascript ×1

jquery ×1

jquery-ui ×1