testTree.vue 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <template>
  2. <HcLazyTree ref="ElTreeRef"
  3. :autoExpandKeys="autoExpandKeys"
  4. :defaultCheckedKeys="defaultExpandedCids"
  5. :hProps="ElTreeProps"
  6. @load="ElTreeLoadNode"
  7. @nodeTap="ElTreeClick"
  8. />
  9. </template>
  10. <script setup>
  11. import {ref, watch} from "vue";
  12. import {getArrValue} from "js-fast-way"
  13. import samplingApi from "~api/tentative/material/sampling";
  14. //参数
  15. const props = defineProps({
  16. projectId: [String, Number],
  17. contractId: [String, Number],
  18. wbsId: [String, Number],
  19. tenantId: [String, Number],
  20. wbsType: [String, Number],
  21. autoExpandKey: {
  22. type: Array,
  23. default: () => ([])
  24. },
  25. })
  26. //变量
  27. const ElTreeRef = ref(null)
  28. const ElTreeProps = ref({
  29. label: 'title',
  30. children: 'children',
  31. // isLeaf: 'hasChildren'
  32. isLeaf: function (data) {
  33. return !data.hasChildren;
  34. },
  35. })
  36. const projectId = ref(props.projectId);
  37. const contractId = ref(props.contractId);
  38. const wbsTempId = ref(props.wbsId)
  39. const tenant_id = ref(props.tenantId)
  40. const wbs_type = ref(props.wbsType)
  41. const autoExpandKeys = ref(props.autoExpandKey)
  42. //监听
  43. watch(() => [
  44. props.projectId,
  45. props.contractId,
  46. props.wbsId,
  47. props.tenantId,
  48. props.wbsType,
  49. props.autoExpandKey,
  50. ], ([pid, cid, wbs_id, tid, type, keys]) => {
  51. projectId.value = pid
  52. contractId.value = cid
  53. wbsTempId.value = wbs_id
  54. tenant_id.value = tid
  55. wbs_type.value = type
  56. autoExpandKeys.value = keys
  57. })
  58. //事件
  59. const emit = defineEmits(['nodeTap', 'nodeLoading'])
  60. //树形结构异步加载数据
  61. const defaultExpandedCids = ref([])
  62. const ElTreeLoadNode = async ({node, item, level}, resolve) => {
  63. let parentId = '0';
  64. if (level !== 0) {
  65. parentId = item?.id
  66. }
  67. //获取数据
  68. const {data} = await samplingApi.queryLazyTree({
  69. wbsId: wbsTempId.value,
  70. tenantId: tenant_id.value,
  71. projectId: projectId.value,
  72. parentId,
  73. wbsType: wbs_type.value
  74. })
  75. resolve(getArrValue(data))
  76. emit('nodeLoading')
  77. }
  78. //节点被点击
  79. const ElTreeClick = async ({node, data, keys}) => {
  80. emit('nodeTap', {node, data, keys: keys})
  81. }
  82. </script>