javascript - 缩进式组织树

标签 javascript html css tree

我正在寻找一些算法解决方案来构建组织树/图表,允许缩进或偏移一个或多个级别。

我有一组 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人(财务部、销售部、人力资源部)。由于HR部门的_parent_id_=1和level=3,那么图表中的位置应该与财务部和销售部在同一水平线上。

下面是模型组织树和缩进为人力资源部门的示例。

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

相关文章:

css - 水平 JQuery UI slider

javascript - 检查数组是否包含字符串,但忽略该字符串中的其余字母

html - 响应式菜单 - 如何?

html - HTML 中的渐变文本效果

html - 使用 div 隐藏的折叠输入类型

javascript - 在 Rails 开发中拆分 css 和 javascript 文件

带有方法的 JavaScript 对象

javascript - jQuery 自动完成太多数据库请求

javascript - 如何在 jquery ui 模态对话框小部件内部从外部 url 动态加载内容?

css - 有序列表 CSS 样式包括父编号