解决element-ui el-select数据过大方案

一、背景

项目中需要用到el-select选择医院,全国医院数据量非常大,通过API读取数据页面直接卡死。

 

二、解决方案

1、组件:el-select + vue虚拟滚动(vue-virtual-scroll-list)

2、安装:npm install vue-virtual-scroll-list --save

3、参考:

    NPM地址:https://www.npmjs.com/package/vue-virtual-scroll-list

    官网地址:https://tangbc.github.io/vue-virtual-scroll-list/#/

三:参考代码

###create.vue

<template> <div> <el-form ref="dataForm" :model="form" :rules="rules" label-width="0px"> <el-descriptions :column="3" border> <el-descriptions-item labelStyle="width: 13%" contentStyle="width: 20%"> <template slot="label"><span class="red">*</span> 医院</template> <el-form-item label="" prop="hospital_id"> <el-select @change="changeValue" remote :remote-method="getListHospital" :loading="loading" v-model="form.hospital_id" :placeholder="'请输入医院'" size="small" filterable clearable> <virtual-list style="height: 150px; overflow-y: auto;" :data-key="'id'" :data-sources="hospitalData" :data-component="itemComponent" /> </el-select> </el-form-item> </el-descriptions-item> </el-descriptions> </el-form> </div> </template> <script> import {allHospital, listHospital} from "@/api/system/hospital"; import Item from "./item"; import VirtualList from 'vue-virtual-scroll-list' export default { props: ["rowData", "dialogStatus"], components: { VirtualList, }, data() { return { itemComponent: Item, hospitalData: [], loading: false, }; }, created() { if (this.dialogStatus === "update" || this.dialogStatus === "detail") { this.form = {...this.rowData}; this.getAllHospital(); } }, methods: { changeValue(id) { if (id) { let item = this.hospitalData.find(item => item.id === id); this.form.hospital_name = item.hospital } }, /** * 获取医院列表 */ getAllHospital() { if (this.form.hospital_id !== '') { let param = { id: this.form.hospital_id, field:"id,hospital" }; allHospital(param).then((res) => { if (res.code === 200) { this.hospitalData = res.data; } }); } }, getListHospital(query) { if (query !== '') { this.loading = true; setTimeout(() => { this.loading = false; let param = { hospital: query, pageSize: 100, field:"id,hospital" }; listHospital(param).then((res) => { if (res.code === 200) { this.hospitalData = res.data.data; } }); }, 200); } }, }, }; </script>



###item.vue
<template>
<el-option :label="source.hospital" :value="source.id"></el-option>
</template>

<script>
export default {
name: 'item-component',
props: {
index: { // index of current item
type: Number
},
source: { // here is: {uid: 'unique_1', text: 'abc'}
type: Object,
default() {
return {}
}
}
}
}
</script>

 

四:解决样式

1、支持el-select一切属性,利用select远程搜索,解决大数据查找问题

 

posted @ 2024-03-27 23:12  北漂生活  阅读(844)  评论(0编辑  收藏  举报