当前位置:   article > 正文

Vue关系结构插件的使用(vue-org-tree)

vue-org-tree

一、安装插件

  1. npm install --save-dev vue2-org-tree
  2. // # 建议安装 less-loader 防止样式出现问题
  3. // 建议版本为 5.0.0,不然可能会报错
  4. npm install --save-dev less less-loader

二、在main中添加

  1. import Vue2OrgTree from 'vue2-org-tree'
  2. Vue.use(Vue2OrgTree)

下载css样式

https://unpkg.com/vue2-org-tree/dist/style.css

在使用页面引入

@import '../assets/css/style.css';

三、使用插件实现

1.页面使用

<vue2-org-tree :data="data"/>
  1. data: {
  2. id: 0,
  3. label: "XXX科技有限公司",
  4. children: [
  5. {
  6. id: 2,
  7. label: "产品研发部",
  8. children: [
  9. {
  10. id: 5,
  11. label: "研发-前端"
  12. },
  13. {
  14. id: 6,
  15. label: "研发-后端"
  16. },
  17. {
  18. id: 9,
  19. label: "UI设计"
  20. },
  21. {
  22. id: 10,
  23. label: "产品经理"
  24. }
  25. ]
  26. },
  27. {
  28. id: 3,
  29. label: "销售部",
  30. children: [
  31. {
  32. id: 7,
  33. label: "销售一部"
  34. },
  35. {
  36. id: 8,
  37. label: "销售二部"
  38. }
  39. ]
  40. },
  41. {
  42. id: 4,
  43. label: "财务部"
  44. },
  45. {
  46. id: 9,
  47. label: "HR人事"
  48. }
  49. ]
  50. },

2.修改背景色和点击事件

  1. <vue2-org-tree :data="data" horizontal :label-class-name="labelClassName"
  2. @on-node-click="onNodeClick" />
  1. data数据中添加
  2. data() {
  3. return {
  4. labelClassName:"bg-color-blue",//自定义颜色
  5. collapsable:true, //折叠属性
  6. horizontal:true, // 水平排列方式
  7. }
  8. },
  9. methods: {
  10. onNodeClick(e, data) {
  11. console.log(data)
  12. },
  13. }
  14. <style lang="scss" scoped>
  15. @import '../assets/css/style.css';
  16. ::v-deep .bg-color-blue{
  17. color: orange;
  18. background-color: red;
  19. }
  20. </style>

移入移出事件

  1. @on-node-mouseover="onMouseover"
  2. @on-node-mouseout="onMouseout"
  3. onMouseout(e, data) {
  4. },
  5. onMouseover(e, data) {
  6. },

四、点击展开关闭

1.增加@on-expand="onExpand" 方法

  1. <vue2-org-tree :data="NewsList[0]" :horizontal="horizontal"
  2. :label-class-name="labelClassName" :collapsable="collapsable"
  3. @on-node-click="onNodeClick" @on-expand="onExpand"
  4. :renderContent="renderContent"/>

  1. onExpand(e, data) {
  2. if ("expand" in data) {
  3. data.expand = !data.expand;
  4. if (!data.expand && data.children) {
  5. this.collapse(data.children);
  6. }
  7. } else {
  8. this.$set(data, "expand", true);
  9. }
  10. },
  11. collapse(list) {
  12. var _this = this;
  13. list.forEach(function (child) {
  14. if (child.expand) {
  15. child.expand = false;
  16. }
  17. child.children && _this.collapse(child.children);
  18. });
  19. },

五、自定义返回内容

1、:renderContent="renderContent" 

  1. <vue2-org-tree :data="NewsList[0]" :horizontal="horizontal"
  2. :label-class-name="labelClassName" :collapsable="collapsable"
  3. @on-node-click="onNodeClick" @on-expand="onExpand"
  4. :renderContent="renderContent"/>

2、js部分 

  1. renderContent(h, data) {
  2. if(data.type==1){
  3. return (
  4. <div class="cardlable">
  5. {data.sj?this.classType('书记',data.sj):''}
  6. </div>
  7. )
  8. }else{
  9. return (
  10. <div class="cardlable">{data.label}</div>
  11. )
  12. }
  13. },
  14. classType(name,data){
  15. if(!Array.isArray(data)) return;
  16. return (
  17. <div class="position-item">
  18. <h5>{name}:</h5>
  19. <div class="name">
  20. {
  21. data.map(item=> {
  22. return <h6>{item}</h6>
  23. })
  24. }
  25. </div>
  26. </div>
  27. )
  28. },

 完整代码

  1. <template>
  2. <div class="zznews-list">
  3. <vue2-org-tree :data="data" :horizontal="horizontal"
  4. :label-class-name="labelClassName" :collapsable="collapsable"
  5. @on-node-click="onNodeClick" @on-expand="onExpand"
  6. :renderContent="renderContent"/>
  7. </div>
  8. </template>
  9. <script>
  10. export default {
  11. data() {
  12. return {
  13. labelClassName:"bg-color-blue",
  14. collapsable:true,
  15. jibenInfo:{},
  16. showBJiBenModel:false,
  17. horizontal:true,
  18. data:{
  19. id: 0,
  20. label: "XXX科技有限公司",
  21. children: [
  22. {
  23. id: 2,
  24. label: "产品研发部",
  25. children: [
  26. {
  27. id: 5,
  28. label: "研发-前端",
  29. children: [
  30. {
  31. id: 55,
  32. label: "前端1"
  33. },
  34. {
  35. id: 56,
  36. label: "前端2"
  37. },
  38. {
  39. id: 57,
  40. label: "前端3"
  41. },
  42. {
  43. id: 58,
  44. label: "前端4"
  45. }
  46. ]
  47. },
  48. {
  49. id: 6,
  50. label: "研发-后端"
  51. },
  52. {
  53. id: 9,
  54. label: "UI设计"
  55. },
  56. {
  57. id: 10,
  58. label: "产品经理"
  59. }
  60. ]
  61. },
  62. {
  63. id: 3,
  64. label: "销售部",
  65. children: [
  66. {
  67. id: 7,
  68. label: "销售一部"
  69. },
  70. {
  71. id: 8,
  72. label: "销售二部"
  73. }
  74. ]
  75. },
  76. {
  77. id: 4,
  78. label: "财务部"
  79. },
  80. {
  81. id: 9,
  82. label: "HR人事"
  83. }
  84. ]
  85. },
  86. },
  87. }
  88. },
  89. created(){
  90. },
  91. methods: {
  92. onExpand(e, data) {
  93. if ("expand" in data) {
  94. data.expand = !data.expand;
  95. if (!data.expand && data.children) {
  96. this.collapse(data.children);
  97. }
  98. } else {
  99. this.$set(data, "expand", true);
  100. }
  101. },
  102. onNodeClick(e, data) {
  103. console.log(data)
  104. },
  105. collapse(list) {
  106. var _this = this;
  107. list.forEach(function (child) {
  108. if (child.expand) {
  109. child.expand = false;
  110. }
  111. child.children && _this.collapse(child.children);
  112. });
  113. },
  114. renderContent(h, data) {
  115. return (
  116. <div class="cardlable">{data.label}</div>
  117. )
  118. },
  119. }
  120. }
  121. </script>
  122. <style lang="scss" scoped>
  123. ::v-deep .bg-color-blue{
  124. color: orange;
  125. background-color: red;
  126. }
  127. </style>

六、补充插件  关系组件(relation-graph)

 网址:relation-graph:一个vue关系图谱组件

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/904343
推荐阅读
相关标签
  

闽ICP备14008679号