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

Vue實現(xiàn)下拉加載更多

 更新時間:2021年05月09日 11:20:39   作者:aiguangyuan  
這篇文章主要為大家詳細介紹了Vue實現(xiàn)下拉加載更多,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

熟悉Element-UI的開發(fā)者可能都會有這樣的經(jīng)歷,它的無限滾動 InfiniteScroll 并不好用,下面介紹兩種下拉加載的實現(xiàn)方法:

1. 使用el-table-infinite-scroll 插件

(1). 安裝插件

npm install --save el-table-infinite-scroll

(2). 全局引入并注冊

// main.js
 
import elTableInfiniteScroll from 'el-table-infinite-scroll';
 
Vue.use(elTableInfiniteScroll);

(3). 局部文件引入

<script>
// 引入
import elTableInfiniteScroll from 'el-table-infinite-scroll';
export default {
  // 注冊指令
  directives: {
    'el-table-infinite-scroll': elTableInfiniteScroll
  }
}
</script>

(4). 使用指令

<el-table :height="tableHeight" v-el-table-infinite-scroll="loadMore">
 
</el-table>

(5). 代碼實例

<template>
    <div class="app-container">
        <el-table :height="tableHeight" v-el-table-infinite-scroll="loadMore" :data="tableList">
            <!-- 表格數(shù)據(jù)省略 -->
        </el-table>
    </div>
</template>
 
<script>
// 引入插件
import elTableInfiniteScroll from "el-table-infinite-scroll";
 
export default {
    name: "index",
    data() {
        return {
            // 表格高度
            tableHeight:"",
            // 數(shù)據(jù)總數(shù)
            tableCount:0,
            // 表格數(shù)據(jù)列表
            tableList:[],
            // 是否加載中
            tableLoading:false,
            // 表格搜索條件
            tableSearch:{
                page:1
            }
        }
    },
    // 注冊指令
    directives: {
        "el-table-infinite-scroll": elTableInfiniteScroll,
    },
 
    created() {
        let windowHeight =document.documentElement.clientHeight || document.body.clientHeight;
        // 動態(tài)計算表格的高度,200為屏幕內(nèi)除了表格以外其他元素的高度,依實際情況而定
        this.tableHeight = windowHeight - 200 + "px";
    },
    mounted(){
        this.getTableData(this.tableSearch);
    },
 
    methods: {
        // 請求表格數(shù)據(jù)
        getTableData(search) {
            let page = search.page;
            let url = "index?page=" + page;
            // 首次打開頁面要加載一次數(shù)據(jù),為了防止數(shù)據(jù)過多自動觸發(fā)滾動,此處需要置為加載中
            this.tableLoading = true;
            this.$http.get(url).then((result) => {
                if (res.code == 10000) {
                    // 拼接數(shù)據(jù)
                    this.tableList = this.tableList.concat(result.data.list);
                    this.tableCount = result.count;
                    this.tableSearch.page = result.current;
                    this.tableLoading = false;
                }
            });
        },
        // 加載更多
        loadMore() {
            if (!this.tableLoading) {
                this.tableLoading = true;
                if (this.tableList.length < this.tableCount) {
                    this.tableSearch.page++;
                    this.getTableData(this.tableSearch);
                } else {
                    this.$message("已加載完所有的數(shù)據(jù)!");
                    this.tableLoading = false;
                }
            }
        },
    }
};
</script>

2. 自定義下拉加載方法

上面使用的插件需要依賴Element-UI,如果沒有使用Element-UI,那就只能自己寫一個下拉加載了,實現(xiàn)代碼如下:

<template>
    <div class="app-container">
        <div :style="{height:tableHeight,overflow:'auto'}" id="tableBox">
            <!-- 表格數(shù)據(jù)省略 -->
        </div>
    </div>
</template>
 
