org-menu.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <template>
  2. <el-card shadow="never" style="margin-top: 8px" body-style="padding:0px;" class="my-fill">
  3. <template #header>
  4. <el-input v-model="state.filterText" placeholder="筛选部门" clearable />
  5. </template>
  6. <el-scrollbar v-loading="state.loading" height="100%" max-height="100%" :always="false" wrap-style="padding:var(--el-card-padding)">
  7. <el-tree
  8. ref="orgMenuRef"
  9. :data="state.orgTreeData"
  10. node-key="id"
  11. :props="{ children: 'children', label: 'name' }"
  12. :filter-node-method="onFilterNode"
  13. highlight-current
  14. check-strictly
  15. default-expand-all
  16. render-after-expand
  17. :expand-on-click-node="false"
  18. v-bind="$attrs"
  19. @node-click="onNodeClick"
  20. @check-change="onCheckChange"
  21. />
  22. </el-scrollbar>
  23. </el-card>
  24. </template>
  25. <script lang="ts" setup name="admin/org/menu">
  26. import { onMounted, reactive, ref, watch, nextTick } from 'vue'
  27. import { OrgListOutput } from '/@/api/admin/data-contracts'
  28. import { OrgApi } from '/@/api/admin/Org'
  29. import { listToTree } from '/@/utils/tree'
  30. import { ElTree } from 'element-plus'
  31. interface Props {
  32. modelValue: number[] | null | undefined
  33. selectFirstNode: boolean
  34. }
  35. const props = withDefaults(defineProps<Props>(), {
  36. modelValue: () => [],
  37. selectFirstNode: false,
  38. })
  39. const orgMenuRef = ref<InstanceType<typeof ElTree>>()
  40. const state = reactive({
  41. loading: false,
  42. filterText: '',
  43. orgTreeData: [] as Array<OrgListOutput>,
  44. lastKey: 0,
  45. })
  46. watch(
  47. () => state.filterText,
  48. (val) => {
  49. orgMenuRef.value?.filter(val)
  50. }
  51. )
  52. onMounted(() => {
  53. initData()
  54. })
  55. const emits = defineEmits<{
  56. (e: 'node-click', node: OrgListOutput | null): void
  57. (e: 'update:modelValue', node: any[] | undefined | null): void
  58. }>()
  59. const onFilterNode = (value: string, data: OrgListOutput) => {
  60. if (!value) return true
  61. return data.name?.indexOf(value) !== -1
  62. }
  63. const onNodeClick = (node: OrgListOutput) => {
  64. if (state.lastKey === node.id) {
  65. state.lastKey = 0
  66. orgMenuRef.value?.setCurrentKey(undefined)
  67. emits('node-click', null)
  68. } else {
  69. state.lastKey = node.id as number
  70. emits('node-click', node)
  71. }
  72. }
  73. const onCheckChange = () => {
  74. emits('update:modelValue', orgMenuRef.value?.getCheckedKeys())
  75. }
  76. const initData = async () => {
  77. state.loading = true
  78. const res = await new OrgApi().getList().catch(() => {
  79. state.loading = false
  80. })
  81. state.loading = false
  82. if (res?.success && res.data && res.data.length > 0) {
  83. state.orgTreeData = listToTree(res.data)
  84. if (state.orgTreeData.length > 0 && props.selectFirstNode) {
  85. nextTick(() => {
  86. const firstNode = state.orgTreeData[0]
  87. orgMenuRef.value?.setCurrentKey(firstNode.id)
  88. emits('node-click', firstNode)
  89. })
  90. }
  91. } else {
  92. state.orgTreeData = []
  93. }
  94. }
  95. defineExpose({
  96. orgMenuRef,
  97. })
  98. </script>