2

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

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

小提琴的例子。

4

1 回答 1

2

您的视图模型中有很多错误。

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()));
于 2013-11-08T10:34:01.343 回答