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
        })
      }
    },
  }
}
相关推荐
键盘不能没有CV键42 分钟前
【图片处理】✈️HTML转图片字体异常处理
前端·javascript·html
yantuguiguziPGJ1 小时前
WPF 联合 Web 开发调试流程梳理(基于 Microsoft.Web.WebView2)
前端·microsoft·wpf
大飞记Python2 小时前
部门管理|“编辑部门”功能实现(Django5零基础Web平台)
前端·数据库·python·django
tsumikistep3 小时前
【前端】前端运行环境的结构
前端
你的人类朋友3 小时前
【Node】认识multer库
前端·javascript·后端
Aitter3 小时前
PDF和Word文件转换为Markdown的技术实现
前端·ai编程
mapbar_front3 小时前
面试问题—上家公司的离职原因
前端·面试
昔人'4 小时前
css使用 :where() 来简化大型 CSS 选择器列表
前端·css
昔人'4 小时前
css `dorp-shadow`
前端·css
流***陌4 小时前
扭蛋机 Roll 福利房小程序前端功能设计:融合趣味互动与福利适配
前端·小程序