123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- <template>
- <t-select
- v-model="selected"
- filterable
- clearable
- v-bind="attrs"
- @change="onChange"
- >
- <t-option
- v-for="item in optionList"
- :key="item.id"
- :value="item.id"
- :label="item.name"
- />
- </t-select>
- </template>
- <script setup name="SelectCustomer">
- import { onMounted, ref, useAttrs, watch } from 'vue';
- import { customerListApi } from '@/api/system';
- let optionList = ref([]);
- let selected = ref('');
- const attrs = useAttrs();
- const emit = defineEmits(['update:modelValue', 'change']);
- const props = defineProps({
- modelValue: { type: [Number, String, Array], default: '' },
- type: { type: String, default: '' },
- typeRequired: { type: Boolean, default: false },
- });
- const search = async () => {
- if (props.typeRequired && !props.type) return;
- optionList.value = [];
- let data = { enable: true };
- if (props.type) data.type = props.type;
- const res = await customerListApi(data).catch(() => {});
- if (!res) return;
- optionList.value = res;
- };
- const onChange = () => {
- const isMultiple = Object.prototype.hasOwnProperty.call(attrs, 'multiple');
- const selectedData = isMultiple
- ? optionList.value.filter(
- (item) => selected.value && selected.value.includes(item.id)
- )
- : optionList.value.filter((item) => selected.value === item.id);
- emit('update:modelValue', selected.value);
- emit('change', isMultiple ? selectedData : selectedData[0]);
- };
- onMounted(() => {
- search();
- });
- watch(
- () => props.modelValue,
- (val) => {
- selected.value = val;
- }
- );
- watch(
- () => props.type,
- (val, oldval) => {
- if (val !== oldval) search();
- }
- );
- </script>
|