Tree.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <template>
  2. <div class="tree-container">
  3. <el-row :gutter="20" >
  4. <TreeNode v-for="item in colList[0]" :title="item.title" :position-y="item.positionY" position-x="0" :key="item.id"></TreeNode>
  5. <TreeNode v-for="item in colList[1]" :title="item.title" :position-y="item.positionY" :position-x="1" :key="item.id"></TreeNode>
  6. <TreeNode v-for="item in colList[2]" :title="item.title" :position-y="item.positionY" :position-x="2" :key="item.id"></TreeNode>
  7. <TreeNode v-for="item in colList[3]" :title="item.title" :position-y="item.positionY" :position-x="3" :key="item.id"></TreeNode>
  8. </el-row>
  9. </div>
  10. </template>
  11. <script>
  12. import TreeNode from "@/components/TreeNode";
  13. export default {
  14. name: "Tree",
  15. components: {TreeNode},
  16. data() {
  17. return {
  18. treeHeight: 4,
  19. requirements: [{
  20. deep: 1,
  21. id: 1,
  22. parent: null,
  23. title: '这是标题',
  24. },
  25. {
  26. deep: 2,
  27. id: 2,
  28. parent: 1,
  29. title: '这是标题2',
  30. },
  31. {
  32. deep: 2,
  33. id: 3,
  34. parent: 1,
  35. priority: 0,
  36. title: '这是标题3',
  37. },
  38. {
  39. deep: 3,
  40. id: 4,
  41. parent: 3,
  42. title: '这是标题4',
  43. },
  44. {
  45. deep:4,
  46. id:9,
  47. parent: 4,
  48. title: '这是标题9'
  49. },
  50. {
  51. deep: 3,
  52. id: 5,
  53. parent: 3,
  54. title: '这是标题5',
  55. },
  56. {
  57. deep: 4,
  58. id: 6,
  59. parent: 5,
  60. title: '这是标题6',
  61. },
  62. {
  63. deep: 4,
  64. id: 7,
  65. parent: 5,
  66. title: '这是标题7',
  67. },
  68. {
  69. deep: 4,
  70. id: 8,
  71. parent: 5,
  72. title: '这是标题8',
  73. },
  74. ],
  75. tree: {},
  76. nodeMap: {},
  77. colList: [[],[],[],[]]
  78. }
  79. },
  80. mounted() {
  81. this.array2tree()
  82. this.getList()
  83. this.tree.positionY = 0
  84. this.calculateContentHeight(this.tree)
  85. this.calculateNodePositionY()
  86. this.offsetPositionY()
  87. console.warn(this.tree)
  88. },
  89. methods: {
  90. array2tree(){
  91. let treeMap = new Map()
  92. this.requirements.forEach((item)=>{
  93. treeMap.set(item.id,item)
  94. })
  95. this.nodeMap = treeMap
  96. this.requirements.forEach((item)=>{
  97. if(item.parent){
  98. let tempParent = treeMap.get(item.parent)
  99. if(!tempParent.childs){
  100. tempParent.childs = []
  101. }
  102. tempParent.childs.push(item)
  103. }else{
  104. this.tree = item
  105. }
  106. })
  107. },
  108. getList(){
  109. this.requirements.forEach((item)=>{
  110. this.colList[item.deep-1].push(item)
  111. })
  112. },
  113. calculateContentHeight(node){
  114. if(!node.childs){
  115. node.contentHeight = 1
  116. return 1
  117. }
  118. node.contentHeight = node.childs.reduce((total,current)=>{
  119. return total + this.calculateContentHeight(current)
  120. },0)
  121. return node.contentHeight
  122. },
  123. calculateNodePositionY() {
  124. for(let i=1;i<4;i++){
  125. let sum = 0
  126. this.colList[i].forEach((item)=>{
  127. item.positionY = this.nodeMap.get(item.parent).positionY + sum
  128. sum = sum + item.contentHeight
  129. })
  130. }
  131. },
  132. offsetPositionY(){
  133. for(let i=2;i>=0;i--){
  134. this.colList[i].forEach((item)=>{
  135. if(item.childs){
  136. let length = item.childs.length
  137. item.positionY = item.childs.reduce((total,current)=>{
  138. return total+current.positionY/length
  139. },0)
  140. }
  141. })
  142. }
  143. }
  144. }
  145. }
  146. </script>
  147. <style scoped>
  148. .tree-container{
  149. min-height: 300px;
  150. width: 100%;
  151. background-color: #ffffff;
  152. }
  153. </style>