vue橫向組織結(jié)構(gòu)圖實現(xiàn)

實現(xiàn)效果圖如下:


image.png

數(shù)據(jù)格式 tree

let  dataInfo = [
        {
          name: "root",
          value: "root",
          children: [
            {
              name: "child1",
              value: "一級1",
              children: [
                {
                  name: "child2",
                  value: "二級1",
                  children: []
                },
                {
                  name: "child2",
                  value: "二級2",
                  children: []
                }
              ]
            },
            {
              name: "child1",
              value: "一級2",
              children: [
                {
                  name: "child2",
                  value: "二級3",
                  children: [
                  ]
                }
              ]
            }
          ]
        }
      ]

實現(xiàn)代碼 遞歸調(diào)用最小實現(xiàn)單元 組件NodeTree.vue

<!--
 * @Descripttion: 橫向樹實現(xiàn)demo
 * @version: 
 * @Author: year
--> 
<template>
  <div class="item-content" ref="cardItemDiv">
    <div class="info-card">
      <div class="card-item" v-for="(item, index) in data" :key="index">
        <span
          class="vertical-line"
          :style="computedHeight(item.height, data.length, index)"
          v-if="item.name !== 'root'"
        ></span>
        <span class="horizontal-line" v-if="item.name !== 'root'"></span>
        <div class="div-card">
          <div>{{item.value}}</div>
        </div>
        <span class="horizontal-line" v-if="item.children&&item.children.length !== 0"></span>
        <equip-list :data="item.children" v-if="item.children&&item.children.length !== 0"></equip-list>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "equipList",
  props: {
    data: Array
  },
  data() {
    return {
    };
  },
  methods: {
    computedHeight(pheight, length, index) {
      if (length == 1 || length == 0) {
        return {
          height: "0px",
          display: "none"
        };
      } else {
        let height = 0;
        let marginTop = 0;
        let marginB = 0;
        if (index == 0) {
          height = pheight / 2;
          marginTop = height;
          return {
            height: height + "px",
            "margin-top": marginTop + "px"
          };
        }
        if (index == length - 1) {
          height = pheight / 2;
          marginB = height;
          return {
            height: height + "px",
            "margin-bottom": marginB + "px"
          };
        } else {
          height = pheight;
          return {
            height: height + "px"
          };
        }
      }
    }
  },
  components: {},
  mounted() {
  }
};
</script>

<style lang='scss' scoped>
.item-content {
  height: 100%;
  width: 100%;
  display: flex;
  .vertical-line {
    display: inline-block;
    width: 2px;
    background: #eaeaea;
  }

  .card-item {
    display: flex;
    align-items: center;
    .horizontal-line {
      width: 40px;
      display: inline-block;
      height: 2px;
      background: #eaeaea;
    }
  }
}

.div-card {
  height: 77px;
  & > div {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 220px;
    height: 67px;
    background: inherit;
    background-color: rgba(253, 253, 253, 1);
    border: none;
    border-radius: 4px;
    -moz-box-shadow: 1px 3px 5px rgba(0, 0, 0, 0.2);
    -webkit-box-shadow: 1px 3px 5px rgba(0, 0, 0, 0.2);
    box-shadow: 1px 3px 5px rgba(0, 0, 0, 0.2);
    color: #8d8d8e;
  }
}
</style>

父組件調(diào)用 Parent.vue

引入NodeTree.vue

<!--
 * @Descripttion: 橫向組織架構(gòu)圖實現(xiàn)
 * @version: 
 * @Author: year

--> 
<template>
  <div class="shopping-table-info">
    <NodeTree :data="dataInfo" />
  </div>
</template>

<script>
import NodeTree from "./ShoppingEquipList";
export default {
  data() {
    return {
      dataInfo: []
    };
  },
  methods: {
    getDataInfo() {
      let dataInfo = [
        {
          name: "root",
          value: "root",
          children: [
            {
              name: "child1",
              value: "一級1",
              children: [
                {
                  name: "child2",
                  value: "二級1",
                  children: []
                },
                {
                  name: "child2",
                  value: "二級2",
                  children: []
                }
              ]
            },
            {
              name: "child1",
              value: "一級2",
              children: [
                {
                  name: "child2",
                  value: "二級3",
                  children: []
                }
              ]
            }
          ]
        }
      ];

      let fixedData = dataInfo.map(item => {
        return this.traveTree(item);
      });

      this.dataInfo = fixedData;
    },
    traveTree(nodeInfo) {
      let childrenInfo = nodeInfo.children;
      if (!childrenInfo || childrenInfo.length == 0) {
        nodeInfo.height = 77;
      } else {
        childrenInfo.map(item => {
          this.traveTree(item);
        });

        nodeInfo.height = childrenInfo.reduce((preV, n) => {
          return preV + n.height;
        }, 0);
      }
      return nodeInfo;
    }
  },
  components: {
    NodeTree
  },
  mounted() {
    this.getDataInfo();
  }
};
</script>



