gpt4 book ai didi

javascript - 从 knockoutJS 中的 observableArray 中删除自身

转载 作者:搜寻专家 更新时间:2023-11-01 04:26:42 24 4
gpt4 key购买 nike

我正在使用 knockoutjs 创建分区 TreeView 。每个节点旁边将是三个按钮: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/但是它似乎在那里根本不起作用。如果你们做不到,我会附上托管它的网站链接,以便您仔细查看。

最佳答案

我采纳了你的想法并做了一个 working fiddle ,其行为完全符合您的描述。对不起,我不想尝试解决你的问题。它有很多与您的问题没有直接关系的东西,而且这个解决方案足够通用,其他人应该能够使用它。如果您需要帮助调整它,请告诉我。

需要注意的一件事是克隆功能。您的复制功能不深,会导致多个节点指向同一个对象。如果您要更新节点值,它将传播到它的克隆。 Knockout 使用 ko.toJS 提供了一个方便的深度复制 + 展开可观察对象。 super 有用。

JS:

var Node = function(name, children) {
var self = this;
self.name = ko.observable(name || 'NewNode');
self.children = ko.observableArray(
ko.utils.arrayMap(children || [], function(i) {
return new Node(i.name, i.children);
}));
self.newChild = function() {
self.children.push(new Node());
};
self.removeNode = function(node) {
self.children.remove(node);
};
self.copyNode = function(node) {
var cloneNode = ko.toJS(node);
self.children.push(new Node(cloneNode.name, cloneNode.children));
};
};

//Example data removed for brevity, see fiddle
ko.applyBindings(new Node(data.name, data.children));​

HTML:

<button data-bind="click: newChild">NewNode</button>
<ul data-bind="template: { name: 'treeTemplate', foreach: children}">
</ul>

<script id="treeTemplate" type="text/html">
<li>
<input data-bind="value: name" />
<button data-bind="click: newChild">New Child</button>
<button data-bind="click: $parent.removeNode">Remove Node</button>
<button data-bind="click: $parent.copyNode">Copy Node</button>
<ul data-bind="template: { name: 'treeTemplate', foreach: children}"></ul>
</li>
</script>

关于javascript - 从 knockoutJS 中的 observableArray 中删除自身,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11463970/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com