html
复制代码
<template>
<div class="t-select">
<van-field v-bind="fieldAttrs" :value="text" @click="clickField" />
<van-popup v-model="isShow" position="bottom">
<van-field
v-model="searchVal"
:placeholder="searchPlaceholder"
@input="search"
v-if="isSearch"
clearable
input-align="left"
/>
<van-picker
v-on="$listeners"
v-bind="$attrs"
ref="picker"
@change="changeVal"
:value-key="labelKey"
:columns="columnsData"
show-toolbar
@confirm="onConfirm"
@cancel="isShow = false"
/>
</van-popup>
</div>
</template>
<script>
export default {
name: 'TSelect',
props: {
// 下拉数据源
list: {
type: Array,
default: () => []
},
val: { default: null },
// 下拉选择传给后台的key
valueKey: {
type: String,
default: 'key',
},
// 下拉选择显示label
labelKey: {
type: String,
default: 'label',
},
// 是否显示搜索框
isSearch: {
type: Boolean,
default: false
},
// van-field显示的文本
showLabel: {
type: String
},
searchPlaceholder: {
type: String,
default: '请输入搜索内容'
}
},
model: {
prop: 'val',
event: 'emitVal'
},
data() {
return {
isShow: false,
columnsData: this.list,
searchVal: '',
text: '',
}
},
watch: {
val: {
handler(newVal) {
// 赋值回显
if (newVal) {
const findItem = this.list.find((item) => item[this.valueKey] == newVal)
this.text = findItem[this.showLabel || this.labelKey]
}
},
deep: true,
immediate: true
},
list: {
handler(newVal) {
this.columnsData = newVal
},
deep: true,
immediate: true
}
},
computed: {
fieldAttrs() {
return {
readonly: true,
clickable: true,
...this.$attrs
}
}
},
methods: {
clickField() {
this.isShow = true
this.$emit('clickField', true)
},
onConfirm(value) {
// console.log('点击确定', value)
this.$emit('emitVal', value[this.valueKey], value)
this.text = value[this.showLabel || this.labelKey]
this.isShow = false
},
changeVal() {
this.$emit('change')
},
clear() {
this.text = ''
this.$emit('emitVal', null)
},
// 搜索
search(val) {
// console.log('搜索', val)
if (val) {
this.columnsData = this.columnsData.filter(item => {
return item[this.labelKey].indexOf(val) > -1
})
} else {
this.columnsData = JSON.parse(JSON.stringify(this.list))
}
},
}
}
</script>