最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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,
                // 當(dāng)前頁碼數(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);
            }

			// 設(shè)置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.先查看當(dāng)前顯示的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ù)小于等于當(dāng)前頁碼數(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)容,更多關(guān)于el-select可分頁加載數(shù)據(jù)的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 關(guān)于pinia的使用和持久化方式(pinia-plugin-persistedstate)

    關(guān)于pinia的使用和持久化方式(pinia-plugin-persistedstate)

    本文介紹了Pinia的使用方法,包括安裝和配置插件pinia-plugin-persistedstate,以及在項目中創(chuàng)建和使用Store模塊,同時,還講解了Pinia的state、getters和actions的使用,并提供了在uniapp中使用持久化插件的總結(jié)
    2025-02-02
  • 如何利用Vue3+Element?Plus實現(xiàn)動態(tài)標(biāo)簽頁及右鍵菜單

    如何利用Vue3+Element?Plus實現(xiàn)動態(tài)標(biāo)簽頁及右鍵菜單

    標(biāo)簽頁一般配合菜單實現(xiàn),當(dāng)你點擊一級菜單或者二級菜單時,可以增加對應(yīng)的標(biāo)簽頁,當(dāng)你點擊對應(yīng)的標(biāo)簽頁,可以觸發(fā)對應(yīng)的一級菜單或者二級菜單,下面這篇文章主要給大家介紹了關(guān)于如何利用Vue3+Element?Plus實現(xiàn)動態(tài)標(biāo)簽頁及右鍵菜單的相關(guān)資料,需要的朋友可以參考下
    2022-11-11
  • 淺析在Vue中watch使用的必要性及其優(yōu)化

    淺析在Vue中watch使用的必要性及其優(yōu)化

    這篇文章主要來和大家深入討論一下在Vue開發(fā)中是否有必要一定用watch,如果換成watcheffect會如何,文中的示例代碼講解詳細,需要的可以參考下
    2023-12-12
  • Vue.js 2.0 移動端拍照壓縮圖片預(yù)覽及上傳實例

    Vue.js 2.0 移動端拍照壓縮圖片預(yù)覽及上傳實例

    這篇文章主要介紹了Vue.js 2.0 移動端拍照壓縮圖片預(yù)覽及上傳實例,本來移動端開發(fā)H5應(yīng)用,準(zhǔn)備將mui框架和Vue.js+vue-router+vuex 全家桶結(jié)合起來使用
    2017-04-04
  • vue與bootstrap實現(xiàn)時間選擇器的示例代碼

    vue與bootstrap實現(xiàn)時間選擇器的示例代碼

    本篇文章主要介紹了vue與bootstrap實現(xiàn)時間選擇器的示例代碼,非常具有實用價值,需要的朋友可以參考下
    2017-08-08
  • ElementUI下拉組件el-select一次從后端獲取選項并設(shè)置默認值方式

    ElementUI下拉組件el-select一次從后端獲取選項并設(shè)置默認值方式

    這篇文章主要介紹了ElementUI下拉組件el-select一次從后端獲取選項并設(shè)置默認值方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • Vue.js輪播圖走馬燈代碼實例(全)

    Vue.js輪播圖走馬燈代碼實例(全)

    這篇文章主要介紹了Vue.js輪播圖走馬燈,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • vue-resource 攔截器使用詳解

    vue-resource 攔截器使用詳解

    本篇文章主要介紹了vue-resource 攔截器使用詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02
  • vue+elementUI實現(xiàn)點擊左右箭頭切換按鈕功能

    vue+elementUI實現(xiàn)點擊左右箭頭切換按鈕功能

    這篇文章主要介紹了vue+elementUI實現(xiàn)點擊左右箭頭切換按鈕功能,樣式可以根據(jù)自己需求改動,感興趣的朋友可以參考下實現(xiàn)代碼
    2024-05-05
  • 解讀Vue-loader的相關(guān)知識

    解讀Vue-loader的相關(guān)知識

    這篇文章主要介紹了解讀Vue-loader的相關(guān)知識,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03

最新評論

大洼县| 寿阳县| 西盟| 黄冈市| 黔江区| 塘沽区| 宜城市| 沾化县| 沂水县| 齐河县| 阜新市| 涟水县| 金溪县| 宜春市| 卢湾区| 乳源| 二手房| 安康市| 图木舒克市| 西充县| 河源市| 建宁县| 新巴尔虎右旗| 永春县| 墨玉县| 乐业县| 黎城县| 美姑县| 黑龙江省| 青海省| 观塘区| 余江县| 阳曲县| 潢川县| 甘谷县| 贺州市| 宁蒗| 新邵县| 宿迁市| 长寿区| 囊谦县|