1
我在看以下問題Knockout JS treeview with checkboxes,我最終做了很多嵌套的foreach循環,這裏是示例。 http://jsfiddle.net/47d6r/384/所以我想知道有沒有辦法做一種遞歸foreach?在淘汰賽中遞歸調用foreach
HTML(想把遞歸的foreach在這裏)
<div id='tree2'>
<ul data-bind="foreach: folders">
<li>
<span data-bind="text: name"></span>
<ul data-bind="foreach: folders">
<li>
<span data-bind="text: name"></span>
<ul data-bind="foreach: folders">
<li>
<span data-bind="text: name"></span>
<ul data-bind="foreach: folders">
<li>
<span data-bind="text: name"></span>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</ul>
</div>
的JavaScript
function folder(name) {
var self = this;
this.name = ko.observable(name);
this.folders = ko.observableArray();
this.addFolder = function(name) {
self.folders.push(new folder(name));
}
}
function model() {
var self = this;
this.folders = ko.observableArray()
}
var mymodel = new model();
$(document).ready(function() {
ko.applyBindings(mymodel);
mymodel.folders.push(new folder('Jord'));
mymodel.folders.push(new folder('Mud'));
mymodel.folders.push(new folder('Rock'));
mymodel.folders.push(new folder('Rock1'));
mymodel.folders()[0].addFolder('GroJord');
mymodel.folders()[0].addFolder('ModerJord');
mymodel.folders()[0].folders()[0].addFolder('Jord - Grov Matjord');
mymodel.folders()[0].folders()[0].addFolder('Jord - Fin Matjord');
mymodel.folders()[0].folders()[0].folders()[0].addFolder('Jord Grov Matjord 0 18 verified')
mymodel.folders()[0].folders()[0].folders()[0].addFolder('Jord Grov Matjord unknown')
});
$(function() {
$("#tree2").jstree({
"checkbox": {
"keep_selected_style": false
},
"plugins": ["checkbox"]
});
});