欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

el-select二次封裝實(shí)現(xiàn)可分頁(yè)加載數(shù)據(jù)的示例代碼

 更新時(shí)間:2023年12月20日 09:23:56   作者:酒渣  
使用el-select時(shí)一次性渲染幾百條數(shù)據(jù)時(shí)會(huì)造成頁(yè)面克頓, 可以通過(guò)分頁(yè)來(lái)實(shí)現(xiàn),所以本文給大家介紹了el-select二次封裝實(shí)現(xiàn)可分頁(yè)加載數(shù)據(jù),文中有詳細(xì)的代碼講解,具有一定的參考價(jià)值,需要的朋友可以參考下

使用el-select時(shí)一次性渲染幾百條數(shù)據(jù)時(shí)會(huì)造成頁(yè)面克頓, 可以通過(guò)分頁(yè)來(lái)實(shí)現(xiàn), 這里我用的方式為默認(rèn)獲取全部數(shù)據(jù), 然后一次性截取10條進(jìn)行展示, 滾動(dòng)條觸底后會(huì)累加, 大家也可以?xún)?yōu)化為滾動(dòng)條觸底后發(fā)送請(qǐng)求去加載數(shù)據(jù)

  • 創(chuàng)建自定義指令customizeFocus用戶(hù)懶加載
    在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文件用來(lái)編寫(xiě)分頁(yè)加載代碼
<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:每頁(yè)顯示多少條, 默認(rèn)值: 10, 類(lèi)型: number
 * stationDataList: 已選擇的數(shù)據(jù), 默認(rèn)值: [], 類(lèi)型: array
 * isMultiple: 是否多選, 默認(rèn)值: false, 類(lèi)型: boolean
 * returnValue: 返回值內(nèi)容, 默認(rèn)值: 數(shù)據(jù)id, 類(lèi)型: string
 * placeholder: 提示, 默認(rèn)值: 請(qǐng)選擇, 類(lèi)型: string
 * */
export default {
    name: "selectModel",
    data() {
        return {
            value1: null,
            // el-select展示的數(shù)據(jù)
            stationOptions: [],
            // 全部數(shù)據(jù)
            stationAll: [],
            // 測(cè)點(diǎn)懶加載分頁(yè)
            stationLazy: {
                startCur: 0,
                // 當(dāng)前頁(yè)碼數(shù)據(jù)
                pageNum: 1,
            },
            stationData: this.stationDataList
        }
    },
    props: {
        isMultiple: {
            default: false,
            type: Boolean
        },
        stationDataList: {
            default: Array,
            type: Array
        },
        placeholder: {
            default: '請(qǐng)選擇',
            type: String
        },
        returnValue: {
            default: 'id',
            type: String
        },
        cur: {
            default: 10,
            type: Number
        }
    },
    mounted() {
        this.getStationAll();
    },
    methods: {
        /**
         * 獲取全部測(cè)點(diǎn)數(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]);
                        }
                    }
                })
            }
        },

        /**
         * 滾動(dòng)條觸底時(shí)觸發(fā)此方法
         * */
        lazyloading() {
            if (this.stationAll.length > 0){
                // 計(jì)算總數(shù)據(jù)共有多少頁(yè)
                let total = Math.ceil(this.stationAll.length / this.cur);
                // 如果計(jì)算總頁(yè)碼數(shù)小于等于當(dāng)前頁(yè)碼數(shù)證明已到最后一頁(yè)
                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], // 模擬獲取出來(lái)的數(shù)據(jù),數(shù)據(jù)id
      }
    },
    mounted(){
    },
    methods:{
        /**
         * 返回值
         * */
        callBackData(e){
            console.log(e,'eeeeeeeeeee')
        }
    }
  }
</script>

<style scoped>

</style>