實現(xiàn)思路

抽取最小實現(xiàn)單元继榆,然后循環(huán)遞歸即可


image.png

(1)左右兩端水平線 實現(xiàn):


image.png
 <span class="horizontal-line" v-if="item.name !== 'root'"></span>
<span class="horizontal-line"  v-if="item.children&&item.children.length !== 0"></span>

span標簽畫兩條水平線巾表,然后利用flex布局,居中布局即可略吨。最小單元后面的水平線判斷沒有后代的時候 不顯示即可
(2)連接后代的縱軸部分實現(xiàn):


image.png

需要知道每個后代的高度集币,然后第一個后代的高度/2 + 中間所有后代的高度和+ 最后一個后代的高度/2 就是所需要縱軸的高度
高度的具體實現(xiàn)代碼: 假設最后一層后代的高度為70
a.初始數(shù)據(jù)處理: 利用二叉樹的 后序遍歷方法先給每一個節(jié)點添加上高度屬性

     traveTree(nodeInfo) {
      let childrenInfo = nodeInfo.children;
      if (!childrenInfo || childrenInfo.length == 0) {
        nodeInfo.height = 77;
      } else {
        childrenInfo.map(item => {
          this.traveTree(item);
        });

        nodeInfo.height = childrenInfo.reduce((preV, n) => {
          return preV + n.height;
        }, 0);
      }
      return nodeInfo;
    }

b.計算鏈接線高度

 computedHeight(pheight, length, index) {
      if (length == 1 || length == 0) {
        return {
          height: "0px",
          display: "none"
        };
      } else {
        let height = 0;
        let marginTop = 0;
        let marginB = 0;
        if (index == 0) {
          height = pheight / 2;
          marginTop = height;
          return {
            height: height + "px",
            "margin-top": marginTop + "px"
          };
        }
        if (index == length - 1) {
          height = pheight / 2;
          marginB = height;
          return {
            height: height + "px",
            "margin-bottom": marginB + "px"
          };
        } else {
          height = pheight;
          return {
            height: height + "px"
          };
        }
      }
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市翠忠,隨后出現(xiàn)的幾起案子鞠苟,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,525評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件当娱,死亡現(xiàn)場離奇詭異吃既,居然都是意外死亡,警方通過查閱死者的電腦和手機跨细,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,203評論 3 395
  • 文/潘曉璐 我一進店門鹦倚,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人冀惭,你說我怎么就攤上這事震叙。” “怎么了散休?”我有些...
    開封第一講書人閱讀 164,862評論 0 354
  • 文/不壞的土叔 我叫張陵媒楼,是天一觀的道長。 經(jīng)常有香客問我戚丸,道長划址,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,728評論 1 294
  • 正文 為了忘掉前任限府,我火速辦了婚禮夺颤,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘谣殊。我一直安慰自己拂共,他們只是感情好,可當我...
    茶點故事閱讀 67,743評論 6 392
  • 文/花漫 我一把揭開白布姻几。 她就那樣靜靜地躺著宜狐,像睡著了一般。 火紅的嫁衣襯著肌膚如雪蛇捌。 梳的紋絲不亂的頭發(fā)上抚恒,一...
    開封第一講書人閱讀 51,590評論 1 305
  • 那天,我揣著相機與錄音络拌,去河邊找鬼俭驮。 笑死,一個胖子當著我的面吹牛春贸,可吹牛的內(nèi)容都是我干的混萝。 我是一名探鬼主播,決...
    沈念sama閱讀 40,330評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼萍恕,長吁一口氣:“原來是場噩夢啊……” “哼逸嘀!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起允粤,我...
    開封第一講書人閱讀 39,244評論 0 276
  • 序言:老撾萬榮一對情侶失蹤崭倘,失蹤者是張志新(化名)和其女友劉穎翼岁,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體司光,經(jīng)...
    沈念sama閱讀 45,693評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡琅坡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,885評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了残家。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片榆俺。...
    茶點故事閱讀 40,001評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖坞淮,靈堂內(nèi)的尸體忽然破棺而出谴仙,到底是詐尸還是另有隱情,我是刑警寧澤碾盐,帶...
    沈念sama閱讀 35,723評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站揩局,受9級特大地震影響毫玖,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜凌盯,卻給世界環(huán)境...
    茶點故事閱讀 41,343評論 3 330
  • 文/蒙蒙 一付枫、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧驰怎,春花似錦阐滩、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,919評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至症杏,卻和暖如春装获,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背厉颤。 一陣腳步聲響...
    開封第一講書人閱讀 33,042評論 1 270
  • 我被黑心中介騙來泰國打工穴豫, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人逼友。 一個月前我還...
    沈念sama閱讀 48,191評論 3 370
  • 正文 我出身青樓精肃,卻偏偏與公主長得像,于是被迫代替她去往敵國和親帜乞。 傳聞我的和親對象是個殘疾皇子司抱,可洞房花燭夜當晚...
    茶點故事閱讀 44,955評論 2 355