vue 渲染多列表格,拖动加载

vue在使用el-table渲染多列(几千列)表格时,页面会十分卡顿,使用html原生表格拖动滚动条加载列,可以解决这个问题

后端接口返回的数据格式如下:

line_data中的数据title对应index_title里的内容

javascript 复制代码
<template>
  <table class="echarts-table" v-loading="dataTableLoading" :height="tableHeight">
    <thead class="table-title">
       <tr>
          <th v-for="(item, index) in titleList" :key="index" :title="item">
              {{ item }}
          </th>
        </tr>
    </thead>
    <tbody>
        <tr
          v-for="(item, index) in datalist"
          :key="index"
          class="table-content"
          :class="{ 'odd-number': index % 2 != 0 }"
         >
            <td
              v-for="(prop, idx) in item"
              :key="idx"
              :title="idx != 0 ? prop : ''"
            >
              {{ prop }}
            </td>
        </tr>
    </tbody>
  </table>
</template>
<script>
export default {
  data() {
    return {
      titleList: [],
      contentList: [],
      datalist: [],
      currentFeatureList: [],
      colPage: 1,
      colPageSize: 100,
      total: 0
    }
  },
  mounted() {
    this.$nextTick(() => {
      const scrollDiv = document.getElementsByClassName('echarts-table')[0]//获取表格元素
      scrollDiv.addEventListener('scroll', () => {
        // 判断是否滚动到底部
        if (scrollDiv.scrollLeft + scrollDiv.clientWidth === scrollDiv.scrollWidth) {
          console.log('横向滚动到底部');
          if (this.total >= this.startData.excel_data[0].line_data.length) return
          this.colPage += 1
          //加载列
          this.getColData(this.colPage, 'scroll')
        } else {
          console.log('object');
        }
      });
    })
  },
  methods: {
  //获取表格数据
    getDetailList() {
      this.dataTableLoading = true;
      this.$axios
        .get(`/ai/radiomics/feature/excel/${this.info.id}`)
        .then((res) => {
          if (res.data.code == 200) {
            console.time('p1');
            this.startData = res.data.data
            this.getColData()
            this.dataTableLoading = false;
          }
        });
    },
    getColData() {
      const length = this.startData.excel_data[0].line_data.length
      const isLastPage = this.colPage === parseInt(length / this.colPageSize) //判断是否为组后一页
      const residueNumber = length % this.colPageSize //余数
      this.total = length >= this.colPageSize ? (isLastPage ? (this.colPage * this.colPageSize) + residueNumber : this.colPage * this.colPageSize) : length
      const currentDatalist = []
      this.startData.excel_data.forEach((item, index) => {
        const currentPage = (this.colPage - 1) * this.colPageSize
        const currentLineData = []
        for (let i = currentPage; i < this.total; i++) {
          const value = item.line_data[i]
          const key = this.startData.index_title[i]
          if (index == 0) {
            this.titleList.push(key)
          }
          currentLineData.push(value)
        }
        currentDatalist.push(currentLineData)
      })
      if (this.colPage == 1) {
        this.datalist = currentDatalist
      } else {
       // 当前列添加到原数组
        this.datalist = this.datalist.map((item, index) => {
          const currentItem = currentDatalist[index]
          item = [
            ...item,
            ...currentItem
          ]
          return item
        })
      }
    },
  }
}
相关推荐
lichong9514 分钟前
【混合开发】vue+Android、iPhone、鸿蒙、win、macOS、Linux之video 的各种状态和生命周期调用说明
android·vue.js·macos
excel14 分钟前
为什么相同卷积代码在不同层学到的特征完全不同——基于 tfjs-node 猫图像识别示例的逐层解析
前端
知识分享小能手15 分钟前
React学习教程,从入门到精通,React 使用属性(Props)创建组件语法知识点与案例详解(15)
前端·javascript·vue.js·学习·react.js·前端框架·vue
用户214118326360217 分钟前
dify案例分享-免费玩转即梦 4.0 多图生成!Dify 工作流从搭建到使用全攻略,附案例效果
前端
CodeSheep17 分钟前
稚晖君又开始摇人了,有点猛啊!
前端·后端·程序员
JarvanMo20 分钟前
Flutter Web vs Mobile:主要区别以及如何调整你的UI
前端
IT_陈寒39 分钟前
Java性能优化:从这8个关键指标开始,让你的应用提速50%
前端·人工智能·后端
天生我材必有用_吴用41 分钟前
Vue3+Node.js 实现大文件上传:断点续传、秒传、分片上传完整教程(含源码)
前端
摸鱼的春哥1 小时前
前端程序员最讨厌的10件事
前端·javascript·后端
牧羊狼的狼5 小时前
React 中的 HOC 和 Hooks
前端·javascript·react.js·hooks·高阶组件·hoc