0
以下爲對象的樣本結構:KnockoutJS添加,編輯,刪除
[{
MasterType: "mtype1",
Types: [{
TypeStage: "st1",
TypeDate: "12/02/2001",
SubTypes: [{
SubTypeData: "st1"
}, {
AnotherData: "ad1"
}]
}, {
TypeStage: "st3",
TypeDate: "15/02/2001",
SubTypes: [{
SubTypeData: "st3"
}, {
AnotherData: "ad3"
}]
}]
},
{
MasterType: "mtype2",
Types: [{
TypeStage: "st2",
TypeDate: "12/04/2001",
SubTypes: [{
SubTypeData: "st2"
}, {
AnotherData: "ad2"
}]
}]
}];
注:在頁面加載,對象是空的。
我需要顯示每個行的編輯/刪除的MasterType表。 我也需要一個「添加」按鈕,然後隱藏表格並顯示輸入字段以輸入新的MasterType的值。 保存新的MasterType後,我需要隱藏輸入字段並再次顯示錶格。
請幫
我也做了以下內容:
<table>
<tbody data-bind="foreach: MasterTypes">
<tr><td data-bind='text: MasterType'></td></tr>
</tbody>
</table>
<div >
<table>
<tbody data-bind="foreach: MasterTypes">
<tr>
<td>
<input data-bind="value: MasterType,valueUpdate: 'afterkeydown'" />
<div><a href='#' data-bind='click: $root.removeMasterType'>Delete</a></div>
</td>
<td>
<table>
<tbody data-bind="foreach: Types">
<tr>
<td><input data-bind='value: TypeStage' /></td>
<td><input data-bind='value: TypeDate' /></td>
<td><a href='#' data-bind='click: $root.removeType'>Delete</a></td>
<td>
<table>
<tbody data-bind="foreach: SubTypes">
<tr>
<td><input data-bind='value: Discharge' /></td>
<td><a href='#' data-bind='click: $root.removeSubType'>Delete</a></td>
</tr>
</tbody>
</table>
<a href='#' data-bind='click: $root.addSubType'>Add New Sub type</a>
</tr>
</tbody>
</table>
<a href='#' data-bind='click: $root.addType'>Add new Type</a>
</td>
</tr>
</tbody>
</table>
</div>
<p>
<button data-bind='click: addMasterType'>Add New MasterType</button>
<button data-bind='click: save, enable: MasterTypes().length > 0'>Save to JSON</button>
</p>
<textarea data-bind='value: lastSavedJson' rows='5' cols='60' disabled='disabled'> </textarea>
<script type="text/javascript">
var MasterTypesModel = function (mastertypes) {
var self = this;
self.tableVisible = ko.observable(true);
self.MasterTypes = ko.observableArray(ko.utils.arrayMap(mastertypes, function (mastertype) {
return { MasterType: mastertype.MasterType, Types: ko.observableArray(ko.utils.arrayMap(mastertype.Types, function (type) {
return { TypeStage: type.TypeStage, TypeDate: type.TypeDate, SubTypes: ko.observableArray(type.SubTypes) };
}))
};
}));
self.addMasterType = function() {
// self.tableVisible(false);
self.MasterTypes.push({
MasterType: "",
Types: ko.observableArray(
[{ TypeStage: "", TypeDate: "", SubTypes: ko.observableArray()}]
)
});
};
self.removeMasterType = function (mastertype) {
self.MasterTypes.remove(mastertype);
};
self.addType = function (mastertype) {
mastertype.Types.push({
TypeStage: "",
TypeDate: "",
SubTypes: ko.observableArray()
});
};
self.removeType = function (type) {
$.each(self.MasterTypes(), function() {
this.Types.remove(type)
})
};
self.addSubType = function (type) {
type.SubTypes.push({
Discharge: ""
});
};
self.removeSubType = function (subtype) {
$.each(self.MasterTypes(), function() {
$.each(this.Types(), function() {
this.SubTypes.remove(subtype)
})
})
};
self.save = function() {
self.lastSavedJson(JSON.stringify(ko.toJS(self.MasterTypes), null, 2));
$.ajax({
url: "/MyController/UpdateMasterType",
contentType: 'application/json',
type: 'POST',
data: ko.toJSON(self.MasterTypes),
success: function (data) {
self.lastSavedJson = ko.observable("")
alert('That is it!');
}
});
};
self.lastSavedJson = ko.observable("")
};
ko.applyBindings(new MasterTypesModel());
</script>
你還做了什麼? – Damien
請參閱編輯後的文章,看看我有什麼。我無法將其添加爲評論,因爲它超過了允許的字符數。 – user1918553
感謝您的時間每個人...我找到了答案....請找到最後得到答案的原文。 – user1918553