<script>
export default {
    name: "index",
    data() {
        return {
            // 表格高度
            tableHeight:"",
            // 數(shù)據(jù)總數(shù)
            tableCount:0,
            // 表格數(shù)據(jù)列表
            tableList:[],
            // 是否加載中
            tableLoading:false,
            // 表格搜索條件
            tableSearch:{
                page:1
            }
        };
    },
    created(){
        let windowHeight = document.documentElement.clientHeight || document.body.clientHeight;
        // 動態(tài)計算表格的高度,200為屏幕內(nèi)除了表格以外其他元素的高度,依實際情況而定
        this.tableHeight = windowHeight - 200 +'px';
    },
    mounted() {
        this.getTableData(this.tableSearch);
        document.getElementById("tableBox").addEventListener('scroll',this.onTableScroll); 
    },
 
    beforeDestroy() {
        // 移除監(jiān)聽事件
        window.removeEventListener('scroll', this.onTableScroll)
    },
 
    methods: {
        onTableScroll(){
            var obj = document.getElementById("tableBox");
            var scrollHeight = obj.scrollHeight;
            var scrollTop = obj.scrollTop; 
            var objHeight = obj.offsetHeight;  
            // 100為閾值,可根據(jù)實際情況調(diào)整    
            if(scrollHeight <= (scrollTop + objHeight + 100) && !this.tableLoading && this.tableList.length<this.tableCount){ 
                this.tableLoading = true;
                this.tableSearch.page++;
                setTimeout(()=>{   
                    this.getTableData(this.tableSearch);
                },300)
            }
        },
 
        getTableData(search){
            let page = search.page;
            let url = "index?page=" + page;
            // 首次打開頁面要加載一次數(shù)據(jù),為了防止數(shù)據(jù)過多自動觸發(fā)滾動,此處需要置為加載中
            this.tableLoading = true;
            this.$http.get(url).then((result) => {
                if (res.code == 10000) {
                    // 拼接數(shù)據(jù)
                    this.tableList = this.tableList.concat(result.data.list);
                    this.tableCount = result.count;
                    this.tableSearch.page = result.current;
                    this.tableLoading = false;
                }
            });
        },
        
     
    },
};
</script>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • vuex的幾個屬性及其使用傳參方式

    vuex的幾個屬性及其使用傳參方式

    這篇文章主要介紹了vuex的幾個屬性及其使用傳參,本文結(jié)合實例代碼給大家講解的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-01-01
  • Vue監(jiān)聽數(shù)組變化源碼解析

    Vue監(jiān)聽數(shù)組變化源碼解析

    這篇文章主要為大家詳細解析了Vue監(jiān)聽數(shù)組變化的源碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • vue實現(xiàn)圖片路徑轉(zhuǎn)二進制文件流(binary)

    vue實現(xiàn)圖片路徑轉(zhuǎn)二進制文件流(binary)

    這篇文章主要介紹了vue實現(xiàn)圖片路徑轉(zhuǎn)二進制文件流(binary),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • Vue實現(xiàn)倒計時小功能

    Vue實現(xiàn)倒計時小功能

    這篇文章主要為大家詳細介紹了Vue實現(xiàn)倒計時小功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • vue 自動化路由實現(xiàn)代碼

    vue 自動化路由實現(xiàn)代碼

    這篇文章主要介紹了vue 自動化路由實現(xiàn)代碼,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-09-09
  • vue頁面切換項目實現(xiàn)轉(zhuǎn)場動畫的方法

    vue頁面切換項目實現(xiàn)轉(zhuǎn)場動畫的方法

    這篇文章主要介紹了vue頁面切換項目實現(xiàn)轉(zhuǎn)場動畫的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • vue usePop彈窗控制器的實現(xiàn)

    vue usePop彈窗控制器的實現(xiàn)

    本文主要介紹了vue usePop彈窗控制器的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • vue.js?Table?組件自定義列寬實現(xiàn)核心方法

    vue.js?Table?組件自定義列寬實現(xiàn)核心方法

    這篇文章主要介紹了vue.js?Table?組件自定義列寬實現(xiàn)核心方法,文圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-07-07
  • vue中echarts關(guān)系圖動態(tài)增刪節(jié)點以及連線方式

    vue中echarts關(guān)系圖動態(tài)增刪節(jié)點以及連線方式

    這篇文章主要介紹了vue中echarts關(guān)系圖動態(tài)增刪節(jié)點以及連線方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • Vuex之Action的使用方法詳解

    Vuex之Action的使用方法詳解

    這篇文章主要介紹了Vuex之Action的使用方法詳解,Action 類似于 mutation ,不同在于Action 提交的是 mutation,而不是直接變更狀態(tài),
    Action 可以包含任意異步操作,需要的朋友可以參考下
    2023-11-11

最新評論