knockoutjs를 사용하여 디비전 트리 뷰를 만듭니다. 각 노드 옆에는 세 개의 버튼이 있습니다 : 1) 새로운 자식 (2 옆에있는 노드에 적용) 제거 (옆에있는 노드를 제거합니다.) 3) 복사 : 노드와 노드를 모두 복사하고 새로운 노드를 만듭니다. 노드를 부모 아래에 두십시오.knockoutJS의 observableArray에서 셀프 제거
나는 새로운 버튼을 가지고 있고, 이제 나는 제거 버튼을 만들고있다. 나는 제대로 작동하지 않는 것 같아서 유용하게 사용하는 대신 전체 페이지를 새로 고침 만합니다. 여기 코드는 다음과 같습니다
보기 :
<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/입니다. 그러나 전혀 작동하지 않는 것 같습니다. 너희들이 그것을 얻을 수 없다면, 나는 당신이 그것을 잘 볼 수 있도록 호스트되는 사이트에 대한 링크를 포함시킬 것이다.
또한 필자는 일부 지점에서 제거 작업을 수행했지만 두 단계 만 추가한다고 덧붙이고 싶습니다. 더 깊은 것은 효과가 없었습니다.거기에서 일한 코드를 기억하면 알려 드리겠습니다. –
jsfiddle을 만들 수 있습니까? – HashCoder
나는 지금 일하러 가야하지만, 내 휴식 시간에 나는 하나를 올려 놓을 것이다. –