小言_互联网的博客

vue+Element实现Tree树形(是否默认展开所有节点属性: default-expand-all)

383人阅读  评论(0)

vue-element-admin一个基于 vue2.0 和 Eelement 的控制面板 UI 框架,这是使用vue技术栈开发的前端程序员的首选管理系统模板,模板以及非常的成熟了,并且有相关的社区和维护人员,开发时候遇到问题也不要慌。

推荐指数:star:62.1k
Github 地址:https://github.com/PanJiaChen/vue-element-admin
Demo体验:https://panjiachen.github.io/vue-element-admin/#/dashboard
官方文档:https://panjiachen.github.io/vue-element-admin-site/zh/

今天就来说一下,怎么使用Tree Table 树形表格。废话不多说,首先看一下官方文档吧,看看官方文档是怎么给到的例子吧:
组件:https://element.eleme.cn/#/zh-CN/component/tree

分别通过default-expanded-keys和default-checked-keys设置默认展开和默认选中的节点。需要注意的是,此时必须设置node-key,其值为节点数据中的一个字段名,该字段在整棵树中是唯一的。

静态
在文档上选一颗自己想要的树的demo


   
  1. <template>
  2. <div class="grid-content bg-purple">
  3. <el-tree
  4. :data="data"
  5. show-checkbox
  6. node-key="id"
  7. :default-expanded-keys="[2, 3 ]"
  8. :default-checked-keys="[5]"
  9. >
  10. </el-tree>
  11. </div>
  12. </template>
  13. <script>
  14. export default {
  15. data() {
  16. return {
  17. data: [
  18. {
  19. id: 1,
  20. label: "一级 2",
  21. children: [
  22. {
  23. id: 3,
  24. label: "二级 2-1",
  25. children: [
  26. {
  27. id: 4,
  28. label: "三级 3-1-1",
  29. },
  30. {
  31. id: 5,
  32. label: "三级 3-1-2",
  33. disabled: true,
  34. },
  35. ],
  36. },
  37. {
  38. id: 2,
  39. label: "二级 2-2",
  40. disabled: true,
  41. children: [
  42. {
  43. id: 6,
  44. label: "三级 3-2-1",
  45. },
  46. {
  47. id: 7,
  48. label: "三级 3-2-2",
  49. disabled: true,
  50. },
  51. ],
  52. },
  53. ],
  54. },
  55. ],
  56. defaultProps: {
  57. children: "children",
  58. label: "label",
  59. },
  60. } ;
  61. },
  62. } ;
  63. </script>

效果是这个样子的

在实际应用之中,需要从后端请求过来json数据,然后再对json数据进行一些处理,渲染在界面。
我这里有一些json数据,但是并不是我想要的格式,并不是以上代码中的格式,需要做一些处理,对json的格式的转化。

json数据


   
  1. {
  2. "msg": "success",
  3. "code": 1,
  4. "data": [{
  5. "id": 1,
  6. "organName": "住部门",
  7. "parentId": null,
  8. "manager": "zs",
  9. "phone": null,
  10. "companyId": 1,
  11. "address": null
  12. }, {
  13. "id": 19,
  14. "organName": "uiui",
  15. "parentId": 1,
  16. "manager": "iuui",
  17. "phone": "ui",
  18. "companyId": 1,
  19. "address": null
  20. }, {
  21. "id": 20,
  22. "organName": "3223",
  23. "parentId": 19,
  24. "manager": "32",
  25. "phone": "3232",
  26. "companyId": 1,
  27. "address": null
  28. }, {
  29. "id": 22,
  30. "organName": "测试部",
  31. "parentId": 1,
  32. "manager": "李云泥",
  33. "phone": "18117166505",
  34. "companyId": 1,
  35. "address": null
  36. }, {
  37. "id": 23,
  38. "organName": "123",
  39. "parentId": 19,
  40. "manager": "1",
  41. "phone": "12",
  42. "companyId": 1,
  43. "address": null
  44. }, {
  45. "id": 24,
  46. "organName": "www",
  47. "parentId": 19,
  48. "manager": "",
  49. "phone": "",
  50. "companyId": 1,
  51. "address": null
  52. }]
  53. }

动态demo如下:


   
  1. <template>
  2. <div class="grid-content bg-purple">
  3. <el-tree
  4. : data= "treeData"
  5. show-checkbox
  6. default-expand-all
  7. node-key= "id"
  8. ref= "tree"
  9. highlight-current
  10. :props= "defaultProps"
  11. >
  12. </el-tree>
  13. </div>
  14. </template>
  15. <script>
  16. //调用接口
  17. import { getQuerycheckList } from "@/api/data/organ";
  18. export default {
  19. data() {
  20. return {
  21. setTree: [],
  22. defaultProps: {
  23. children: "children",
  24. label: "organName",
  25. },
  26. treeData: [],
  27. organList: [],
  28. };
  29. },
  30. created() {
  31. //加载部门管理节点接口定义
  32. this.getQuerycheckList();
  33. },
  34. methods: {
  35. //部门管理节点接口定义
  36. getQuerycheckList() {
  37. const params = {};
  38. this.dataLoading = true;
  39. getQuerycheckList(params).then((res) => {
  40. this.setTree = res. data;
  41. this.organList = res. data.map((a) => ({
  42. label: a.organName,
  43. value: a.id,
  44. }));
  45. //debugger;
  46. this.getListData();
  47. this.dataLoading = false;
  48. });
  49. },
  50. //对json的格式的转化
  51. getListData() {
  52. let dataArray = [];
  53. this.setTree.forEach(function ( data) {
  54. // debugger;
  55. let parentId = data.parentId;
  56. if (parentId === null) {
  57. let objTemp = {
  58. id: data.id,
  59. organName: data.organName,
  60. manager: data.manager,
  61. phone: data.manager,
  62. parentId: parentId,
  63. };
  64. dataArray.push(objTemp);
  65. }
  66. });
  67. this.treeData = this.data2treeDG( this.setTree, dataArray);
  68. },
  69. data2treeDG(datas, dataArray) {
  70. for (let j = 0; j < dataArray.length; j++) {
  71. let dataArrayIndex = dataArray[j];
  72. let childrenArray = [];
  73. let Id = dataArrayIndex.id;
  74. for (let i = 0; i < datas.length; i++) {
  75. let data = datas[i];
  76. let parentId = data.parentId;
  77. if (parentId == Id) {
  78. //判断是否为儿子节点
  79. let objTemp = {
  80. id: data.id,
  81. organName: data.organName,
  82. manager: data.manager,
  83. phone: data.phone,
  84. parentId: parentId,
  85. };
  86. childrenArray.push(objTemp);
  87. }
  88. }
  89. dataArrayIndex.children = childrenArray;
  90. if (childrenArray.length > 0) {
  91. this.data2treeDG(datas, childrenArray);
  92. }
  93. }
  94. return dataArray;
  95. },
  96. },
  97. };
  98. </script>

转载:https://blog.csdn.net/qq_36538012/article/details/116229417
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场