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
        })
      }
    },
  }
}
相关推荐
无责任此方_修行中1 小时前
每周见闻分享:杂谈AI取代程序员
javascript·资讯
Σίσυφος19002 小时前
halcon 条形码、二维码识别、opencv识别
前端·数据库
学代码的小前端2 小时前
0基础学前端-----CSS DAY13
前端·css
dorabighead3 小时前
JavaScript 高级程序设计 读书笔记(第三章)
开发语言·javascript·ecmascript
css趣多多3 小时前
案例自定义tabBar
前端
姑苏洛言5 小时前
DeepSeek写微信转盘小程序需求文档,这不比产品经理强?
前端
林的快手5 小时前
CSS列表属性
前端·javascript·css·ajax·firefox·html5·safari
匹马夕阳5 小时前
ECharts极简入门
前端·信息可视化·echarts
bug总结5 小时前
新学一个JavaScript 的 classList API
开发语言·javascript·ecmascript
网络安全-老纪5 小时前
网络安全-js安全知识点与XSS常用payloads
javascript·安全·web安全