Knockout可排序 - 分解元素

时间:2013-11-08 10:28:39

标签: javascript jquery knockout.js mapping jquery-ui-sortable

当我在“foreach”绑定中使用“sortable”时,移动元素会使它们消失。更重要的是,阵列位置正在更新!

<div id="main">
<div data-bind="foreach: tasks">
    <div class="item" data-bind="sortable: $data">
        <label data-bind="text: name"></label>
    </div>
</div>
</div>

Fiddle example.

1 个答案:

答案 0 :(得分:2)

您的viewmodel中有很多错误。

HTML:

<div id="main">
    <div data-bind="foreach: tasks">
        <div class="item" data-bind="sortable: tasks">
            <label data-bind="text: name"></label>
        </div>
    </div>
</div>

视图模型:

var Task = function(name) {
    this.name = name;
}

var InternalTasks = function(tasks) {
    this.tasks = ko.observableArray(tasks); 
}

var ViewModel = function() {
    var tasks1 = new InternalTasks([
        new Task("Get dog food"),
        new Task("Mow lawn"),
        new Task("Fix car"),
        new Task("Fix fence"),
        new Task("Walk dog"),
        new Task("Read book")
    ]);
    var tasks2 = new InternalTasks([
        new Task("aa"),
        new Task("bb"),
        new Task("cc"),
        new Task("dd"),
        new Task("ee"),
        new Task("ff")
    ]);
    this.tasks = ko.observableArray([tasks1, tasks2]);
};

ko.applyBindings(ko.mapping.fromJS(new ViewModel()));