elementUI中的 el-table 实现虚拟滚动

使用了 elementUI 中的 el-table 组件,不使用分页,当表格数据过多时导致页面卡顿;
解决该问题主要是用虚拟滚动的思路。引发页面卡顿的原因主要是由于数据量太大导致渲染的 dom 较多,然后页面就卡顿了。
下面使用 element 中的 el-table 实现一下虚拟滚动来解决这个问题
在这里插入图片描述
1 页面结构

<template>
  <div>
    <div class="gl-cell-card-box">
      <el-table
        ref="tableRef"
        style="width:418px"
        border
        max-height="448"
        :data="sliceTable"
        :row-key="row => row.id"
        @select="handleSelect"
        @select-all="handleSelectAll"
      >
        <el-table-column type="selection" width="40"> </el-table-column>
        <el-table-column prop="name" label="姓名" width="120"></el-table-column>
        <el-table-column prop="age" label="年龄" width="120"></el-table-column>
        <el-table-column prop="address" label="住址"></el-table-column>
      </el-table>
    </div>
  </div>
</template>

2 定义一些变量

data() {
    return {
        // 表格所有数据
        tableData: [],
        // 开始索引
        startIndex: 0,
        // 选中的数据
        selectedRows: [],
        // 空元素,用于撑开table的高度
        vEle: undefined,
        // 是否全选
        isSelectedAll: false,
    };
},

3 定义方法

// 计算属性
computed: {
    // 这个是截取表格中的部分数据,放到了 table 组件中来显示
    sliceTable() {
        return this.tableData.slice(this.startIndex, this.startIndex + 9);
    },
},
created() {
    // 创建一个空元素,这个空元素用来撑开 table 的高度,模拟所有数据的高度
    this.vEle = document.createElement("div");
    this.loadData();
}mounted() {
    // 绑定滚动事件
    this.$refs.tableRef.$el
        .querySelector(".el-table__body-wrapper")
        .addEventListener("scroll", this.tableScroll, {
        passive: true
    });
},
methods: {
    // 加载数据
    loadData() {
        let start_i = this.tableData.length;
        for (let i = start_i; i < start_i + 20; i++) {
            this.tableData.push({
                id: i,
                name: "zhangsan" + i,
                age: 12,
                address: "china"
            });
        }
        this.$nextTick(() => {
            // 设置成绝对定位,这个元素需要我们去控制滚动
            this.$refs.tableRef.$el.querySelector(".el-table__body").style.position = "absolute";
            // 计算表格所有数据所占内容的高度
            this.vEle.style.height = this.tableData.length * 48 + "px";
            // 把这个节点加到表格中去,用它来撑开表格的高度
            this.$refs.tableRef.$el.querySelector(".el-table__body-wrapper").appendChild(this.vEle);
			// 重新设置曾经被选中的数据
            this.selectedRows.forEach(row => {
                this.$refs.tableRef.toggleRowSelection(row, true);
            });
        });
    },
    /**
     * @description: 手动勾选时的事件
     * @param {*} selection - 选中的所有数据
     * @param {*} row - 当前选中的数据
     * @return {*}
     */
    handleSelect(selection, row) {
        this.selectedRows = selection;
    },
    /**
     * @description: 全选事件
     * @param {*} selection
     * @return {*}
     */
    handleSelectAll(selection) {
        this.isSelectedAll = !this.isSelectedAll;
        if (this.isSelectedAll) {
            this.selectedRows = this.tableData;
        } else {
            this.selectedRows = [];
            this.$refs.tableRef.clearSelection();
        }
    },
    /**
     * @description: table 滚动事件
     * @param {*}
     * @return {*}
     */
    tableScroll() {
        let bodyWrapperEle = this.$refs.tableRef.$el.querySelector(".el-table__body-wrapper");
        // 滚动的高度
        let scrollTop = bodyWrapperEle.scrollTop;
        // 下一次开始的索引
        this.startIndex = Math.floor(scrollTop / 48);
        // 滚动操作
        bodyWrapperEle.querySelector(".el-table__body").style.transform = `translateY(${this.startIndex * 48}px)`;
		// 滚动操作后,上面的一些 tr 没有了,所以需要重新设置曾经被选中的数据
        this.selectedRows.forEach(row => {
            this.$refs.tableRef.toggleRowSelection(row, true);
        });

        // 滚动到底,加载新数据
        if (bodyWrapperEle.scrollHeight <= scrollTop + bodyWrapperEle.clientHeight) {
            if (this.tableData.length == 100) {
                this.$message.warning("没有更多了");
                return;
            }
            this.loadData();
        }
    }
}
  • 7
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
要修改 `el-table` `el-table-filter` 的样式,可以使用以下步骤: 1. 在 `el-table` 添加 `slot="header"`,以便自定义表头。 2. 在 `slot="header"` 添加 `template`,并设置 `slot-scope` 为 `{ column }`,以便获取当前列的信息。 3. 在 `template` 添加一个 `div` 元素,并设置 `class` 属性为 `filter-wrapper`,并为其添加样式。 4. 在 `div` 元素添加 `el-table-filter` 组件,并设置其 `data-key` 属性为当前列的 `property` 值。 5. 设置 `el-table-filter` 组件的样式,可以通过使用 `::v-deep` 或 `>>>` 来覆盖其默认样式。 下面是一个示例代码: ```html <template> <el-table :data="tableData"> <el-table-column prop="name" label="姓名" :filters="filterOptions" :filter-method="filterMethod" slot="header" v-slot="{ column }" > <div class="filter-wrapper"> <el-table-filter :data-key="column.property" style="width: 100%;" ></el-table-filter> </div> </el-table-column> </el-table> </template> <style scoped> .filter-wrapper { padding: 10px; border: 1px solid #ccc; border-radius: 4px; background-color: #f7f7f7; } ::v-deep .el-table-filter__list { max-height: 200px; overflow: auto; } ::v-deep .el-table-filter__list li label { display: block; margin: 5px 0; } ::v-deep .el-table-filter__bottom { padding: 5px; text-align: right; } </style> ``` 在上面的示例代码,我们为 `el-table` 的每一列都添加了 `el-table-filter` 组件,并使用 `filter-wrapper` 类来设置其样式。同时,我们使用 `::v-deep` 来覆盖了 `el-table-filter` 组件的默认样式,包括设置选项列表的最大高度、调整每个选项的样式以及设置底部按钮的样式。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值