我正在使用 knockoutjs 创建部门的树视图。每个节点旁边将有三个按钮:1)新建子节点(应用于它旁边的节点 2)删除(这会删除它旁边的节点,以及 3)复制,它复制节点及其所有子节点并创建一个新的父节点下的节点。
我已经按下新建按钮,现在我正在处理删除按钮。我似乎无法让它工作,而不是做任何有用的事情,它只是刷新了整个页面。这是代码:
看法:
<h2>Skill & Weight Divisions</h2>
<span data-bind="text: tournamentname"></span><button data-bind="click: addDivision"><img src="new.png"/></button>
<ul data-bind="template: { name: 'divisionTemplate', foreach: divisions }"></ul>
模板:
<script id="divisionTemplate" type="text/html">
<li data-bind="style: {'background-color':color}">
<input data-bind="value: name"/><button data-bind="click: addDivision"><img src="new.png"/></button><button data-bind="click: $parent.removeDivision"><img src="remove.png"/></button><button data-bind="click: $parent.copyDivision"><img src="copy.png"/></button>
<ul data-bind="template: { 'if': children, name: 'divisionTemplate', foreach: children }"></ul>
</li>
</script>
查看模型和适当的辅助函数:
function division(id, name, filter, children) {
this.id = ko.observable(id);
this.name = ko.observable(name);
this.filter = ko.observable(filter)
if(children){
this.children = ko.observableArray(children);
}else{
this.children = ko.observableArray();
}
this.addDivision = function(){
this.children.push(new division("", "", ""));
}
this.removeDivision = function(division){
this.children.remove(division);
}
this.copyDivision = function(division){
this.children.push(division);
}
this.color = randColor();
};
function tournamentViewModel(){
var self= this;
self.tournamentname = ko.observable('NO NAME YET');
self.districts = ko.observableArray([new district('Provo',1),new district('Salt Lake City',2),new district('St. George',3)]);
self.district = ko.observable(self.districts()[0]);
self.regions = ko.observableArray([new region('Utah',1),new region('Idaho',2)]);
self.region = ko.observable(self.regions()[0]);
self.location = ko.observable('WHEREVER YOU WANT');
self.eventdate = ko.observable('');
self.startTime = ko.observable('');
self.image = ko.observable();
self.flyer = ko.computed(function(){
var flyerHTML = '<span style="text-align:center;padding:10px;"><h1>'+self.tournamentname()+'</h1><img src="'+self.image()+'"/><br/>';
flyerHTML += 'District: ' + self.district().districtName + ' Region: ' + self.region().regionName+'<br><br>';
flyerHTML += '<h2>WHEN: '+self.eventdate()+' '+self.startTime()+'</h2>';
flyerHTML += '<h2>WHERE: '+self.location()+'</h2>';
flyerHTML += '<img src="http://maps.googleapis.com/maps/api/staticmap?center='+encodeURI(self.location())+'&zoom=12&size=200x200&markers=color:blue%7Clabel:S%7C'+encodeURI(self.location())+'&maptype=roadmap&sensor=false"/>';
return flyerHTML;
}, self);
self.clearImage = function(){
self.image('');
}
self.tournamentID = ko.computed(function(){return 't_'+self.district()+'_'+self.region()+'_'+self.eventdate()}, self);
self.pricingStructures = ko.observableArray([new pricingStructure(3,2.99), new pricingStructure(1,1.99)]);
self.removePricingStructure = function(pricingStructure){
self.pricingStructures.remove(pricingStructure);
}
self.addPricingStructure = function(){
self.pricingStructures.push(new pricingStructure("", ""));
}
self.promoCodes = ko.observableArray();
self.promoTypes = ['%','$'];
self.removePromoCode = function(promoCode){
self.promoCodes.remove(promoCode);
}
self.addPromoCode = function(){
self.promoCodes.push(new promoCode("", ""));
}
self.divisions = ko.observableArray([new division(1, "Men","",[new division(2,"Gi"), new division(3,"No-Gi")])]);
self.addDivision = function(){
self.divisions.push(new division("", "", ""));
}
}
ko.applyBindings(new tournamentViewModel());
在这一切中我的主要问题是:有没有办法访问对象的父数组以便从数组中删除该对象?在此先感谢您的帮助!
编辑:这是一个 jsFiddle:http: //jsfiddle.net/eqY7Z/但是它似乎根本不起作用。如果你们不能让它继续下去,我会附上我的网站的链接,这样你们就可以好好看看它了。