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,则停止加载数据。

相关推荐
FRing032 小时前
vue2干货总结7(自定义指令、插槽、单页应用程序、路由的使用(VueRouter))
开发语言·前端·vue.js·vue2
疯华整猫5 小时前
被杀掉之后:我把 Android 的「唤起 + 常驻」重做了一遍
前端·面试·架构
ruanno_top5 小时前
点全选,已选的被清空了:Transfer 三态复选框的三个 bug
vue.js
时间的情敌7 小时前
Cesium 加载 3D Tiles 倾斜摄影、压平、样式设置(Vue3 实战)
vue.js·3d·cesium
IT_陈寒7 小时前
Java开发实战:从入门到精通
前端·人工智能·后端
风骏时光牛马7 小时前
生成一个AI转岗经历标题,只需要一个标题,不需要其他的任何文本。
前端
Csvn17 小时前
Monorepo
前端
Csvn17 小时前
流水线
前端
Csvn17 小时前
微前端架构
前端
Linux-lucky18 小时前
45-Linux学习之旅之防火墙与WEB加固
linux·前端·网络·学习·ubuntu