DivisionTree1.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <template>
  2. <ElTree class="hc-tree-node tree-line" ref="ElTreeRef" :props="ElTreeProps" :load="ElTreeLoadNode" lazy highlight-current accordion node-key="primaryKeyId"
  3. show-checkbox :default-expanded-keys="defaultExpandedCids" @node-click="ElTreeClick" :indent="0">
  4. <template #default="{ node, data }">
  5. <div class="data-custom-tree-node" :id="`${idPrefix}${data['primaryKeyId']}`">
  6. <div class="label division" :class="node.level === 1?'level-name':node.level">{{ node.label }}</div>
  7. </div>
  8. </template>
  9. </ElTree>
  10. </template>
  11. <script setup>
  12. import {ref,nextTick,watch} from "vue";
  13. import dataFillQuery from '~api/data-fill/query';
  14. import {isItem,getArrValue,getObjValue} from "vue-utils-plus"
  15. //参数
  16. const props = defineProps({
  17. projectId: {
  18. type: [String,Number],
  19. default: ''
  20. },
  21. contractId: {
  22. type: [String,Number],
  23. default: ''
  24. },
  25. autoExpandKeys: {
  26. type: Array,
  27. default: () => ([])
  28. },
  29. idPrefix: {
  30. type: String,
  31. default: 'division-tree-'
  32. },
  33. isAutoClick: {
  34. type: Boolean,
  35. default: true
  36. }
  37. })
  38. //变量
  39. const ElTreeRef = ref(null)
  40. const ElTreeProps = ref({
  41. label: 'title',
  42. children: 'children',
  43. isLeaf: 'notExsitChild'
  44. })
  45. const TreeExpandKey = ref(props.autoExpandKeys)
  46. const projectId = ref(props.projectId);
  47. const contractId = ref(props.contractId);
  48. const idPrefix = ref(props.idPrefix);
  49. //监听
  50. watch(() => [
  51. props.autoExpandKeys,
  52. props.projectId,
  53. props.contractId,
  54. props.idPrefix,
  55. ], ([expandKeys, UserProjectId, UserContractId,UserIdPrefix]) => {
  56. TreeExpandKey.value = expandKeys
  57. projectId.value = UserProjectId
  58. contractId.value = UserContractId
  59. idPrefix.value = UserIdPrefix
  60. })
  61. //事件
  62. const emit = defineEmits(['nodeTap', 'nodeLoading', 'relationTap'])
  63. //树形结构异步加载数据
  64. const defaultExpandedCids = ref([])
  65. const ElTreeLoadNode = async (node, resolve) => {
  66. let contractIdRelation = '', parentId = '', primaryKeyId = '';
  67. if (node.level !== 0) {
  68. const nodeData = getObjValue(node?.data);
  69. contractIdRelation = nodeData?.contractIdRelation || ''
  70. parentId = contractIdRelation ? nodeData?.primaryKeyId : nodeData?.id
  71. primaryKeyId = nodeData?.id || ''
  72. }
  73. //获取数据
  74. const {error, code, data} = await dataFillQuery.queryWbsTreeData({
  75. contractId: contractId.value || '',
  76. contractIdRelation,
  77. primaryKeyId,
  78. parentId
  79. })
  80. //处理数据
  81. if (!error && code === 200) {
  82. let clickKey = '', defaultExpandedArr = [];
  83. const keys = TreeExpandKey.value || []
  84. const resData = getArrValue(data)
  85. if (keys.length > 0) {
  86. let lastKey = keys[keys.length-1];
  87. for (const item of resData) {
  88. //自动展开
  89. if (isItem(keys,item?.primaryKeyId)) {
  90. defaultExpandedArr.push(item?.primaryKeyId)
  91. }
  92. //最后一个,选中点击
  93. if (item?.primaryKeyId === lastKey) {
  94. clickKey = item?.primaryKeyId
  95. }
  96. }
  97. } else if (node.level === 0) {
  98. defaultExpandedArr.push(resData[0]?.primaryKeyId)
  99. }
  100. //自动展开
  101. defaultExpandedCids.value = defaultExpandedArr
  102. if (node.level === 0) {
  103. emit('nodeLoading')
  104. }
  105. resolve(resData)
  106. //最后一个,执行点击
  107. if (props.isAutoClick && clickKey) {
  108. await nextTick(() => {
  109. document.getElementById(`${idPrefix.value}${clickKey}`)?.click()
  110. })
  111. }
  112. } else {
  113. if (node.level === 0) {
  114. emit('nodeLoading')
  115. }
  116. resolve([])
  117. }
  118. }
  119. //节点被点击
  120. const ElTreeClick = async (data,node) => {
  121. emit('nodeTap', {node, data})
  122. }
  123. //鼠标右键事件
  124. const ElTreeRelationClick = (e,data,node) => {
  125. if (node.level !== 1) {
  126. e.preventDefault();
  127. emit('relationTap', {node, data})
  128. }
  129. }
  130. </script>
  131. <style lang="scss" scoped>
  132. @import "../../../styles/app/tree.scss";
  133. </style>
  134. <style lang="scss">
  135. .hc-tree-node .el-tree-node__label {
  136. flex: 1;
  137. }
  138. </style>