element-plus Table 虚拟滚动

背景:dom加载太多,导致动画卡顿。

解决方案一:使用分页、Virtualized Table

解决方案二:监听触底和触顶,懒加载数据 固定dom节点数量

现在使用解决方案二实现一下:

<template>
  <el-table ref="TableRef" id="TableId" :data="sliceTable" stripe style="width: 100%" :height="320">
    <el-table-column prop="Index" label="序号" width="180" />
    <el-table-column prop="date" label="Date" width="180" />
    <el-table-column prop="address" label="Address" />
  </el-table>
</template>

<script setup>
let tableData = [];
const state = reactive({
  startIndex: 0,
  endIndex: 0,
  showDataLength: 20
})
for (let i = 1; i <= 100; i++) {
  tableData[i] = {
    Index: i,
    date: '2016-05-03',
    address: 'No. 189, Grove St, Los Angeles',
  }

}

const sliceTable = computed(() => {
  return tableData.slice(state.startIndex, state.startIndex + state.showDataLength)
})
const TableRef = ref();
// 滚动行为
function scrollBehavior(e) {
  // 清除之前的定时器
  clearTimeout(timer)

  // 设置新的定时器
  timer = setTimeout(() => {
    // 滚动方向判定
    const scrollDirection = e.deltaY > 0 ? 'down' : 'up'
    // 获取提供实际滚动的容器
    const dom = TableRef.value.$refs.bodyWrapper.getElementsByClassName('el-scrollbar__wrap')[0]

    const { clientHeight, scrollTop, scrollHeight } = dom;
    if (scrollDirection === 'down') {

      // 父容器高度 + 子容器距离父容器顶端的高度 = 子容器可滚动的高度
      if (clientHeight + scrollTop === scrollHeight) {
        if (state.startIndex < tableData.length - state.showDataLength) {
          state.startIndex += state.showDataLength;
          dom.scrollTop = 0;
        }
      }
    } else {
      if (scrollTop == 0) { // 滚动到顶部

        if (state.startIndex >= state.showDataLength) {
          state.startIndex -= state.showDataLength;
          dom.scrollTop = 0; // 将滚动位置重置到底部
        }
      }
    }
  }, debounceTime)
}

const debounceTime = 500
let timer = null

onMounted(() => {
  // 挂载
  TableRef.value && TableRef.value.$refs.bodyWrapper.addEventListener('mousewheel', scrollBehavior)
})

onUnmounted(() => {
  // 卸载
  TableRef.value && TableRef.value.$refs.bodyWrapper.removeEventListener('mousewheel', scrollBehavior)
})

</script>
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以的,以下是一个使用 Vue3 和 Element Plus 实现表格分页的示例: ``` <template> <div class="table-container"> <el-table :data="tableData" :page-size="pageSize" :current-page="currentPage" @current-change="handleCurrentChange"> <el-table-column prop="id" label="ID"></el-table-column> <el-table-column prop="name" label="Name"></el-table-column> <el-table-column prop="address" label="Address"></el-table-column> </el-table> <el-pagination :page-size="pageSize" :total="total" :current-page.sync="currentPage"></el-pagination> </div> </template> <script> import { ref } from 'vue' import { getTableData } from '@/api/table' export default { setup() { const tableData = ref([]) const currentPage = ref(1) const pageSize = ref(10) const total = ref(0) const fetchData = async () => { const { data } = await getTableData(currentPage.value, pageSize.value) tableData.value = data.list total.value = data.total } const handleCurrentChange = (page) => { currentPage.value = page fetchData() } fetchData() return { tableData, currentPage, pageSize, total, handleCurrentChange } } } </script> <style scoped> .table-container { margin: 20px; } </style> ``` 这个示例使用了 `el-table` 和 `el-pagination` 组件来实现表格和分页功能。我们通过 `ref` 创建了响应式变量 `tableData`、`currentPage`、`pageSize` 和 `total`。在 `setup` 函数中,我们定义了一个 `fetchData` 函数来获取表格数据,然后在组件初始化时调用该函数。我们还定义了一个 `handleCurrentChange` 函数来处理分页组件的页码变化事件。在模板中,我们使用了这些变量和函数来渲染表格和分页组件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值