0
我需要一個像重複表格功能在我的web表單並需要存儲在JSON格式像這樣我的數據:淘汰賽嵌套模特屬性
[
{
"id": 1, "name": "T01", "title": "T01 form title", "totalPoints": "total of all points for sections below",
"sections":
[
{ "section": "First section", "point": 4 },
{ "section": "Second section", "point": 5 }
]
},
{
"id": 2, "name": "T02", "title": "T02 form title", "totalPoints": "total of all points for sections below",
"sections":
[
{ "section": "First section", "point": 4 },
{ "section": "Second section", "point": 5 }
]
}
]
我使用基因敲除和我實現了下面的結構的頂層,但是與嵌套的部分掙扎。
這是我嘗試構建我的模型,請告知使用什麼選項,或者如果這不正確,請告知正確的選擇:
function Form(data)
{
this.Id = ko.observable(data.Id);
this.Name = ko.observable(data.Name);
this.Title = ko.observable(data.Title);
this.Total = ko.observable(data.Total);
// Option 1, like an array
this.Sections = ko.observableArray([
{
Section: data.Section,
Point: data.Total
}
]);
// Option 2, like a function
function Sections(data) {
this.Section = ko.observable(data.Section),
this.Point = ko.observable(data.Point)
}
}
後來我把這個數據作爲模型來觀察的陣列狀這一點,我又可以推頂級水平,但不能嵌套的特性:
self.addForm = function() {
self.forms.push(
new Form({
Id: this.id(),
Name: this.name(),
Title: this.title(),
Total: function() // TODO
// Sections nested properties implementation
})
);
self.name("");
};
嗨,謝謝你的回覆。你能否告訴我如果我需要在UI中管理部分要點? –
你讓它成爲'ko.observable'。在'total'計算中,使用'section.points()'來獲取值。在你的用戶界面中,你可以使用'data-bind =「value:points」'來綁定點 – user3297291