index.vue 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <template>
  2. <a-tree-select
  3. v-model="selected"
  4. :placeholder="placeholder"
  5. :allow-clear="clearable"
  6. :disabled="disabled"
  7. :data="optionList"
  8. :field-names="fieldNames"
  9. @change="onChange"
  10. >
  11. </a-tree-select>
  12. </template>
  13. <script setup lang="ts">
  14. import { ref, watch } from 'vue';
  15. import { omit } from 'lodash';
  16. import { organizationList } from '@/api/base';
  17. import { OrgTreeItem, OrgItem } from '@/api/types/base';
  18. defineOptions({
  19. name: 'SelectOrg',
  20. });
  21. const props = defineProps<{
  22. modelValue: string;
  23. clearable?: boolean;
  24. disabled?: boolean;
  25. placeholder?: string;
  26. multiple?: boolean;
  27. }>();
  28. const emit = defineEmits(['update:modelValue', 'change']);
  29. const fieldNames = {
  30. key: 'id',
  31. title: 'name',
  32. };
  33. const selected = ref('');
  34. const optionList = ref<OrgTreeItem[]>([]);
  35. const search = async () => {
  36. optionList.value = [];
  37. const resData = await organizationList();
  38. optionList.value = resData || [];
  39. };
  40. search();
  41. const getDataByIds = (ids: string[]): OrgItem[] => {
  42. const datas: OrgItem[] = [];
  43. const getData = (list: OrgTreeItem[]) => {
  44. list.forEach((item) => {
  45. if (ids.includes(item.id)) {
  46. datas.push(omit(item, ['children']));
  47. }
  48. });
  49. };
  50. getData(optionList.value);
  51. return datas;
  52. };
  53. const onChange = () => {
  54. const selectedData = props.multiple
  55. ? getDataByIds(selected.value)
  56. : getDataByIds([selected.value]);
  57. emit('update:modelValue', selected.value);
  58. emit('change', props.multiple ? selectedData : selectedData[0]);
  59. };
  60. watch(
  61. () => props.modelValue,
  62. (val) => {
  63. selected.value = val;
  64. },
  65. {
  66. immediate: true,
  67. }
  68. );
  69. </script>