我正在尝试使用jstree来提供树。应该从javascript数组元素中提取树的节点。例如,我的数组是Library = {Milk,Bread,Cheese,...} 所以我希望父节点是Milk,Bread,Cheese,...... 这是我尝试的代码的一部分,我想基于数组初始化jstee_data。在这里,我只是初衷这个manullay。但我可以循环或某些事情吗?因为如果说数组有20个元素,则很难编写manullay!
<script type="text/javascript">
$(function() {
var lib_info = new Array();
lib_info[0]= new library_Object("Milk");
lib_info[1]= new library_Object("Bread");
lib_info[2]= new library_Object("Cheese");
$("#someButton").click(function() {
$("#treeContainer").jstree({
"json_data": {
"data": [
{ "data": lib_info[0].libData, "children": ["Child 1", "A Child 2"] }, { "data": lib_info[1].libData, "children": ["Child 1", "A Child 2"] }, { "data": "Last node" }, ]
},
"plugins": ["ui", "json_data", "themes"]
});
});
});
function library_Object(lib_data) {
this.libData = lib_data;
}
</script>
非常感谢任何帮助。