vue.js el-table虚拟滚动完整实例代码

 更新时间:2022年12月30日 14:05  点击:574 作者:CryptoCode

前言

基于Element-UI的Table 组件开发的虚拟滚动组件,支持动态高度,解决数据量大时滚动卡顿的问题

实例代码

<template>
  <div
    ref="listWrap"
    style="height: 400px; overflow-y: scroll; margin-top: 20px; padding: 10px"
    @scroll="scrollListener"
  >
    <div ref="list">
      <el-table
        @select="select"
        @select-all="selectAll"
        style="margin-top: 10px"
        :data="showList"
        ref="scrollTable"
      >
        <slot></slot>
      </el-table>
    </div>
  </div>
</template>

<script lang="ts">
import { ref, onMounted, computed, watch, defineComponent, nextTick } from 'vue'
interface IProps {
  start: number
  end: number
  height: number
  itemHeight: number
  rowKey: string
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  initList: any[]
}
export default defineComponent({
  name: 'Vue3VitualTable',
  props: ['start', 'end', 'height', 'itemHeight', 'initList', 'rowKey'],
  emits: ['handleSelect'],
  setup(props: IProps, { emit }) {
    // 表格
    const listWrap = ref()
    const list = ref()
    const scrollTable = ref()
    const start = ref(props.start)
    const end = ref(props.end)
    const isAllSelected = ref(false)
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    const selections = ref([] as any[])

    // 可视区列表
    const showList = computed(() => {
      return props.initList.slice(start.value, end.value)
    })

    // 数据长度
    const length = computed(() => {
      return props.initList.length
    })

    // 滚动
    const scrollListener = () => {
      // 获取滚动高度
      const scrollTop = listWrap.value.scrollTop
      // 开始的数组索引
      start.value = Math.floor(scrollTop / props.itemHeight)
      // 结束索引
      end.value = start.value + 10
      list.value.style.transform = `translateY(${start.value * 65}px)` // 对列表项y轴偏移
      nextTick(() => {
        selections.value.forEach((ele) => {
          scrollTable.value.toggleRowSelection(ele, true)
        })
      })
    }

    watch(length, (val) => {
      if (val > 10) {
        listWrap.value.style.height = props.itemHeight * 10 + 'px'
      } else {
        listWrap.value.style.height = props.itemHeight * val + 57 + 'px'
      }
    })

    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    const handleSelect = (val: any) => {
      if (!isAllSelected.value) {
        isAllSelected.value = scrollTable.value.store.states.isAllSelected.value
      }

      console.log('store.states.isAllSelected', scrollTable.value.store.states.isAllSelected.value)
      emit('handleSelect', val)
    }

    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    const select = (val: any) => {
      if (val.length < props.initList.length) {
        isAllSelected.value = false
      } else {
        isAllSelected.value = true
      }
      selections.value = val
      emit('handleSelect', selections.value)
      console.log('select', val)
    }

    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    const selectAll = (val: any) => {
      if (val.length) {
        selections.value = props.initList
        isAllSelected.value = true
      } else {
        selections.value = []
        isAllSelected.value = false
      }
      emit('handleSelect', selections.value)
      console.log('selectAll', val)
    }

    onMounted(() => {
      console.log('onMounted')
    })

    return {
      listWrap,
      list,
      scrollTable,
      scrollListener,
      showList,
      length,
      handleSelect,
      selections,
      select,
      selectAll,
    }
  },
})
</script>

总结

到此这篇关于el-table虚拟滚动的文章就介绍到这了,更多相关el-table虚拟滚动内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞!

原文出处:https://juejin.cn/post/7153537820622241806

[!--infotagslink--]

相关文章

  • el-table树形表格表单验证(列表生成序号)

    这篇文章主要介绍了el-table树形表格表单验证(列表生成序号),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-01
  • 关于Element-ui中el-table出现的表格错位问题解决

    使用ElementUI的el-table后,偶然发现出现行列错位、对不齐问题,下面这篇文章主要给大家介绍了关于Element-ui中el-table出现的表格错位问题解决的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下...2022-07-22
  • Vue如何动态修改el-table的某列数据

    这篇文章主要介绍了Vue如何动态修改el-table的某列数据,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2022-04-23
  • vue el-table实现递归嵌套的示例代码

    本文主要介绍了vueel-table实现递归嵌套的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2022-08-14
  • vue el-table 动态添加行与删除行的实现

    这篇文章主要介绍了vueel-table动态添加行与删除行的实现方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2022-07-26
  • vue.js el-table虚拟滚动完整实例代码

    这篇文章主要给大家介绍了关于el-table虚拟滚动的相关资料,文中通过实例代码介绍的非常详细,对大家学习或者使用vue.js具有一定的参考学习价值,需要的朋友可以参考下...2022-12-30
  • Element使用el-table组件二次封装

    这篇文章主要为大家介绍了Element使用el-table组件二次封装示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪...2022-06-24