我试图根据ko.observableArray返回的列未预先确定的位置输出数据表.
来自我的observableArray的项目样本self.userData()[0]将是:
Object {
RowNum: 1,
ID: "123",
Surname: "Bloggs",
Forename: "Joe",
Address line 1: "1 Park Lane"
}
Run Code Online (Sandbox Code Playgroud)
根据用户选择输出的内容,这些列每次都不同.
我希望输出中的列标题由数组中的内容确定,所以我想要的输出是:
<table>
<thead>
<tr>
<th>RowNum</th>
<th>ID</th>
<th>Surname</th>
<th>Forename</th>
<th>Address line 1</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>123</td>
<td>Bloggs</td>
<td>Joe</td>
<td>1 Park Lane</td>
</tr>
<!-- repeated for each row -->
</tbody>
</table>
Run Code Online (Sandbox Code Playgroud)
我知道我可以foreach用来重复行和列,但我不确定如何根据我的内容动态引用它observableArray.
目前我有这个基本结构:
<table>
<thead>
<tr data-bind="foreach: userData [property name] ">
<th>
<span data-bind="text: [property name]"></span>
</th>
</tr>
</thead>
<tbody data-bind="foreach: userData"> …Run Code Online (Sandbox Code Playgroud)