Files
biz-pc-cloud-zax/src/components/Select/SelectUser.vue
T
2025-02-17 15:25:30 +08:00

84 lines
1.6 KiB
Vue

<template>
<el-select
style="width: 100%"
v-model="state.selectId"
:placeholder="props.placeholder"
clearable
remote-show-suffix
filterable
remote
:remote-method="remoteSearch"
:loading="state.loading"
@visible-change="(e: boolean) => e && getOptions()"
>
<el-option v-for="v in state.options" :key="v.userId" :label="`${v.name}-${v.phone}`" :value="v.userId">
<div class="flex items-center">
<div class="color-primary">{{ v.name }}</div>
<div class="pl5 color-success">{{ v.phone }}</div>
</div>
</el-option>
</el-select>
</template>
<script setup lang="ts" name="select-user">
import { reactive } from "vue"
import * as userApi from "/@/api/admin/user"
const selectId = defineModel()
export type Props = {
placeholder?: string
}
const props = withDefaults(defineProps<Props>(), {
placeholder: "输入姓名、手机号",
})
const queryForm = reactive<any>({
nameOrPhone: "",
current: 1,
size: 10,
})
const state = reactive<any>({
loading: true,
selectId: selectId,
options: [],
})
watch(
selectId,
(newVal, oldValue) => {
if (newVal == oldValue) return
state.selectId = newVal
getOptions()
},
{ immediate: true },
)
async function getOptions() {
try {
state.loading = true
const _pa = {
userId: selectId.value,
...queryForm,
}
const { data } = await userApi.pageList(_pa)
state.options = data.records ?? []
} finally {
setTimeout(() => (state.loading = false), 500)
}
}
function remoteSearch(params: string) {
if (params != queryForm.nameOrPhone) {
queryForm.nameOrPhone = params
getOptions()
}
}
function clear() {
state.selectId = null
getOptions()
}
</script>