我正在寻找一些用于构建组织树/图的算法解决方案,以允许缩进或偏移一个或多个级别。
我有一组json数据:
[{
"id": 1,
"level":1,
"parent_id": 0,
"name": "Board of Director"
}, {
"id": 2,
"level":2,
"parent_id": 1,
"name": "Finance Division"
}, {
"id": 3,
"level":3,
"parent_id": 2,
"name": "Finance Department"
}, {
"id": 4,
"level":3,
"parent_id": 2,
"name": "Sales Department"
}, {
"id": 5,
"level":3,
"parent_id": 1,
"name": "HR Division"
}]
简要说明:
_parent_id_属性用于使用行将部门与其父级联系起来,而level属性用于对部门应位于或缩进的位置进行分类,例如:1级(董事会),2级(财务部)和3级(财务部,销售部) ,人力资源部)。由于人力资源部的_parent_id_ = 1且级别= 3,因此图表中的职位应与财务部和销售部在同一直线上。
下面是样机组织树和缩进为HR部门的示例。
.tree li {
list-style-type: none;
margin: 0;
padding: 10px 5px 0 5px;
position: relative
}
.tree li::before,
.tree li::after {
content: '';
left: -20px;
position: absolute;
right: auto
}
.tree li::before {
border-left: 1px solid #999;
bottom: 50px;
height: 100%;
top: 0;
width: 1px
}
.tree li::after {
border-top: 1px solid #999;
top: 30px;
width: 25px
}
.tree li div {
border: 1px solid #999;
display: inline-block;
padding: 8px 24px;
text-decoration: none
}
.tree li.parent > div {
cursor: pointer
}
.tree > ul > li::before,
.tree > ul > li::after {
border: 0
}
.tree li:last-child::before {
height: 30px
}
.tree li.parent > div:hover,
.tree li.parent > div:hover + ul li div {
background: #f3f3f4;
border: 1px solid #94a0b4;
color: #000
}
li div.just-line {
display: none;
}
div.just-line + ul > li::before {
padding: 0;
border-left: 0;
}
div.just-line + ul > li::after {
left: -40px;
width: 45px;
}
.no-padding {
padding: 0 !important;
}
<div class="tree">
<ul>
<li data-id="1">
<div>Board of Director</div>
<ul>
<li>
<div>Finance Division</div>
<ul>
<li>
<div>Finance Department</div>
<li>
<div>Sales Department</div>
</li>
</ul>
</li>
<li class="no-padding">
<div class="just-line"></div>
<ul>
<li>
<div>HR Division</div>
</li>
</ul>
</li>
</ul>
</li>
</ul>
任何帮助或建议,将不胜感激。谢谢。
最佳答案
刚开始,这是一个两步解决方案,它首先生成一棵树,然后渲染DOM。
var data = [{ "id": 1, "level": 1, "parent_id": 0, "name": "Board of Director" }, { "id": 2, "level": 2, "parent_id": 1, "name": "Finance Division" }, { "id": 3, "level": 3, "parent_id": 2, "name": "Finance Department" }, { "id": 4, "level": 3, "parent_id": 2, "name": "Sales Department" }, { "id": 5, "level": 3, "parent_id": 1, "name": "HR Division" }],
tree = function (data, root) {
var r, o = {};
data.forEach(function (a) {
a.children = o[a.id] && o[a.id].children;
o[a.id] = a;
if (a.parent_id === root) {
r = a;
} else {
o[a.parent_id] = o[a.parent_id] || {};
o[a.parent_id].children = o[a.parent_id].children || [];
o[a.parent_id].children.push(a);
}
});
return r;
}(data, 0),
ul = document.createElement('ul');
[tree].forEach(function iter(level) {
return function (a) {
var li = document.createElement('li'),
div = document.createElement('div'),
ul,
l = level;
this.appendChild(li);
while (++l < a.level) {
div.className = 'just-line';
li.className = 'no-padding';
li.appendChild(div);
ul = document.createElement('ul');
li.appendChild(ul);
li = document.createElement('li');
ul.appendChild(li);
div = document.createElement('div');
}
div.appendChild(document.createTextNode(a.name));
li.appendChild(div);
if (a.children) {
ul = document.createElement('ul');
li.appendChild(ul);
a.children.forEach(iter(a.level), ul);
}
};
}(0), ul);
document.getElementById('tree').appendChild(ul);
.tree li { list-style-type: none; margin: 0; padding: 10px 5px 0 5px; position: relative; }
.tree li::before, .tree li::after { content: ''; left: -20px; position: absolute; right: auto; }
.tree li::before { border-left: 1px solid #999; bottom: 50px; height: 100%; top: 0; width: 1px; }
.tree li::after { border-top: 1px solid #999; top: 30px; width: 25px; }
.tree li div { border: 1px solid #999; display: inline-block; padding: 8px 24px; text-decoration: none; }
.tree li.parent > div { cursor: pointer; }
.tree > ul > li::before, .tree > ul > li::after { border: 0; }
.tree li:last-child::before { height: 30px; }
.tree li.parent > div:hover, .tree li.parent > div:hover + ul li div { background: #f3f3f4; border: 1px solid #94a0b4; color: #000; }
li div.just-line { display: none; }
div.just-line + ul > li::before { padding: 0; border-left: 0; }
div.just-line + ul > li::after { left: -40px; width: 45px; }
.no-padding { padding: 0 !important; }
<div id="tree" class="tree"></div>
关于javascript - 缩进的组织树,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39918912/