VUE页面表格数据多卡顿用虚拟表单

28 篇文章 1 订阅
1 篇文章 0 订阅

情景概况:页面表格想要通过滚动条展示非常多的数据,后端返回接口很快。

解决方法虚拟滚动  vue + vxe-table

20220708_145309

element ui 表单已经不能满足这个需求,二次封装也很麻烦,所以推荐用现有插件vxetable

插件地址vxe-table v4

第一步: 安装,右上角选择需要的版本

 根据指南安装(选择版本后再npi,不然会出现未知错误)

第二步: 找到使用需要使用的功能,我这里以横向&纵向为例

第三步:直接上代码,根据需要API里面查找

<template>
  <div>
    <vxe-grid
      show-header-overflow
      border
      show-overflow
      :scroll-y="{ gt: 0 }"
      :scroll-x="{ gt: 0 }"
      ref="xGrid"
      :column-config="{ minWidth: '200' }"
      height="500"
    >
    </vxe-grid>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableColumn: [],
      tableData: [],
    };
  },
  created() {
    for (let i = 0; i < 100; i++) {
      this.tableColumn.push({ field: "name", title: "Name"  + i},);
      this.tableData.push({ id: i, name: "Test1" })
    }
  },
  mounted() {
    this.loadColumnAndData();
  },
  methods: {
    loadColumnAndData() {
      const startTime = Date.now();
      const xGrid = this.$refs.xGrid;
      // 使用函数式加载,阻断 vue 对大数组的双向绑定
      if (xGrid) {
        Promise.all([
          xGrid.reloadColumn(this.tableColumn),
          xGrid.reloadData(this.tableData),
        ]).then(() => {
          this.$XModal.message({
            message: `渲染用时 ${Date.now() - startTime}毫秒`,
            status: "info",
          });
        });
      }
    },
  },
};
</script>

<style >
</style>

开启虚拟滚动

:scroll-y="{ gt: 0 }"
:scroll-x="{ gt: 0 }"

第四步:渲染

由于普通渲染是双向绑定,我们可以选择单方向输入,页面会流畅很多

 下面是数据结构和渲染体

tableStructure: [  // 表头结构
  {
    title: "Name",
    children: [
      { field: "name", title: "名称" },
      { field: "age", title: "年龄", sortable: true },
    ],
  },
  { field: "name", title: "Name" },
],
tableData: [ // 数据结构
  {name: '张三', age: 18}
],


const startTime = Date.now();
const xGrid = this.$refs.xGrid;
// 使用函数式加载,阻断 vue 对大数组的双向绑定
if (xGrid) {
  Promise.all([
    xGrid.reloadColumn(this.tableColumn), // 渲染表头
    xGrid.reloadData(this.tableData),  // 渲染数据
  ]).then(() => {
    this.$XModal.message({
      message: `渲染用时 ${Date.now() - startTime}毫秒`,
      status: "info",
    });
  });
}

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天气晚来秋~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值