0

I am using the TreeTable plugin: http://ludo.cubicphuse.nl/jquery-treetable/#examples

<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>

The below works if I HardCode as is and the result is shown as a nicely formatted TreeView.

ko.applyBindings({ TreeView: [{ "id": "1", "Parentid": null, "Name": "test1", "Status": "OK" }, { "id": "2", "Parentid": 1, "Name": "test2", "Status": "OK" }] }

But, I am getting the value from the server(put the value obtained from server in "str" variable) and doing the binding as below:

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})

I get the following error:

Error: Unable to parse bindings.
Message: ReferenceError: 'id' is undefined;
Bindings value: attr: { 'data-tt-id': id ,'data-tt-parent-id': Parentid}

Can someone please help. Thanks! The Json object returned had to be converted to type string and then parsed. This resolved the above issue

New Issue: I was just playing around with the treetable plugin. I wish to reload the data from server on button click(or ajax call every 5 sec). The data is duplicated.

4

1 回答 1

0

您正在对 JSON 进行字符串化而不是对其进行解析,并eval()在不需要时使用它。相反,只需使用JSON.parse().

var str = '[{ "id": "1", "Parentid": null, "Name": "parent1", "Status": "OK" }, { "id": "2", "Parentid": 1, "Name": "child1", "Status": "OK" }]';
var json = JSON.parse(str);
ko.applyBindings({ TreeView: json });
于 2013-10-18T03:17:22.427 回答