【问题标题】:Jquery Sortable List with getJSON method使用 getJSON 方法的 Jquery 可排序列表
【发布时间】:2014-01-26 20:19:09
【问题描述】:

我正在编写一个使用 getJSON 将列表项添加到 DOM 的教程。还需要有 JqueryUI 可排序插件来对列表进行排序。由于某种我不知道的原因,插件不起作用。我在这里想念什么?可排序函数是否应该在 getJSON 回调中?任何建议都会很棒。

这是我目前的代码:

$(function () {
 $('body h1').append('My Todo List');

 $.getJSON('todo.json', function(data) {

var html = '<ul id="sortable" class="ui-sortable">';

$.each(data, function(index) {

    var todo = data[index];
    if (todo.done === false) {
        todo.done = (" ")

    } else {
        todo.done = ("(DONE)")
    }
    html += '<li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>' + todo.who + " needs to " + todo.task + " by " + todo.dueDate + " " + todo.done + '</li>';
});
    html += '</ul>';
   $('body #container').append(html);
});



});

HTML 文件:

<!DOCTYPE html>
<html>
<head>
    <title>Jquery ToDo List</title>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
    <script src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    <script src="todo.js"></script>
    <script>
        $(function () {
            $("#sortable").sortable("refresh");
            $("#sortable").disableSelection("refresh");
        });
    </script>
    <style>
        #sortable { list-style-type: none; margin: 0; padding: 0; width: 60%; }
        #sortable li { margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; font-size: 14px; height: 18px; }
        #sortable li span { position: absolute; margin-left: -1.3em; }
    </style>
</head>
<body>
<h1></h1>
<div id="container">

</div>



</body>
</html>

JSON

[
{"task":"get milk","who":"Scott","dueDate":"2013-05-19","done":false},
{"task":"get broccoli","who":"Elisabeth","dueDate":"2013-05-21","done":false},
{"task":"get garlic","who":"Trish","dueDate":"2013-05-30","done":false},
{"task":"get eggs","who":"Josh","dueDate":"2013-05-15","done":true}

]

【问题讨论】:

  • 查看更新后的答案与工作的 jsbin 演示............

标签: javascript jquery-ui getjson


【解决方案1】:

您必须在附加数据后调用sortable。在您的$.getJSON 回调中再次调用sortable,如下所示。只有当 dom 准备好时元素存在于DOM 中时,jquery 才会连接sortable。您正在动态添加元素,因此您必须在将元素添加到 DOM 后再次调用 sortable

 $.getJSON('todo.json', function(data) {

var html = '<ul id="sortable" class="ui-sortable">';

$.each(data, function(index) {

    var todo = data[index];
    if (todo.done === false) {
        todo.done = (" ")

    } else {
        todo.done = ("(DONE)")
    }
    html += '<li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>' + todo.who + " needs to " + todo.task + " by " + todo.dueDate + " " + todo.done + '</li>';
});
    html += '</ul>';
   $('body #container').append(html);
});

$( "#sortable" ).sortable();
$( "#sortable" ).disableSelection();

});

编辑

这里是垃圾箱http://jsbin.com/IPubElE/1/

demo 使用内存中的数据,但即使在回调方法中也应该可以正常工作。

【讨论】:

  • 是的,我已经尝试过这种方法,但这也不起作用。
  • 我已将 bin 添加到答案中,其中包含 sortable 的工作演示以及您提供的数据看看......
猜你喜欢
  • 1970-01-01
  • 2011-02-05
  • 1970-01-01
  • 1970-01-01
  • 2012-08-02
  • 1970-01-01
  • 1970-01-01
  • 2017-11-18
  • 1970-01-01
相关资源
最近更新 更多