elementui table滚动分页加载

文章目录

概要

简化的实现示例:

小结


概要

在使用 Element UI 的 Table 组件时,如果需要实现滚动分页加载的功能,可以通过监听 Table 的滚动事件来动态加载更多数据。

简化的实现示例:

javascript 复制代码
<template>
    <el-table ref="scrollTable" :data="tableData" height="400">
        <!-- 列配置 -->
    </el-table>
</template>
<script>
export default {
    components: {},
    data() {
        return {
            tableData: [],
            page: 1,
            pageSize: 10,
            total: 100, // 假设总数据量为 100
        }
    },
    computed: {},
    mounted() {
        /**
         * @description:添加分页加载滚动事件
         * @return {*}
         */
        let table = this.$refs.scrollTable.bodyWrapper
        table.addEventListener('scroll', (e) => {
            const { scrollTop, clientHeight, scrollHeight } = e.target
            // 检查是否滚动到底部
            if (scrollHeight - scrollTop <= clientHeight) {
                this.page++
                if ((this.page - 1) * this.pageSize >= this.total) {
                    // 数据已全部加载完毕
                    return
                }
                this.getTableData()
            }
        })
    },

    created() {
        this.getTableData()
    },
    methods: {

        /**
         * @description:获取表格数据
         * @return {*}
         */
        getTableData() {
            let params = {
                size: this.pageSize,
                current: this.page
            }
            this.$http({ method: 'get', url: `/hello/william/page?`, params })
                .then((res) => {
                    this.tableData.push(...res.data.records)
                    this.total = res.data.total
                })
                .finally(() => { })
        },

    }
}
</script>

小结

在这个例子中,我们设置了表格的 height 属性,使其具有固定高度并可以滚动。监听 scroll 事件,当表格滚动到底部时,触发scroll 事件。在scroll 事件中,我们通过当前的 page 和 pageSize 计算已经加载的数据范围,并更新 tableData 以添加新数据。如果加载的数据超过了 total,则停止加载数据。

相关推荐
明月_清风2 分钟前
Chrome 插件开发科普:从零开始打造你的浏览器小工具
前端
若梦plus4 分钟前
Node.js之TypeScript支持
前端·typescript
马优晨4 分钟前
cssnano 在前端工程化中的应用
前端·cssnano应用·cssnano 是什么·cssnano介绍·css优化
若梦plus5 分钟前
Node.js基础与常用模块
前端·node.js
若梦plus8 分钟前
Node.js之进程管理child_process与cluster深度解析
前端·node.js
若梦plus10 分钟前
Node.js之核心模块
前端·node.js
研☆香31 分钟前
html页面如何精准布局
前端·html
零下32摄氏度37 分钟前
【前端干货】接口在 Postman 测试很快,页面加载咋就慢?
前端·程序人生·postman
全栈陈序员41 分钟前
说说你对 Vue 的理解
前端·javascript·vue.js·学习·前端框架
全栈技术负责人1 小时前
Ling框架:针对AIGC工作流中JSON数据流式处理的解决方案
前端·ai