<template>
<div>
<el-select v-model="city" filterable clearable multiple collapse-tags :filter-method="dataFilter" placeholder="请选择">
<el-checkbox v-model="allCheckList" :disabled="checkAllIsShow" class="el-select-dropdown__item el-select-allTop">全选</el-checkbox>
<div class="allPosition">
<el-option v-for="item in cityList" :key="item.value" :label="item.label" :value="item.value">
<span style="padding-left:20px;">{{ item.label }}</span>
</el-option>
</div>
</el-select>
</div>
</template>
<script>
export default {
data() {
return {
city: [],
cityList: [], //存储自定义城市搜索数据
cityListCopy: [], //存储原始City数据
checkAllIsShow: false //自定义搜索时-限制全选
}
},
computed: {
allCheckList: {
get() {
return this.cityList.every(item => this.city.indexOf(item.value) !== -1)
},
set(value) {
this.city = []
if (value) {
this.cityList.forEach(item => {
this.city.push(item.value)
})
}
}
}
},
created() {
let arr = [
{
cityId: '-1',
cityName: '全国'
},
{
cityId: '1',
cityName: '北京'
},
{
cityId: '2',
cityName: '上海'
},
{
cityId: '3',
cityName: '太原'
},
{
cityId: '4',
cityName: '新乡'
},
{
cityId: '5',
cityName: '永安'
}
]
//模拟请求
setTimeout(() => {
this.cityList = arr.map(item => {
return {
label: item.cityName,
value: Number(item.cityId)
}
})
this.cityListCopy = arr.map(item => {
return {
label: item.cityName,
value: Number(item.cityId)
}
})
}, 1000)
},
methods: {
//自定义搜索方法
dataFilter(val) {
if (val) {
this.checkAllIsShow = true
this.cityList = this.cityList.filter(item => {
return item.label.indexOf(val) !== -1
})
} else {
this.checkAllIsShow = false
this.cityList = this.cityListCopy
}
return true
}
}
}
</script>
<style scoped>
.el-select-allTop {
width: 100%;
border-bottom: 1px solid rgba(204, 204, 204, 0.596);
}
.allPosition {
overflow: auto;
height: 200px;
}
</style>
结果展示