Files
pj_admin_cloud/src/components/Select/SelectHard.vue
T
2025-04-03 15:05:03 +08:00

116 lines
2.2 KiB
Vue

<template>
<el-select
style="width: 100%"
v-model="state.selectId"
:placeholder="props.placeholder"
:clearable="props.clearable"
remote-show-suffix
filterable
remote
:disabled="props.disabled"
:remote-method="remoteSearch"
:loading="state.loading"
@visible-change="(e: boolean) => e && getOptions()"
@change="change"
>
<el-option v-for="v in state.options" :key="v.id" :label="`${v.name}`" :value="v.id">
<div class="flex items-center">
<div class="color-primary">{{ v.name }}</div>
<div class="flex items-center justify-center ml5">
<dict-tag :options="state.hardType" :value="v.type" />
</div>
</div>
</el-option>
</el-select>
</template>
<script setup lang="ts" name="select-hard">
import { reactive } from "vue"
import { useDict } from "/@/hooks/dict"
import * as hardManager from "/@/api/hard/hard-manager"
const emit = defineEmits(["change"])
const selectId = defineModel()
const props = withDefaults(
defineProps<{
placeholder?: string
disabled?: boolean
clearable?: boolean
autoFirst?: boolean
apiParams?: any
}>(),
{
placeholder: "请选择",
disabled: false,
clearable: true,
autoFirst: false,
apiParams: {},
},
)
const queryForm = reactive<any>({
name: "",
current: 1,
size: 10,
})
const state = reactive<any>({
loading: true,
selectId: selectId,
options: [],
hardType: [],
})
watch(
selectId,
(newVal, oldValue) => {
if (newVal == oldValue) return
state.selectId = newVal
getOptions()
},
{ immediate: true },
)
async function getOptions() {
try {
state.loading = true
const _pa = {
...queryForm,
...props.apiParams,
}
const { data } = await hardManager.select(_pa)
state.options = data.records ?? []
if (props.autoFirst && state.options.length != 0) {
state.selectId = state.options[0].id
emit("change", state.selectId)
}
const { hard_type } = useDict("hard_type")
state.hardType = hard_type
} finally {
setTimeout(() => (state.loading = false), 500)
}
}
function remoteSearch(params: string) {
if (params != queryForm.name) {
queryForm.name = params
getOptions()
}
}
function clear() {
state.selectId = null
getOptions()
}
function change() {
emit("change", selectId)
}
onMounted(() => {
props.autoFirst && getOptions()
})
</script>