index.vue 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <template>
  2. <t-select
  3. v-model="selected"
  4. filterable
  5. clearable
  6. v-bind="attrs"
  7. @change="onChange"
  8. >
  9. <t-option
  10. v-for="item in optionList"
  11. :key="item.id"
  12. :value="item.id"
  13. :label="item.name"
  14. />
  15. </t-select>
  16. </template>
  17. <script setup name="SelectSupplier">
  18. import { onMounted, ref, useAttrs, watch, computed } from 'vue';
  19. import { getAllRoleList } from '@/api/user';
  20. let optionList = ref([]);
  21. let selected = ref('');
  22. const attrs = useAttrs();
  23. const emit = defineEmits(['update:modelValue', 'change']);
  24. const props = defineProps({
  25. modelValue: { type: [Number, String, Array], default: '' },
  26. });
  27. const isMultiple = computed(() => {
  28. const multiple = attrs.multiple;
  29. return multiple === '' || multiple;
  30. });
  31. const search = async () => {
  32. optionList.value = [];
  33. const res = await getAllRoleList().catch(() => {});
  34. if (!res) return;
  35. optionList.value = res;
  36. };
  37. const onChange = () => {
  38. const selectedData = isMultiple.value
  39. ? optionList.value.filter(
  40. (item) => selected.value && selected.value.includes(item.id)
  41. )
  42. : optionList.value.filter((item) => selected.value === item.id);
  43. emit('update:modelValue', selected.value);
  44. emit('change', isMultiple.value ? selectedData : selectedData[0]);
  45. };
  46. onMounted(() => {
  47. search();
  48. });
  49. watch(
  50. () => props.modelValue,
  51. (val) => {
  52. selected.value = val;
  53. },
  54. {
  55. immediate: true,
  56. }
  57. );
  58. </script>