我需要根据数据库中的级别创建组织结构图。以下是表中的详细信息:
<script>
var datasource = {
'name': 'Lao Lao',
'title': 'general manager',
'children': [{
'name': 'Bo Miao',
'title': 'department manager'
}, {
'name': 'Su Miao',
'title': 'department manager',
'children': [{
'name': 'Tie Hua',
'title': 'senior engineer'
}, {
'name': 'Hei Hei',
'title': 'senior engineer'
}]
}, {
'name': 'Hong Miao',
'title': 'department manager'
}, {
'name': 'Chun Miao',
'title': 'department manager'
}]
};
$('#chart-container').orgchart({
'data': datasource,
'depth': 2,
'nodeContent': 'title'
});
</script>
我需要将数据库表值转换为数据源变量。
答案 0 :(得分:0)
您需要重新排序表格:
ordered=[];
function checkobj(obj,level){
//loop trough children:
obj.children.forEach(function(child){
//check children (children is in a lower level)
checkobj(child,level+1);
});
//add the object to the current level
ordered[level]=ordered[level]||[];
ordered[level].push(obj);
}
//start looping with your data at root
checkobj(datasource,0);
Ordered现在包含一个数组,代表每个级别:
ordered:[
0:[{},{}]//level 0 people (Lao Lao)
1:[{},{}]//level 1 the Miaos
...
];
所以你可以这样做:
ordered[0]//all people of level 0