我正在尝试为“项目计划应用程序”创建嵌套(分支)的淘汰模型/视图。数据结构应如下:
- 项目(projectid,projectname) | ---子项目(子项,子名称)
目前,我的淘汰模型/ viewmodel看起来像:
//----------------------------
// model "project"
//----------------------------
var project = function ( projectid,
projectname )
{
var self = this;
this.projectid = ko.observable(projectid);
this.projectname = ko.observable(projectname);
}
//------------------------------
// model subproject
//------------------------------
var subproject = function ( subid,
subname )
{
var self = this;
this.subid = ko.observable(subid);
this.subname = ko.observable(subname);
}
//----------------------------
// viewmodel project
//----------------------------
var viewmodel_project = function ()
{
//--------------------------
// projects data/view
//--------------------------
this.projects = ko.observableArray([]);
var projectnumber = 0;
// Add new project (open modal)
this.addproject = function ()
{
projectnumber++;
self.projects.push(new project(projectnumber,"New Project XY"));
};
//--------------------------
// subprojects data/view
//--------------------------
this.subprojects = ko.observableArray([]);
this.selectedsubproject = ko.observable();
var subcounter = 0;
this.addsubproject = function (project)
{
subcounter++;
self.subprojects.push(new subproject(
project.projectid(),
project.projectname() + subcounter));
};
}
在HTML中,我想在tbody表中显示嵌套模型, 我的意思是在“项目”和“子项目”的嵌套循环中:
<table>
<tr>
<th>Mainprojects and subprojects</th>
<th></th>
<th href="#" data-bind="click: addproject">Add Project</th>
</tr>
<tbody >
<!-- ko foreach: projects -->
<tr>
<td data-bind="text: projectid"></td>
<td data-bind="text: projectname"></td>
<td class="add" href="#" data-bind="click: addsubproject">Add Subproject</td>
</tr>
<!-- ko foreach: subprojects -->
<tr>
<td></td>
<td data-bind="text: subid"></td>
<td data-bind="text: subname"></td>
</tr>
<!-- /ko -->
<!-- /ko -->
</tbody>
</table>
我所能做的就是,我可以添加项目,我可以添加子项目,但我不会让它们嵌套或分支。我不知道如何创建像这样的模型:
项目(项目,项目, 项目名, “subprojects array”)
示例(数据结构应如何显示,无编码):
项目:
项目100(
Project ID = 100,
Project Name = "Project 100"
subprojects ( [ Subproject ID = 100.10,
Subproject Name = "Sub Name 10" ],
[ Subproject ID = 100.11,
Subproject Name = "Sub Name 11" ] )
) ,
项目200(
Project ID = 200,
Project Name = "Project 200"
subprojects ( [ Subproject ID = 200.10,
Subproject Name = "Sub Name 10" ],
[ Subproject ID = 200.11,
Subproject Name = "Sub Name 11" ] )
)
任何想法如何构建淘汰模型/ viewmodel将是 非常感谢:-)圣诞快乐
史蒂夫