以上就是el-select二次封裝實(shí)現(xiàn)可分頁(yè)加載數(shù)據(jù)的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于el-select可分頁(yè)加載數(shù)據(jù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Vue3.3?+?TS4構(gòu)建實(shí)現(xiàn)ElementPlus功能的組件庫(kù)示例

    Vue3.3?+?TS4構(gòu)建實(shí)現(xiàn)ElementPlus功能的組件庫(kù)示例

    Vue.js?是目前最盛行的前端框架之一,而?TypeScript?則是一種靜態(tài)類(lèi)型言語(yǔ),它能夠讓開(kāi)發(fā)人員在編寫(xiě)代碼時(shí)愈加平安和高效,本文將引見(jiàn)如何運(yùn)用?Vue.js?3.3?和?TypeScript?4?構(gòu)建一個(gè)自主打造媲美?ElementPlus?的組件庫(kù)
    2023-10-10
  • vue+高德地圖寫(xiě)地圖選址組件的方法

    vue+高德地圖寫(xiě)地圖選址組件的方法

    這篇文章主要介紹了vue+高德地圖寫(xiě)地圖選址組件的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • Vue頭像處理方案小結(jié)

    Vue頭像處理方案小結(jié)

    這篇文章主要介紹了Vue頭像處理方案,實(shí)現(xiàn)思路主要是通過(guò)獲取后臺(tái)返回頭像url,判斷圖片寬度,高度。具體實(shí)例代碼大家參考下本文
    2018-07-07
  • Vue動(dòng)態(tài)面包屑功能的實(shí)現(xiàn)方法

    Vue動(dòng)態(tài)面包屑功能的實(shí)現(xiàn)方法

    面包屑功能是我們?cè)陧?xiàng)目中經(jīng)常遇到的功能,今天小編使用Element-UI 進(jìn)行實(shí)現(xiàn)在vue項(xiàng)目中實(shí)現(xiàn)面包屑功能,具體實(shí)現(xiàn)方式大家跟隨小編一起學(xué)習(xí)吧
    2019-07-07
  • vue實(shí)現(xiàn)頁(yè)面刷新動(dòng)畫(huà)

    vue實(shí)現(xiàn)頁(yè)面刷新動(dòng)畫(huà)

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)頁(yè)面刷新動(dòng)畫(huà),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • vue bus全局事件中心簡(jiǎn)單Demo詳解

    vue bus全局事件中心簡(jiǎn)單Demo詳解

    ue-bus 提供了一個(gè)全局事件中心,并將其注入每一個(gè)組件,你可以像使用內(nèi)置事件流一樣方便的使用全局事件。這篇文章給大家介紹了vue bus全局事件中心簡(jiǎn)單Demo,需要的朋友參考下吧
    2018-02-02
  • 詳解Nuxt.js部署及踩過(guò)的坑

    詳解Nuxt.js部署及踩過(guò)的坑

    這篇文章主要介紹了詳解Nuxt.js部署及踩過(guò)的坑,Nuxt.js 提供了兩種發(fā)布部署應(yīng)用的方式:服務(wù)端渲染應(yīng)用部署 和 靜態(tài)應(yīng)用部署。本文主要說(shuō)說(shuō)服務(wù)端渲染應(yīng)用部署,感興趣的小伙伴們可以參考一下
    2018-08-08
  • 如何封裝Vue Element的table表格組件

    如何封裝Vue Element的table表格組件

    這篇文章主要介紹了如何封裝Vue Element的table表格組件,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下
    2021-02-02
  • Hooks對(duì)于Vue作用意義詳解

    Hooks對(duì)于Vue作用意義詳解

    這篇文章主要為大家介紹了Hooks對(duì)于Vue作用意義詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • vue?過(guò)濾、模糊查詢(xún)及計(jì)算屬性?computed詳解

    vue?過(guò)濾、模糊查詢(xún)及計(jì)算屬性?computed詳解

    計(jì)算屬性是vue里面為了簡(jiǎn)化在模板語(yǔ)法中對(duì)響應(yīng)式屬性做計(jì)算而存在的,這篇文章主要介紹了vue?過(guò)濾、模糊查詢(xún)(計(jì)算屬性?computed),需要的朋友可以參考下
    2022-11-11

最新評(píng)論