Vue2 ant-design table由于表格渲染太多数据导致渲染慢问题

在这里插入图片描述
由于表格渲染数据太多,导致渲染慢,所以初始展示一些数据,滚动再加载剩余数据,加快首次渲染页面速度
废话不多说,直接上代码:

<template>
  <div class="page-main">
 	<a-table
       ref="tableRef"
       :bordered="true"
       :columns="tableColumnList"
       :data-source="tableCopyData"
       :loading="tableLoading"
       :pagination="false"
       :scroll="scrollNum"
       :rowKey="(record, index) => index"
     >
       <a slot="action" slot-scope="text">action</a>
     </a-table>
  </div>
</template>
<script>
export default {
  name: 'tableScroll',
  data() {
    return {
      scrollNum: { x: 2600, y: 'calc(100vh - 560px)' },
      tableLoading: false,
      hasMore: false,
      tableData: [], // 展示表格数据
      tableCopyData: [], // 全部表格数据
      tableColumnList: [
		  { title: 'Full Name', width: 100, dataIndex: 'name', key:'name', fixed: 'left' },
		  { title: 'Age', width: 100, dataIndex: 'age', key: 'age', fixed: 'left' },
		  { title: 'Column 1', dataIndex: 'address', key: '1', width: 150 },
		  { title: 'Column 2', dataIndex: 'address', key: '2', width: 150 },
		  { title: 'Column 3', dataIndex: 'address', key: '3', width: 150 },
		  { title: 'Column 4', dataIndex: 'address', key: '4', width: 150 },
		  { title: 'Column 5', dataIndex: 'address', key: '5', width: 150 },
		  { title: 'Column 6', dataIndex: 'address', key: '6', width: 150 },
		  { title: 'Column 7', dataIndex: 'address', key: '7', width: 150 },
		  { title: 'Column 8', dataIndex: 'address', key: '8' },
		  {
		    title: 'Action',
		    key: 'operation',
		    fixed: 'right',
		    width: 100,
		    scopedSlots: { customRender: 'action' },
		  },
      ],
    }
  },
  created() {
    this.billMonth = [moment().subtract(1, 'months').format('YYYY-MM'), moment().subtract(1, 'months').format('YYYY-MM')]
    this.prodIncomeCostQueryItem()
    this.fetchTable()
  },
  mounted() {
    this.handleScrollTable()
  },
  destroyed() {
    // 组件销毁前移除事件监听器
    const table = this.$refs.tableRef
    if (table) {
      const scrollContainer = table.$el.getElementsByClassName('ant-table-body')[0]
      if (scrollContainer) {
        scrollContainer.removeEventListener('scroll', this.handleScroll)
      }
    }
  },
  methods: {
    handleScrollTable() {
      // 确保表格已挂载
      this.$nextTick(() => {
      	// 通过设置ref 属性,获取表格dom 元素
        const table = this.$refs.tableRef
        if (table) {
          // 获取表格的滚动容器并添加滚动事件监听器
          const scrollContainer = table.$el.getElementsByClassName('ant-table-body')[0]
          if (scrollContainer) {
            const throttledHandleScroll = _.throttle(this.handleScroll, 200)
            scrollContainer.addEventListener('scroll', throttledHandleScroll)
          }
        }
      });
    },
    handleScroll(event) {
      const target = event.target;
      const { scrollHeight, scrollTop, clientHeight } = target;
      this.hasMore = this.tableCopyData.length === this.tableData.length ? true : false
      // 距离表格内容底部距离 小于 '30' 时 渲染剩余数据
      if(scrollHeight - (scrollTop + clientHeight) < 30 && !this.hasMore) {
        this.tableCopyData = lodash.slice(this.tableData, 0, this.tableCopyData.length+2)
      }
    },
  }
}
</script>

旨在分享~~~~~~~~~~~~~~~~~~~~~~~

Ant Design Vue (ant-design-vue) 中,Table组件默认不会自动滚动,但如果表格内容过多导致无法全部显示在视口中,你可以通过CSS样式或第三方库来实现滚动效果。以下是两种常见的做法: 1. **使用Vue的`v-if`或`v-show`结合`scroll`属性**: 当数据长度超过一屏时,你可以创建一个虚拟滚动区域(`<div v-scroll="yourScrollMethod">`),然后在JavaScript中处理滚动事件。例如: ```html <template> <div v-scroll="scrollHandler"> <el-table :data="longData" /> </div> </template> <script> export default { methods: { scrollHandler(scrollY) { // 更新表头或其他需要跟随滚动的位置 this.$refs.myTable.scrollTop = scrollY; } }, data() { return { longData: [...], // 长度很大的数据源 }; } }; </script> ``` 2. **利用第三方滚动库,如vue-virtual-scroller**: 如果你需要更复杂的滚动功能,可以引入像vue-virtual-scroller这样的库。它能只渲染可视区的数据,提高性能。安装并配置这个库后,可以在Table组件上应用。 ```bash npm install vue-virtual-scroller ``` 然后在Table组件上使用: ```html <template> <VirtualScroller :items="longData" @scroll-end="scrollEnd"> <el-table :data="items" /> </VirtualScroller> </template> <script> import VirtualScroller from 'vue-virtual-scroller'; export default { components: { VirtualScroller }, computed: { items() { return this.longData.slice(this.start, this.end); }, start() { return Math.floor(this.vscachedScrollTop / this.itemHeight); }, end() { return Math.min(Math.ceil((this.vscachedScrollBottom + this.itemHeight) / this.itemHeight), this.longData.length); } }, mounted() { this.vscachedScrollTop = 0; // 初始化滚动位置 }, watch: { '$vscachedScrollTop': 'scrollEnd' }, }; </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值