TestTree.vue 2.3 KB

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