我正在开发一个基本的树视图,它将列出一些项目,并且我希望能够根据用户输入来更改子项目的文本。
看法:
<div class="treeviewUI">
<p>
Test Tree View Model
</p>
<p><strong>Enter Child One: <input data-bind="value: $parent.childOne"/></strong> <!-- also tried just childOne -->
</p>
<ul id='treeMenu' data-bind="foreach: menu">
<li>
<div data-bind="click: function() {$parent.toggle($data)}">
<span data-bind="text: $data.text></span>
<ul data-bind="visible: $data.expanded,
foreach: {data: $data.children, as 'child' }">
<li data-bind="text:child.text"/>
</ul>
</div>
</li>
</ul>
和视图模型:
define([knockout'], function (ko) {
var childOne = ko.observable();
var viewModel = {
menu: [
text: "Item 1",
children: [
{ text: childOne() },
{ text: "child 1b" }
],
expanded: ko.observable(false)
},
{
text: "Item 2",
children: [
{ text: "child 2a" },
{ text: "child 2b" }
],
expanded: ko.observable(false)
}
],
toggle: function(node) {
node.expanded(!node.expanded());
}
};
return viewModel;
});
但是,为 输入输入childOne
不会更新 treeView 子项。我是否缺少绑定或只是使用了错误类型的绑定。还是这里发生了更复杂的事情?
JSFIDDLE 链接:http: //jsfiddle.net/xQ7GS/54/