Vue 组织结构图

2021-09-28  本文已影响0人  菜鸡前端

1. 实现方案

2. css+div 绘制思路

主要的绘制思路如下:

3. 细节与特性

4. 代码和效果演示

x4VEzeyj0N.gif
<template>
  <div class="wrapper">
    <div class="m-container">
      <TreeGraphV1
        ref="treeRef"
        :handleCollapse="mHandleCollpase"
        :treeData="treeData"
        :boxSize="boxSize"
        lazyKey="hasChildren"
        :getContentWidth="getContentWidth"
      />
    </div>
  </div>
</template>

<script lang="jsx">
import JsonData from './DemoV1.json'
import TreeGraphV1 from '../components/TreeGraphV1.vue'
export default {
  components: {
    TreeGraphV1
  },
  data() {
    return {
      treeData: JsonData,
      boxSize: {
        height: 40,
        margin: 15
      }
    };
  },

  methods: {
    // 自定义插槽
    renderContent (node) {
      return <span>{node.name}</span>
    },

    // 获取容器宽度
    getContentWidth(node) {
      return Math.max(node.name.length * 15 + 20, 60)
    },

    // 重新渲染
    refresh() {
      this.$refs.treeRef.reLayout()
    },

    // 处理懒加载节点
    mHandleCollpase(node, cb) {
      if (!node.children) {
        this.treeData = JSON.parse(JSON.stringify(this.treeData))
      }
      // 掉用 cb 进行再次渲染
      setTimeout(cb, 100);
    }
  }
};
</script>

<style lang="less" scoped>
.m-container {
  overflow: auto;
  width: 100vw;
  height: 100vh;
  border: 1px solid #eee;
}
</style>

5. 本文总结

本文中的实现方案,是在未做现有方法的研究下得出的,也未参考他人的实现,可能并不正确,仅提供参考。后续,待实现方案2后,我会去找一些其他的实现方案或者代码,进行对比,重新梳理本文。

上一篇 下一篇

猜你喜欢

热点阅读