我正在尝试将平面JSON数组转换为组织结构图的管理器/从属树结构。我当前正在运行此功能(我没有写这个,我从'Convert flat JSON file to hierarchical json data'那里拿来的):
function parseJSON(xlsxData) {
console.log(xlsxData);
var newData = { name: "root", children: [] },
levels = ["ManagerLast"];
// For each data row, loop through the expected levels traversing the output tree
xlsxData.forEach(function(d) {
// Keep this as a reference to the current level
var depthCursor = newData.children;
// Go down one level at a time
levels.forEach(function(property, depth) {
// Look to see if a branch has already been created
var index;
depthCursor.forEach(function(child, i) {
if (d[property] == child.name) index = i;
});
// Add a branch if it isn't there
if (isNaN(index)) {
depthCursor.push({ name: d[property], children: [] });
index = depthCursor.length - 1;
}
// Now reference the new child array as we go deeper into the tree
depthCursor = depthCursor[index].children;
// This is a leaf, so add the last element to the specified branch
if (depth === levels.length - 1)
depthCursor.push({
name: d.FirstName + " " + d.LastName,
orgCode: d.SOMOrgCode
});
});
});
我遇到一个问题,我的数据仅解析为两层,即经理及其下属的第一层(我需要下属拥有下属等)。
我正在尝试在treant.js中渲染它,这也给我带来了一些问题,因为我不知道如何使用以下配置将所有这些JSON数据添加到已经存在的JSON数组的末尾其中的信息:
var chart_config = {
chart: {
container: "#orgChart",
connectors: {
type: "step"
},
node: {
HTMLclass: "nodeExample1"
},
scrollbar: "fancy"
},
nodeStructure: {
<INSERT JSON DATA HERE>
}
我需要数据以以下格式显示:
{"FirstName": "John",
"LastName": "Doe",
children: [
{
"FirstName": "Jane",
"LastName": "Doe",
children: [
etc...
]
}]
}
我正在努力想出一种仅使用经理的名字和姓氏来创建此层次结构的方法,然后还包括此配置数据...任何人都可以在上述功能上进行改进或提出一个更巧妙的方法也许?原始JSON数据中包含ManagerFirst和ManagerLast字段。