简体   繁体   English

如何使用PHP和Javascript基于级别生成组织结构图?

[英]How to generate an organisation chart based on level using PHP and Javascript?

I need to create an organisation chart based on the level in database. 我需要基于数据库中的级别创建组织结构图。 Given below is the details in table : 下表是详细信息:

数据库表中的值

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

I need to get database table values into datasource variable. 我需要将数据库表值放入数据源变量中。

You need to reorder your table: 您需要重新排序表格:

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 now contains an array, representing each level: 现在订购的包含一个数组,代表每个级别:

ordered:[
 0:[{},{}]//level 0 people (Lao Lao)
 1:[{},{}]//level 1 the Miaos
 ...
  ];

So you can do: 因此,您可以执行以下操作:

 ordered[0]//all people of level 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM