我正在玩淘汰赛,并试图通过和html表显示填充的数组变量。
问题在于我不知道如何显示" last_name"变量数组中的属性如下所示。
JSON FILE + HTML FILE:
//JSON FILE
$(function()
{
console.log('Ready');
ko.applyBindings(new myvm());
}
function myvm()
{
var self = this;
//cust contains the data mentioned at the bottom
self.customers = cust;
}
//HTML FILE
<table class= "table" id="kocustomertable" border= "1">
<tr>
<th>Last name</th>
</tr>
<tbody data-bind = 'customers'>
<tr>
<td data-bind = 'text: last_name'></td>
</tr>
</tbody>
</table>
[
{"id":1,"first_name":"Tracey","last_name":"Jansson","email":"tjansson0@discuz.net","gender":"Female","ip_address":"167.88.183.95","birthdate":"1999-08-25T17:24:23Z","website":"http://hello.com","city":"Medellín","credits":7471}
]
答案 0 :(得分:1)
您的data-bind
上的<tbody>
似乎缺少绑定。
您已在HTML中引用了您的视图模型customers
属性,但您还没有告诉淘汰赛如何将其绑定到视图。如果添加foreach
绑定,您应该会看到每个客户都有一个表行。例如,您可以将开头<tbody>
替换为:
<tbody data-bind='foreach: customers'>
希望这会有所帮助。有关详细信息,请查看knockout documentation on foreach。