orgTree.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <template>
  2. <div v-loading="state.loading">
  3. <div style="text-align: right">
  4. <div style="margin-right: 10px"><el-switch v-model="state.horizontal"></el-switch> 横向/纵向</div>
  5. </div>
  6. <div style="height: 500px">
  7. <vue3-tree-org
  8. :data="state.orgData"
  9. :props="{ id: 'id', pid: 'pid', label: 'name', expand: 'expand', children: 'children' }"
  10. :label-style="state.style"
  11. :default-expand-level="100"
  12. :horizontal="state.horizontal"
  13. :collapsable="state.collapsable"
  14. :only-one-node="state.onlyOneNode"
  15. :clone-node-drag="state.cloneNodeDrag"
  16. :node-draggable="state.nodeDraggable"
  17. style="background-color: var(--el-bg-color)"
  18. >
  19. <template v-slot="{ node }">
  20. <div class="tree-org-node__text node-label">
  21. <div class="node-title">{{ node.label }}</div>
  22. <div class="node-id">编码:{{ node.id }}</div>
  23. </div>
  24. </template>
  25. <template v-slot:expand="{ node }">
  26. <div>{{ node.children.length }}</div>
  27. </template>
  28. </vue3-tree-org>
  29. </div>
  30. </div>
  31. </template>
  32. <script lang="ts" setup name="orgTree">
  33. import { onMounted, reactive } from 'vue';
  34. import { storeToRefs } from 'pinia';
  35. import { useUserInfo } from '/@/stores/userInfo';
  36. import { getAPI } from '/@/utils/axios-utils';
  37. import { SysUserApi } from '/@/api-services/api';
  38. const stores = useUserInfo();
  39. const { userInfos } = storeToRefs(stores);
  40. const currentNodeStyle = { color: '#FFFFFF', background: '#3B3B3B' };
  41. const state = reactive({
  42. loading: false,
  43. orgData: [] as any,
  44. horizontal: false,
  45. collapsable: true,
  46. onlyOneNode: false,
  47. cloneNodeDrag: false,
  48. nodeDraggable: false,
  49. style: {
  50. background: 'var(--el-color-primary)', //'#FF5C00',
  51. color: '#FFFFFF',
  52. },
  53. });
  54. onMounted(async () => {
  55. state.loading = true;
  56. var res = await getAPI(SysUserApi).apiSysUserOrgInfoGet();
  57. var d = res.data.result ?? [];
  58. state.orgData = d[0] ?? []; // 默认第一个树分支
  59. if (state.orgData.id == userInfos.value.orgId) state.orgData.style = currentNodeStyle;
  60. else InitOrg(state.orgData.children, userInfos.value.orgId);
  61. state.loading = false;
  62. });
  63. // 递归遍历
  64. const InitOrg = (orgData: any, id: any) => {
  65. if (orgData && orgData.length > 0) {
  66. orgData.forEach(function (u: any) {
  67. if (u.id == id) {
  68. u.style = currentNodeStyle;
  69. return;
  70. } else {
  71. InitOrg(u.children, id);
  72. }
  73. });
  74. }
  75. };
  76. </script>
  77. <style lang="scss" scoped>
  78. .tree-org-node__text {
  79. // text-align: left;
  80. font-size: 14px;
  81. .node-title {
  82. padding-bottom: 8px;
  83. margin-bottom: 8px;
  84. border-bottom: 1px solid currentColor;
  85. }
  86. .node-id {
  87. font-size: 10px;
  88. }
  89. }
  90. </style>