我使用treetable中插件绑定JSON来treetable中JS: http://ludo.cubicphuse.nl/jquery-treetable/#examples无法使用knockout.js
<table id="example-basic">
<thead>
<tr>
<th>Name</th> <th>Status</th> <th>id</th>
</tr>
</thead>
<tbody data-bind="foreach: TreeView">
<tr data-bind="attr: { 'data-tt-id': id ,'data-tt-parent-id': Parentid}">
<td data-bind="text: Name"></td>
<td data-bind="text: Status"></td>
<td data-bind="text: id"></td>
</tr>
</tbody>
</table>
<button type="button" name="test" onclick="test()"></button>
下面的作品,如果我硬编码的是,结果显示为一个很好的格式化的TreeView。
ko.applyBindings({ TreeView: [{ "id": "1", "Parentid": null, "Name": "test1", "Status": "OK" }, { "id": "2", "Parentid": 1, "Name": "test2", "Status": "OK" }] }
但是,我从服务器获取的值(放在“STR”的变量从服务器获取的值),并做如下的结合:
str = '[{ "id": "1", "Parentid": null, "Name": "parent1", "Status": "OK" }, { "id": "2", "Parentid": 1, "Name": "child1", "Status": "OK" }]',
json = JSON.stringify(eval("(" + str + ")")),
ko.applyBindings({ TreeView: json})
function reload() {
//ko.applyBindings({ TreeView: {} });
str = '[{ "id": "1", "Parentid": null, "Name": "parent1", "Status": "OK" }]'
json = JSON.parse(str),
ko.applyBindings({ TreeView: json})
我得到以下错误:
错误:无法解析绑定。
消息:ReferenceError:'id'未定义;
绑定值:attr: { 'data-tt-id': id ,'data-tt-parent-id': Parentid}
有人可以请帮忙。谢谢! 返回的Json对象必须转换为字符串类型,然后进行解析。这解决了上述问题
新问题: 我只是玩耍着这个可重复使用的插件。我希望在按钮点击时重新加载服务器上的数据(或者每5秒调用一次ajax)。数据被复制。
你是否能够找出数据重复问题? – halivingston
不是。问题依然存在。 –