【发布时间】:2014-10-07 05:00:25
【问题描述】:
嘿,我正在尝试从内部(嵌套)observableArray 中删除一个项目,如下所示,但删除按钮不起作用(对于内部 foreach 项目)。
http://jsfiddle.net/aDahT/1871/
html:
<h4>People</h4>
<ul data-bind="foreach: peoples">
<li>
Name at position <span data-bind="text: $index"> </span>:
<span data-bind="text: name"> </span>
<a href="#" data-bind="click: $parent.removePerson">Remove</a>
<ul data-bind="foreach:people">
<li>
<span data-bind="text: $data"></span>
<button data-bind="click: $parent.deletePerson">Remove</button>
</li>
</ul>
</li>
</ul>
<button data-bind="click: addPerson">Add</button>
脚本:
var Person = function(name, children){
var self = this;
self.name = name;
self.people = ko.observableArray(children);
self.deletePerson = function() {
alert(JSON.stringify(self));
self.people.remove(this);
}
}
function AppViewModel() {
var self = this;
self.peoples = ko.observableArray([
new Person( 'Bert', ['baa', 'bbb'] ),
new Person('Charles', ["caa", "cbb"] )
]);
self.addPerson = function() {
alert(this);
self.peoples.push(new Person( "New" ,["Daa", "Dbb"] ));
}.bind(this);
self.removePerson = function() {
self.peoples.remove(this);
}
}
ko.applyBindings(new AppViewModel());
有人可以帮忙吗? 先谢谢了。
【问题讨论】:
标签: knockout.js