• vue2 使用vue-org-tree 组件完整简单示例Demo vue2-org-tree


    先上效果

    可以切换节点颜色,展开与否,坚排或者横排都可以配置。

    安装

    安装vue2-org-tree, 然后再安装样式 less-loade

    1. cnpm install --save-dev less less-loader
    2. cnpm install --save-dev vue2-org-tree
    3. 或者:
    4. npm i vue2-org-tree -S
    5. npm install --save-dev less less-loader –S

    安装完成后,vue2项目根目录下package.json自动更新以上 2个包信息

    1. "dependencies": {
    2. "core-js": "^3.8.3",
    3. "echarts": "^5.4.0",
    4. "element": "^0.1.4",
    5. "element-ui": "^2.15.10",
    6. "install": "^0.13.0",
    7. "less": "^3.9.0",
    8. "less-loader": "^4.1.0",
    9. "moment": "^2.29.4",
    10. "vue": "^2.6.14",
    11. "vue-router": "^3.5.2",
    12. "vue2-org-tree": "^1.3.6"
    13. },

    项目源码:

    main.js

    TreeTest.vue

    1. <template>
    2. <div ref="appContainer" class="app-container">
    3. <div style="margin-left:30px;">
    4. <el-row :gutter="20">
    5. <el-col :span="5">
    6. <el-switch v-model="horizontal" :width="50" active-text="横排" inactive-text="竖排" style="margin-top:8px;"/>
    7. </el-col>
    8. <el-col :span="5">
    9. <el-switch v-model="expandAll" :width="50" active-text="全部展开"
    10. inactive-text="全部折叠" style="margin:8px;" @change="expandChange"/>
    11. </el-col>
    12. <el-col :span="14">
    13. <span style="font-size:14px;font-weight:500;">选择背景色:</span>
    14. <el-select v-model="labelClassName" @change="selectChange">
    15. <el-option value="bg-white" label="白色">白色</el-option>
    16. <el-option value="bg-orange" label="橘黄色">橘黄色</el-option>
    17. <el-option value="bg-gold" label="金色">金色</el-option>
    18. <el-option value="bg-gray" label="灰色">灰色</el-option>
    19. <el-option value="bg-lightpink" label="浅粉色">浅粉色</el-option>
    20. <el-option value="bg-chocolate" label="巧克力色">巧克力色</el-option>
    21. <el-option value="bg-tomato" label="番茄色">番茄色</el-option>
    22. </el-select>
    23. </el-col>
    24. </el-row>
    25. </div>
    26. <div style="font-size:12px;margin-top:30px;">
    27. <el-scrollbar :style="scrollTreeStyle" class="el-org-tree">
    28. <vue2-org-tree
    29. :data="treeData.data"
    30. :horizontal="!horizontal"
    31. :collapsable="collapsable"
    32. :label-class-name="labelClassName"
    33. :render-content="renderContent"
    34. name="organ"
    35. @on-expand="onExpand"
    36. @on-node-click="onNodeClick"/>
    37. </el-scrollbar>
    38. </div>
    39. <br/><br/>
    40. </div>
    41. </template>
    42. <script>
    43. export default {
    44. name: "TreeTest",
    45. data() {
    46. return {
    47. "treeData": {
    48. labelClassName: "bg-color-orange",
    49. basicInfo: { id: null, label: "---null" },
    50. basicSwitch: false,
    51. data: {
    52. id: 0,
    53. label: "XXX科技有限公司",
    54. children: [
    55. {
    56. id: 2,
    57. label: "产品研发部",
    58. children: [
    59. {
    60. id: 5,
    61. label: "研发-前端",
    62. children: [
    63. {
    64. id: 55,
    65. label: "前端1"
    66. },
    67. {
    68. id: 56,
    69. label: "前端2"
    70. },
    71. {
    72. id: 57,
    73. label: "前端3"
    74. },
    75. {
    76. id: 58,
    77. label: "前端4"
    78. }
    79. ]
    80. },
    81. {
    82. id: 6,
    83. label: "研发-后端"
    84. },
    85. {
    86. id: 9,
    87. label: "UI设计"
    88. },
    89. {
    90. id: 10,
    91. label: "产品经理"
    92. }
    93. ]
    94. },
    95. {
    96. id: 3,
    97. label: "销售部",
    98. children: [
    99. {
    100. id: 7,
    101. label: "销售一部"
    102. },
    103. {
    104. id: 8,
    105. label: "销售二部"
    106. }
    107. ]
    108. },
    109. {
    110. id: 4,
    111. label: "财务部"
    112. },
    113. {
    114. id: 9,
    115. label: "HR人事"
    116. }
    117. ]
    118. },
    119. }
    120. ,
    121. horizontal: true, //横版 竖版
    122. collapsable: false,
    123. expandAll: true, //是否全部展开
    124. labelClassName: '白色', // 默认颜色
    125. scrollTreeStyle: 'width:100%;',
    126. }
    127. },
    128. methods: {
    129. //渲染节点
    130. renderContent(h, data) {
    131. return (
    132. <div>
    133. <div>
    134. <i class="el-icon-user-solid"></i>
    135. <span>{data.label}</span>
    136. <span></span>
    137. </div>
    138. <div style="font-size:12px;line-height:20px;">测试人员</div>
    139. </div>
    140. );
    141. },
    142. //鼠标移出
    143. onMouseout(e, data) {
    144. this.treeData.basicSwitch = false;
    145. },
    146. //鼠标移入
    147. onMouseover(e, data) {
    148. this.treeData.basicInfo = data;
    149. this.treeData.basicSwitch = true;
    150. var floating = document.getElementsByClassName("floating")[0];
    151. floating.style.left = e.clientX + "px";
    152. floating.style.top = e.clientY + "px";
    153. },
    154. //点击节点
    155. NodeClick(e, data) {
    156. console.log(e, data);
    157. },
    158. //默认展开
    159. toggleExpand(data, val) {
    160. if (Array.isArray(data)) {
    161. data.forEach(item => {
    162. this.$set(item, "expand", val);
    163. if (item.children) {
    164. this.toggleExpand(item.children, val);
    165. }
    166. });
    167. } else {
    168. this.$set(data, "expand", val);
    169. if (data.children) {
    170. this.toggleExpand(data.children, val);
    171. }
    172. }
    173. },
    174. collapse(list) {
    175. list.forEach(child => {
    176. if (child.expand) {
    177. child.expand = false;
    178. }
    179. child.children && this.collapse(child.children);
    180. });
    181. },
    182. //展开
    183. onExpand(e, data) {
    184. if ("expand" in data) {
    185. data.expand = !data.expand;
    186. if (!data.expand && data.children) {
    187. this.collapse(data.children);
    188. }
    189. } else {
    190. this.$set(data, "expand", true);
    191. }
    192. },
    193. getList() {
    194. // 后台回去的数据 赋值给data即可
    195. },
    196. // 自定义您的点击事件
    197. onNodeClick(e, data) {
    198. alert("点击")
    199. },
    200. expandChange() {
    201. this.collapsable = true
    202. this.toggleExpand(this.treeData.data, this.expandAll)
    203. },
    204. selectChange(){
    205. }
    206. }
    207. }
    208. </script>
    209. <!--<style scoped>-->
    210. <!--</style>-->
    211. <style lang="less">
    212. .bg-white {
    213. background-color: white;
    214. }
    215. .bg-orange {
    216. background-color: orange;
    217. }
    218. .bg-gold {
    219. background-color: gold;
    220. }
    221. .bg-gray {
    222. background-color: gray;
    223. }
    224. .bg-lightpink {
    225. background-color: lightpink;
    226. }
    227. .bg-chocolate {
    228. background-color: chocolate;
    229. }
    230. .bg-tomato {
    231. background-color: tomato;
    232. }
    233. //.org-tree-node-label-inner {
    234. // color: #fff;
    235. // background-color: orange;
    236. //}
    237. .el-org-tree {
    238. .el-scrollbar__wrap {
    239. overflow-x: hidden;
    240. }
    241. .org-tree-node-label {
    242. white-space: nowrap;
    243. }
    244. .el-tree-node__content{
    245. background: white;
    246. }
    247. .org-tree-node-label .org-tree-node-label-inner {
    248. padding-top: 8px;
    249. padding-right: 10px;
    250. padding-bottom: 5px;
    251. padding-left: 10px;
    252. cursor: pointer;
    253. }
    254. .horizontal .org-tree-node.is-leaf {
    255. padding-top: 5px;
    256. padding-bottom: 5px;
    257. }
    258. }
    259. </style>

    App.vue 

    1. <template>
    2. <div ref="appContainer" class="app-container">
    3. <div style="margin-left:30px;">
    4. <el-row :gutter="20">
    5. <el-col :span="5">
    6. <el-switch v-model="horizontal" :width="50" active-text="横排" inactive-text="竖排" style="margin-top:8px;"/>
    7. </el-col>
    8. <el-col :span="5">
    9. <el-switch v-model="expandAll" :width="50" active-text="全部展开" inactive-text="全部折叠" style="margin-top:8px;" @change="expandChange"/>
    10. </el-col>
    11. <el-col :span="14">
    12. <span style="font-size:14px;font-weight:500;">选择背景色:</span>
    13. <el-select v-model="labelClassName" @change="selectChange">
    14. <el-option value="bg-white" label="白色">白色</el-option>
    15. <el-option value="bg-orange" label="橘黄色">橘黄色</el-option>
    16. <el-option value="bg-gold" label="金色">金色</el-option>
    17. <el-option value="bg-gray" label="灰色">灰色</el-option>
    18. <el-option value="bg-lightpink" label="浅粉色">浅粉色</el-option>
    19. <el-option value="bg-chocolate" label="巧克力色">巧克力色</el-option>
    20. <el-option value="bg-tomato" label="番茄色">番茄色</el-option>
    21. </el-select>
    22. </el-col>
    23. </el-row>
    24. </div>
    25. <div style="font-size:12px;margin-top:30px;">
    26. <el-scrollbar :style="scrollTreeStyle" class="el-org-tree">
    27. <vue2-org-tree
    28. :data="treeData"
    29. :horizontal="!horizontal"
    30. :collapsable="collapsable"
    31. :label-class-name="labelClassName"
    32. :render-content="renderContent"
    33. name="organ"
    34. @on-expand="onExpand"
    35. @on-node-click="onNodeClick"/>
    36. </el-scrollbar>
    37. </div>
    38. </div>
    39. </template>
    40. <script>
    41. export default {
    42. name: "TreeTest",
    43. data() {
    44. return {
    45. "treeData": {
    46. labelClassName: "bg-color-orange",
    47. basicInfo: { id: null, label: null },
    48. basicSwitch: false,
    49. // data: {
    50. id: 0,
    51. label: "XXX科技有限公司",
    52. children: [
    53. {
    54. id: 2,
    55. label: "产品研发部",
    56. children: [
    57. {
    58. id: 5,
    59. label: "研发-前端",
    60. children: [
    61. {
    62. id: 55,
    63. label: "前端1"
    64. },
    65. {
    66. id: 56,
    67. label: "前端2"
    68. },
    69. {
    70. id: 57,
    71. label: "前端3"
    72. },
    73. {
    74. id: 58,
    75. label: "前端4"
    76. }
    77. ]
    78. },
    79. {
    80. id: 6,
    81. label: "研发-后端"
    82. },
    83. {
    84. id: 9,
    85. label: "UI设计"
    86. },
    87. {
    88. id: 10,
    89. label: "产品经理"
    90. }
    91. ]
    92. },
    93. {
    94. id: 3,
    95. label: "销售部",
    96. children: [
    97. {
    98. id: 7,
    99. label: "销售一部"
    100. },
    101. {
    102. id: 8,
    103. label: "销售二部"
    104. }
    105. ]
    106. },
    107. {
    108. id: 4,
    109. label: "财务部"
    110. },
    111. {
    112. id: 9,
    113. label: "HR人事"
    114. }
    115. ]
    116. },
    117. // }
    118. // ,
    119. horizontal: false, //横版 竖版
    120. collapsable: false,
    121. expandAll: true, //是否全部展开
    122. labelClassName: '白色', // 默认颜色
    123. scrollTreeStyle: 'width:100%;',
    124. }
    125. },
    126. methods: {
    127. //渲染节点
    128. renderContent(h, data) {
    129. return (
    130. <div>
    131. <div>
    132. <i class="el-icon-user-solid"></i>
    133. <span>{data.label}</span>
    134. <span></span>
    135. </div>
    136. <div style="font-size:12px;line-height:20px;">测试人员</div>
    137. </div>
    138. );
    139. },
    140. //鼠标移出
    141. onMouseout(e, data) {
    142. this.basicSwitch = false;
    143. },
    144. //鼠标移入
    145. onMouseover(e, data) {
    146. this.basicInfo = data;
    147. this.basicSwitch = true;
    148. var floating = document.getElementsByClassName("floating")[0];
    149. floating.style.left = e.clientX + "px";
    150. floating.style.top = e.clientY + "px";
    151. },
    152. //点击节点
    153. NodeClick(e, data) {
    154. console.log(e, data);
    155. },
    156. //默认展开
    157. toggleExpand(data, val) {
    158. if (Array.isArray(data)) {
    159. data.forEach(item => {
    160. this.$set(item, "expand", val);
    161. if (item.children) {
    162. this.toggleExpand(item.children, val);
    163. }
    164. });
    165. } else {
    166. this.$set(data, "expand", val);
    167. if (data.children) {
    168. this.toggleExpand(data.children, val);
    169. }
    170. }
    171. },
    172. collapse(list) {
    173. list.forEach(child => {
    174. if (child.expand) {
    175. child.expand = false;
    176. }
    177. child.children && this.collapse(child.children);
    178. });
    179. },
    180. //展开
    181. onExpand(e, data) {
    182. if ("expand" in data) {
    183. data.expand = !data.expand;
    184. if (!data.expand && data.children) {
    185. this.collapse(data.children);
    186. }
    187. } else {
    188. this.$set(data, "expand", true);
    189. }
    190. },
    191. getList() {
    192. // 后台回去的数据 赋值给data即可
    193. },
    194. // 自定义您的点击事件
    195. onNodeClick(e, data) {
    196. alert("点击")
    197. },
    198. expandChange() {
    199. this.collapsable = true
    200. this.toggleExpand(this.data, this.expandAll)
    201. },
    202. selectChange(){
    203. }
    204. }
    205. }
    206. </script>
    207. <!--<style scoped>-->
    208. <!--</style>-->
    209. <style lang="less">
    210. .bg-white {
    211. background-color: white;
    212. }
    213. .bg-orange {
    214. background-color: orange;
    215. }
    216. .bg-gold {
    217. background-color: gold;
    218. }
    219. .bg-gray {
    220. background-color: gray;
    221. }
    222. .bg-lightpink {
    223. background-color: lightpink;
    224. }
    225. .bg-chocolate {
    226. background-color: chocolate;
    227. }
    228. .bg-tomato {
    229. background-color: tomato;
    230. }
    231. //.org-tree-node-label-inner {
    232. // color: #fff;
    233. // background-color: orange;
    234. //}
    235. .el-org-tree {
    236. .el-scrollbar__wrap {
    237. overflow-x: hidden;
    238. }
    239. .org-tree-node-label {
    240. white-space: nowrap;
    241. }
    242. .el-tree-node__content{
    243. background: white;
    244. }
    245. .org-tree-node-label .org-tree-node-label-inner {
    246. padding-top: 8px;
    247. padding-right: 10px;
    248. padding-bottom: 5px;
    249. padding-left: 10px;
    250. cursor: pointer;
    251. }
    252. .horizontal .org-tree-node.is-leaf {
    253. padding-top: 5px;
    254. padding-bottom: 5px;
    255. }
    256. }
    257. </style>

    样式配置:

    •  *:horizontal 组织结构图的排列方式,水平还是竖直,值为true 或 false
    •          collapsable 折叠效果,值为true 或 false
    •          label-class-name 修改背景色,支持自定义类名
    •          render-content 定义如何渲染 node 标签,绑定的 renderContent 是函数
    •          on-expand 定义折叠的组织结构如何展开,该函数参数必须传两个,第一个是 e ,第二个是 data
    •          on-node-click 定义当前节点被点击时执行的方法

    需要修改label名称

    1. :render-content="renderContent" //这个属性
    2. renderContent(h, data) { return data.taskName },

    注意:

    注意:在style中引入样式,必须引入 引入全局样式: lang的值是less 样式

    如下,否则展开收缩按钮显示不出来,可以通过上面方法安装,或者下载less文件引用。

    https://gitee.com/hukaicode/vue-org-tree/tree/master/src/styles

    可以根据实际项目需要进行修改。

  • 相关阅读:
    团队Git规范文档(操作规范及提交规范)
    NFT交易平台开发 创建NFT数字藏品平台
    golang中的锁竞争问题
    【MySQL】高性能高可用设计实战-索引篇
    【AUTOSAR-RTE】-3-Runnable及其Task Mapping映射
    Vue + Flask 实现单页面应用
    torch.cuda.is_available() 解决方案
    交换机与路由器技术:静态NAT、动态NAT、PAT和端口镜像
    [附源码]Python计算机毕业设计Django保护濒危动物公益网站
    如何解决Smartsheet 登录时遇到的问题
  • 原文地址:https://blog.csdn.net/LlanyW/article/details/127647395