第一周在这里使用 KnockoutJS 并寻找一种简单的方法来从其动态添加的父数组中删除子数组。访问http://jsfiddle.net/hotdiggity/HC9wU/以获取以下工作示例:
function Parent(name, children) {
var self = this;
//self.name = ko.observable(name);
self.children = ko.observableArray(children);
self.addChild = function () {
self.children.push(new Child(""));
}
self.removeParent = function (parent) {
vm.removeParent(self);
};
}
function Child(name) {
var self = this;
//self.name = ko.observable(name);
self.removeChild = function (child) {
vm.removeChild(self);
// this.child.remove(child);
};
}
function ParentChildViewModel() {
var self = this;
self.parents = ko.observableArray([]);
self.addParent = function () {
self.parents.push(new Parent("", []));
};
self.removeParent = function (parent) {
self.parents.remove(parent);
}
// remove child from the children list somehow
self.removeChild = function (child) {
self.children.remove(child);
}
};
var vm = new ParentChildViewModel();
ko.applyBindings(vm);