Javascript &引用;加上;敲除Js中多维数组的数据绑定
我有一个如下所示的数组Javascript &引用;加上;敲除Js中多维数组的数据绑定,javascript,arrays,knockout.js,Javascript,Arrays,Knockout.js,我有一个如下所示的数组 var initData = [ new Order({ orderId: "183175", name: "Columbus Africentric", production: [{ pType: "Art Time", by: "MJ" }, { pType: "Front Pocket", by: "
var initData = [
new Order({
orderId: "183175",
name: "Columbus Africentric",
production: [{
pType: "Art Time",
by: "MJ"
}, {
pType: "Front Pocket",
by: "WB"
}]
}),
new Order({
orderId: "198675",
name: "Stanford High",
production: [{
pType: "Art Time",
by: "MJ"
}, {
pType: "Full Back",
by: "WB"
}]
})
]
<tbody data-bind="foreach:orders">
<tr>
<td>
<label class="read" data-bind="text:orderId, visible:true" />
</td>
<td>
<label class="read" data-bind="text:name, visible:!$root.isItemEditing($data)" />
</td>
<td>
<td class="tools">
<div data-bind="if: production"><button data-bind="click: $root.toggleProductionMode">Production</button>
</div>
</td>
</tr>
<tr data-bind="visible: showProductionOrder, with: production">
<td colspan="5">
<h3>Production Summary</h3>
<table class="ko-grid">
<thead>
<tr>
<th>Type</th>
<th>By</th>
</tr>
</thead>
<tbody
<tr>
<td>
<label class="read" data-bind="text:pType, visible:!$root.isItemEditing($data)" />
</td>
<td>
<label class="read" data-bind="text:by, visible:!$root.isItemEditing($data)" />
</td>
</tr>
</tbody>
</table>
</tr>
我正在尝试使用绑定来在单击项目时仅显示订单上的额外信息。因此,我有一个foreach用于订单,它在表中显示orderId和名称,还有一个单击按钮,然后应该显示所选订单的所有生产项目。类似于下面的内容
var initData = [
new Order({
orderId: "183175",
name: "Columbus Africentric",
production: [{
pType: "Art Time",
by: "MJ"
}, {
pType: "Front Pocket",
by: "WB"
}]
}),
new Order({
orderId: "198675",
name: "Stanford High",
production: [{
pType: "Art Time",
by: "MJ"
}, {
pType: "Full Back",
by: "WB"
}]
})
]
<tbody data-bind="foreach:orders">
<tr>
<td>
<label class="read" data-bind="text:orderId, visible:true" />
</td>
<td>
<label class="read" data-bind="text:name, visible:!$root.isItemEditing($data)" />
</td>
<td>
<td class="tools">
<div data-bind="if: production"><button data-bind="click: $root.toggleProductionMode">Production</button>
</div>
</td>
</tr>
<tr data-bind="visible: showProductionOrder, with: production">
<td colspan="5">
<h3>Production Summary</h3>
<table class="ko-grid">
<thead>
<tr>
<th>Type</th>
<th>By</th>
</tr>
</thead>
<tbody
<tr>
<td>
<label class="read" data-bind="text:pType, visible:!$root.isItemEditing($data)" />
</td>
<td>
<label class="read" data-bind="text:by, visible:!$root.isItemEditing($data)" />
</td>
</tr>
</tbody>
</table>
</tr>
您不必在主表中创建新表。对于子集合,您必须使用“ko foreach:production”作为html注释,然后添加tr标记以显示生产项。看看这个例子
//HTML
学生证
学名
//淘汰码
函数StudentViewModel(){
var self=这个;
学生自我评价=[
{StudentID:“1”,StudentName:“Ali”,
课程:[{CourseID:“100”,CourseName:“数学”},{CourseID:“102”,CourseName:“物理”}]
},
{StudentID:“2”,StudentName:“Isa”,
课程:[{CourseID:“103”,CourseName:“化学”},{CourseID:“104”,CourseName:“社会研究”},
{StudentID:“3”,StudentName:“Zoya”,
课程:[{CourseID:“100”,CourseName:“数学”},{CourseID:“106”,CourseName:“统计”},
];
}
应用绑定(new StudentViewModel());
我在初始化变量时仍然遇到问题。我遇到了javascript错误“无法解析绑定,未定义pType”,我知道这一定是我初始化对象的方式。你能看一下这里吗->谢谢你的回答,在将新对象推到数组中时,有什么具体的事情需要做吗?你必须使学生数组成为可观察的。看这里