el-select二次封裝實現(xiàn)可分頁加載數(shù)據(jù)的示例代碼
更新時間:2023年12月20日 09:23:56 作者:酒渣
使用el-select時一次性渲染幾百條數(shù)據(jù)時會造成頁面克頓, 可以通過分頁來實現(xiàn),所以本文給大家介紹了el-select二次封裝實現(xiàn)可分頁加載數(shù)據(jù),文中有詳細的代碼講解,具有一定的參考價值,需要的朋友可以參考下
使用el-select時一次性渲染幾百條數(shù)據(jù)時會造成頁面克頓, 可以通過分頁來實現(xiàn), 這里我用的方式為默認獲取全部數(shù)據(jù), 然后一次性截取10條進行展示, 滾動條觸底后會累加, 大家也可以優(yōu)化為滾動條觸底后發(fā)送請求去加載數(shù)據(jù)
- 創(chuàng)建自定義指令customizeFocus用戶懶加載
在utils文件夾(大家可自定義)下創(chuàng)建customizeFocus.vue
import Vue from 'vue'
/**
* 封裝el-selectt懶加載指令
* */
Vue.directive('customizeFocus',{
bind(el,binding){
let SELECT_DOM = el.querySelector(
".el-select-dropdown .el-select-dropdown__wrap"
);
SELECT_DOM.addEventListener("scroll", function () {
let condition = this.scrollHeight - this.scrollTop <= this.clientHeight;
if (condition) {
binding.value();
}
});
}
});
- 創(chuàng)建select.vue文件用來編寫分頁加載代碼
<template>
<div>
<el-select v-model="value1" collapse-tags :multiple="isMultiple" filterable :placeholder="placeholder"
v-customizeFocus="lazyloading" @change="submit()">
<el-option v-for="item in stationOptions" :key="item.id" :label="item.label" :value="item.vBindValue">
<span style="float: left;padding-right: 30px;">{{ item.label }}</span>
<span style="float: left;padding-right: 30px;">id:-{{ item.id }}</span>
<span style="float: right; color: #8492a6; font-size: 13px">{{ item.describe }}</span>
</el-option>
</el-select>
</div>
</template>
<script>
/**
* cur:每頁顯示多少條, 默認值: 10, 類型: number
* stationDataList: 已選擇的數(shù)據(jù), 默認值: [], 類型: array
* isMultiple: 是否多選, 默認值: false, 類型: boolean
* returnValue: 返回值內(nèi)容, 默認值: 數(shù)據(jù)id, 類型: string
* placeholder: 提示, 默認值: 請選擇, 類型: string
* */
export default {
name: "selectModel",
data() {
return {
value1: null,
// el-select展示的數(shù)據(jù)
stationOptions: [],
// 全部數(shù)據(jù)
stationAll: [],
// 測點懶加載分頁
stationLazy: {
startCur: 0,
// 當前頁碼數(shù)據(jù)
pageNum: 1,
},
stationData: this.stationDataList
}
},
props: {
isMultiple: {
default: false,
type: Boolean
},
stationDataList: {
default: Array,
type: Array
},
placeholder: {
default: '請選擇',
type: String
},
returnValue: {
default: 'id',
type: String
},
cur: {
default: 10,
type: Number
}
},
mounted() {
this.getStationAll();
},
methods: {
/**
* 獲取全部測點數(shù)據(jù)
* */
getStationAll() {
let returnValue = this.returnValue;
for (let i = 0; i < 100; i++) {
let obj = {
label: 'test_tag' + i+1,
id: i+1,
describe: 'test_tag' + i+1 + '描述'
};
this.stationAll.push(obj);
}
// 設置el-option的value綁定值
if (this.stationAll && this.stationAll.length){
this.stationAll.forEach(item=>{
item['vBindValue'] = item[returnValue]
})
this.stationOptions = this.stationAll.slice(this.stationLazy.startCur, this.cur);
// 查看
this.matchingData();
}
},
/**
* 匹配stationData里的數(shù)據(jù),將其顯示到下拉列表中
* */
matchingData(){
if (this.stationData && this.stationData.length){
this.value1 = [];
let returnValue = this.returnValue;
// 1.先查看當前顯示的option里是否存在,如果不存在,從全部數(shù)據(jù)里獲取
this.stationData.forEach(eachItem=>{
let stationIndex = this.stationOptions.map(mapItem=>mapItem[returnValue]).indexOf(eachItem);
if (stationIndex !== -1){
this.value1.push(this.stationOptions[stationIndex][returnValue]);
} else {
let stationAllIndex = this.stationAll.map(mapItem=>mapItem[returnValue]).indexOf(eachItem);
if (stationAllIndex !== -1){
this.stationOptions.push(this.stationAll[stationAllIndex]);
this.value1.push(this.stationAll[stationAllIndex][returnValue]);
}
}
})
}
},
/**
* 滾動條觸底時觸發(fā)此方法
* */
lazyloading() {
if (this.stationAll.length > 0){
// 計算總數(shù)據(jù)共有多少頁
let total = Math.ceil(this.stationAll.length / this.cur);
// 如果計算總頁碼數(shù)小于等于當前頁碼數(shù)證明已到最后一頁
if (total <= this.stationLazy.pageNum){
return console.log('已加載全部數(shù)據(jù)');
}
this.stationLazy.pageNum ++;
this.stationOptions = this.stationAll.slice(this.stationLazy.startCur, this.cur * this.stationLazy.pageNum);
this.matchingData();
}
},
/**
* 提交
* */
submit(){
this.stationData = JSON.parse(JSON.stringify(this.value1));
this.$emit('callBackData',this.stationData)
},
}
}
</script>
<style scoped>
</style>
- 在主文件里使用,
<template>
<div>
<selectModel :stationDataList="stationDataList" v-if="stationDataList.length" :isMultiple="true" @callBackData="callBackData"></selectModel>
</div>
</template>
<script>
import selectModel from '../../components/select/index'
export default {
name: "index",
components:{
selectModel
},
data(){
return {
stationDataList: [1,2,3,50,100], // 模擬獲取出來的數(shù)據(jù),數(shù)據(jù)id
}
},
mounted(){
},
methods:{
/**
* 返回值
* */
callBackData(e){
console.log(e,'eeeeeeeeeee')
}
}
}
</script>
<style scoped>
</style>
以上就是el-select二次封裝實現(xiàn)可分頁加載數(shù)據(jù)的示例代碼的詳細內(nèi)容,更多關于el-select可分頁加載數(shù)據(jù)的資料請關注腳本之家其它相關文章!
相關文章
Vue3.3?+?TS4構(gòu)建實現(xiàn)ElementPlus功能的組件庫示例
Vue.js?是目前最盛行的前端框架之一,而?TypeScript?則是一種靜態(tài)類型言語,它能夠讓開發(fā)人員在編寫代碼時愈加平安和高效,本文將引見如何運用?Vue.js?3.3?和?TypeScript?4?構(gòu)建一個自主打造媲美?ElementPlus?的組件庫2023-10-10

