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

 更新时间:2022年12月29日 11:28:07   作者:CryptoCode  
这篇文章主要给大家介绍了关于el-table虚拟滚动的相关资料,文中通过实例代码介绍的非常详细,对大家学习或者使用vue.js具有一定的参考学习价值,需要的朋友可以参考下

前言

基于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虚拟滚动内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • vue实现树形结构样式和功能的实例代码

    vue实现树形结构样式和功能的实例代码

    这篇文章主要介绍了vue树形结构样式和功能,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-10-10
  • 浅谈vue 移动端完美适配方案

    浅谈vue 移动端完美适配方案

    最近接触了一个项目,vue怎么在不同屏幕上做根据不同屏幕大小适配,本文就详细的来介绍一下,感兴趣的可以了解一下
    2021-09-09
  • vue中axios请求的封装实例代码

    vue中axios请求的封装实例代码

    这篇文章主要给大家介绍了关于vue中axios请求封装的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用vue具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧要的朋友可以参考下
    2019-03-03
  • 详解vue的双向绑定原理及实现

    详解vue的双向绑定原理及实现

    这篇文章主要介绍了vue双向绑定原理及实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-05-05
  • 基于element日历组件实现签卡记录

    基于element日历组件实现签卡记录

    这篇文章主要为大家详细介绍了基于element日历组件实现签卡记录,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-08-08
  • 实现一个Vue版Upload组件

    实现一个Vue版Upload组件

    这篇文章主要介绍了实现一个Vue版Upload组件,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-08-08
  • 记录vue项目中遇到的一点小问题

    记录vue项目中遇到的一点小问题

    本文是脚本之家小编给大家收藏整理的关于vue项目中遇到的一点小问题,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-05-05
  • 一文带你简单理解Vue的data为何只能是函数

    一文带你简单理解Vue的data为何只能是函数

    如果data是一个函数的话,这样每复用一次组件,就会返回一份新的data,下面这篇文章主要给大家介绍了关于简单理解Vue的data为啥只能是函数的相关资料,需要的朋友可以参考下
    2022-10-10
  • 详解vue-cli 3.0 build包太大导致首屏过长的解决方案

    详解vue-cli 3.0 build包太大导致首屏过长的解决方案

    这篇文章主要介绍了详解vue-cli 3.0 build包太大导致首屏过长的解决方案,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-11-11
  • 浅谈vue自定义全局组件并通过全局方法 Vue.use() 使用该组件

    浅谈vue自定义全局组件并通过全局方法 Vue.use() 使用该组件

    本篇文章主要介绍了vue自定义全局组件并通过全局方法 Vue.use() 使用该组件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-12-12

最新评论