TestTree.vue 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <template>
  2. <ElTree ref="ElTreeRef" :class="ui" :default-expanded-keys="defaultExpandedCids" :indent="0" :load="ElTreeLoadNode" :props="ElTreeProps"
  3. accordion class="hc-tree-node tree-line" lazy
  4. node-key="primaryKeyId" show-checkbox @check="ElTreeCheckChange">
  5. </ElTree>
  6. </template>
  7. <script setup>
  8. import {ref, watch} from "vue";
  9. import dataFillQuery from '~api/data-fill/query';
  10. import {getArrValue, getObjValue} from "js-fast-way"
  11. import {useAppStore} from "~src/store";
  12. //参数
  13. const props = defineProps({
  14. projectId: {
  15. type: [String, Number],
  16. default: ''
  17. },
  18. contractId: {
  19. type: [String, Number],
  20. default: ''
  21. },
  22. ui: {
  23. type: String,
  24. default: ''
  25. },
  26. })
  27. //变量
  28. const ElTreeRef = ref(null)
  29. const ElTreeProps = ref({
  30. label: 'title',
  31. children: 'children',
  32. isLeaf: 'notExsitChild'
  33. })
  34. const projectId = ref(props.projectId);
  35. const contractId = ref(props.contractId);
  36. const useAppState = useAppStore()
  37. const contractInfo = ref(useAppState.getContractInfo);
  38. const {contractType} = contractInfo.value;
  39. const classifyType = ref(contractType === 2 ? '2' : '1')
  40. //监听
  41. watch(() => [
  42. props.projectId,
  43. props.contractId
  44. ], ([UserProjectId, UserContractId]) => {
  45. projectId.value = UserProjectId
  46. contractId.value = UserContractId
  47. })
  48. //事件
  49. const emit = defineEmits(['change'])
  50. //树形结构异步加载数据
  51. const defaultExpandedCids = ref([])
  52. const ElTreeLoadNode = async (node, resolve) => {
  53. let defaultExpandedArr = [];
  54. let contractIdRelation = '', parentId = '', primaryKeyId = '';
  55. if (node.level !== 0) {
  56. const nodeData = getObjValue(node?.data);
  57. contractIdRelation = nodeData?.contractIdRelation || ''
  58. parentId = contractIdRelation ? nodeData?.primaryKeyId : nodeData?.id
  59. primaryKeyId = nodeData?.id || ''
  60. }
  61. //获取数据
  62. const {data} = await dataFillQuery.queryWbsTreeData({
  63. contractId: contractId.value || '',
  64. contractIdRelation,
  65. primaryKeyId,
  66. parentId,
  67. classifyType: classifyType.value
  68. })
  69. const resData = getArrValue(data)
  70. if (resData.length > 0 && node.level === 0) {
  71. defaultExpandedArr.push(resData[0]?.primaryKeyId)
  72. }
  73. //自动展开
  74. defaultExpandedCids.value = defaultExpandedArr
  75. resolve(resData)
  76. }
  77. //节点勾选
  78. const ElTreeCheckChange = (data, checkeds) => {
  79. emit('change', {data, checkeds})
  80. }
  81. </script>
  82. <style lang="scss" scoped>
  83. @import "../../../../styles/app/tree.scss";
  84. </style>