element table数据量太大,造成浏览器崩溃。解决方案

解决思路大致就是:把后台返回的上万条数据,进行分割(前端分页),这样先加载几十条,然后再用懒加载的方式去concat

<template>
  <div class="home-contianer">
    <el-table
      v-loading="loading"
      :data="tableList"
      ref="containerTable"
      border
      stripe
      v-loadmore="loadmore"
      :height="scrollerHeight"
      :header-cell-style="{ backgroundColor: '#E4E4E4', color: '#000000' }"
    >
      <el-table-column
        label="序号"
        align="center"
        prop="index"
        width="50"
        fixed
      />
      <el-table-column
        label="部门"
        align="left"
        prop="deptName"
        width="100"
        fixed
      />
      <el-table-column
        label="仓库"
        align="left"
        prop="wareName"
        width="200"
        fixed
      />
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dataList: [],
      loading: false,
      resourceTotal: 0,
      resourceNum: 1,
      resourcePage: 20,
      tableList: []
    };
  },
  directives: {
    loadmore: {
      bind(el, binding) {
        const selectWrap = el.querySelector(".el-table__body-wrapper");
        selectWrap.addEventListener("scroll", function() {
          const scrollDistance =
            this.scrollHeight - this.scrollTop - this.clientHeight;
          // 判断是否到底,scrollTop为已滚动到元素上边界的像素数,scrollHeight为元素完整的高度,clientHeight为内容的可见宽度
          if (scrollDistance <= 10) {
            binding.value();
          }
        });
      }
    }
  },
  computed: {
    // 滚动区高度
    scrollerHeight: function() {
      return window.innerHeight - 200 + "px"; //自定义高度需求
    }
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      for (let index = 0; index < 100000; index++) {
        let obj = {
          index: index,
          deptName: index + "deptName",
          wareName: "wareName" + index
        };
        this.dataList.push(obj);
      }
      this.tableList = this.dataList.slice(0, this.resourcePage);
      this.resourceTotal = this.tableList.length;
      this.resourceNum = 1;
      this.loading = false;
    },
    //懒加载
    loadmore() {
      this.resourceNum++;
      this.searchLoadResource();
    },
    searchLoadResource() {
      let result = [];
      if (this.resourcePage * this.resourceNum < this.dataList.length) {
        result = this.dataList.slice(
          this.resourcePage * (this.resourceNum - 1),
          this.resourcePage * this.resourceNum
        );
      } else {
        result = this.dataList.slice(
          this.resourcePage * (this.resourceNum - 1),
          this.dataList.length
        );
      }
      this.resourceTotal = result.length;
      this.tableList = this.tableList.concat(result);
    }
  }
};
</script>
<style>

</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值