TestTree.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <template>
  2. <ElTree ref="treeRef" :class="ui" :data="treeData" :default-expanded-keys="['1570335808521502732']" :indent="0"
  3. :props="treeProps" accordion
  4. auto-expand-parent class="hc-tree-node tree-line el-radio-group" current-node-key="1570335808521502732" highlight-current
  5. node-key="primaryKeyId" @node-click="treeClick">
  6. <template #default="{ node, data }">
  7. <div :id="`${idPrefix}${data['primaryKeyId']}`" class="data-custom-tree-node">
  8. <div class="label">{{ node.label }}</div>
  9. </div>
  10. </template>
  11. </ElTree>
  12. </template>
  13. <script setup>
  14. import {ref, nextTick, watch} from "vue";
  15. import {getArrValue} from "js-fast-way"
  16. import {getProjectTree} from "~api/tentative";
  17. //参数
  18. const props = defineProps({
  19. projectId: {
  20. type: [String, Number],
  21. default: ''
  22. },
  23. contractId: {
  24. type: [String, Number],
  25. default: ''
  26. },
  27. wbsId: {
  28. type: [String, Number],
  29. default: ''
  30. },
  31. wbsType: {
  32. type: [String, Number],
  33. default: ''
  34. },
  35. idPrefix: {
  36. type: String,
  37. default: 'test-tree1-'
  38. },
  39. ui: {
  40. type: String,
  41. default: ''
  42. },
  43. })
  44. //变量
  45. const treeRef = ref(null)
  46. const treeProps = ref({
  47. label: 'title',
  48. children: 'children',
  49. isLeaf: 'hasChildren'
  50. })
  51. const treeData = ref([])
  52. const projectId = ref(props.projectId);
  53. const contractId = ref(props.contractId);
  54. const wbsId = ref(props.wbsId);
  55. //const tenantId = ref(props.tenantId);
  56. //监听
  57. watch(() => [
  58. props.projectId,
  59. props.contractId,
  60. props.wbsId,
  61. ], ([pid, cid, wbs_id]) => {
  62. projectId.value = pid
  63. contractId.value = cid
  64. wbsId.value = wbs_id
  65. })
  66. //加载中
  67. nextTick(() => {
  68. getTreeData()
  69. })
  70. //获取数据
  71. const isLoading = ref(false)
  72. const getTreeData = async () => {
  73. isLoading.value = true
  74. const {data} = await getProjectTree({
  75. projectId: projectId.value,
  76. // contractId: contractId.value,
  77. wbsId: wbsId.value,
  78. })
  79. //处理数据
  80. isLoading.value = false
  81. treeData.value = getArrValue(data)
  82. //最后一个,执行点击
  83. /*if (clickKey) {
  84. await nextTick(() => {
  85. document.getElementById(`${idPrefix.value}${clickKey}`)?.click()
  86. })
  87. }*/
  88. }
  89. //事件
  90. const emit = defineEmits(['nodeTap'])
  91. //节点被点击
  92. const treeClick = async (data) => {
  93. emit('nodeTap', data)
  94. }
  95. </script>
  96. <style lang="scss" scoped>
  97. @import "../../../../styles/app/tree.scss";
  98. </style>