当我在“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>
当我在“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>
您的视图模型中有很多错误。
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()));