el-select数据量大或可搜索
第一种 点击搜索
<el-select class="mr10" style="width:150px;" v-model="valueType" placeholder="请选择考核类型" :loading="selectLoading" > <el-input v-model="searchSelect" size="small" placeholder="请输入搜索内容"> <el-button slot="append" icon="el-icon-search" @click="searchSelectGet"></el-button> </el-input> <el-option label="考核类型" :value="0"></el-option> <el-option v-for="item in optionsType" :key="item.key" :label="item.value" :value="item.key" ></el-option> </el-select>
searchSelectGet(){
模拟异步请求 let arr=[...this.optionsType] arr.splice(0,1); this.selectLoading = true; setTimeout(() => { this.selectLoading = false; this.optionsType=arr; }, 2000); }
第二种 远程搜索
<template> <el-select v-model="value" multiple filterable remote reserve-keyword placeholder="请输入关键词" :remote-method="remoteMethod" :loading="loading"> <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"> </el-option> </el-select> </template> <script> export default { data() { return { options: [], value: [], list: [], loading: false, states: ["Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado","Wyoming"] } }, mounted() { this.list = this.states.map(item => { return { value: item, label: item }; }); }, methods: { remoteMethod(query) { if (query !== '') { this.loading = true; setTimeout(() => { this.loading = false; this.options = this.list.filter(item => { return item.label.toLowerCase() .indexOf(query.toLowerCase()) > -1; }); }, 200); } else { this.options = []; } } } } </script>
第三种懒加载
<template> <el-select v-model="value" placeholder="请选择" filterable multiple v-el-select-loadmore="loadmore" > <el-option v-for="item in options" :key="item.id" :label="item.label" :value="item.id"> </el-option> </el-select> </template> export default { directives: { 'el-select-loadmore': { bind(el, binding) { // 获取element-ui定义好的scroll盒子 const SELECTWRAP_DOM = el.querySelector('.el-select-dropdown .el-select-dropdown__wrap'); SELECTWRAP_DOM.addEventListener('scroll', function () { /** * scrollHeight 获取元素内容高度(只读) * scrollTop 获取或者设置元素的偏移值,常用于, 计算滚动条的位置, 当一个元素的容器没有产生垂直方向的滚动条, 那它的scrollTop的值默认为0. * clientHeight 读取元素的可见高度(只读) * 如果元素滚动到底, 下面等式返回true, 没有则返回false: * ele.scrollHeight - ele.scrollTop === ele.clientHeight; */ const condition = this.scrollHeight - this.scrollTop <= this.clientHeight; if (condition) { binding.value(); } }); } } }, data() { return { value: '', options: [], formData: { pageIndex: 1, pageSize: 20, } }; }, mounted() { this.getList(this.formData); }, methods: { loadmore() { this.formData.pageIndex++; this.getList(this.formData); }, getList(formData) { // 这里是接口请求数据, 带分页条件 const _res = [1, 2, 3]; // 请求得到的数据 this.options = [...this.options, ..._res]; } } };