el-table实现表格滚动上拉加载更多(Vue3)

一、在src目录下新建一个directive文件夹,在里面新建一个index.ts
// 全局自定义指令
const GlobalDirective = (app: any) => {
    // 表格滚动上拉加载
    app.directive('loadmore', {
        mounted(el: any, binding: any, vnode: any, prevVNode: any) {
            const scrollVNode = el.querySelector('.el-scrollbar__wrap')
            // 表格滚动
            let currentScroll = 0,
                lastScroll = 0,
                isDownScroll = true
            scrollVNode.addEventListener(
                'scroll',
                function (event: any) {
                    // 判断是否向上滚动
                    // debugger
                    currentScroll = event.target.scrollTop
                    if (lastScroll < currentScroll) {
                        isDownScroll = true
                    } else {
                        isDownScroll = false
                    }
                    lastScroll = currentScroll
                    // 定一个距离,距离底部当前距离时候触发加载事件
                    const sign = 10
                    // 判断是否到底
                    const scrollDistance = event.target.scrollHeight - event.target.scrollTop - event.target.clientHeight
                    if (scrollDistance <= sign && isDownScroll) {
                        binding.value.eventH()
                    }
                },
                false
            )
        }
    });
}

export default GlobalDirective


二、main.ts中全局引用
// 引入全局指令
import GlobalDirective from '@/plugins/directive/directive'

const app = createApp(App);

GlobalDirective(app)
三、.vue文件中使用
 
<el-table
        v-loadmore="{ eventH: () => loadMore() }"
        :data="state.tableData"
        :height="
            state.tableData.length * 54 >= state.height
              ? state.height + 'px'
              : (state.tableData.length * 54 < 270 ? 270 : state.tableData.length * 54) + 'px'
          "
        style="width: 100%; border-radius: 4px; overflow: auto"
      >

const state = reactive({
  height: null,
  isFinished: false, // 是否所有已加载完
  tableData: [],
  total: 0
})
const pageParams: any = ref({
  page: 1,
  rows: 5 //一页5条数据
});




// 获取数据
const getDataList = async (isLoadmore: boolean = false) => {
  const res = await getList({
    ...params.value,
    ...pageParams.value
  });
  // debugger
  if(isLoadmore){
    // debugger
    state.tableData=state.tableData.concat(res?.rows || [])
  }else{
    state.tableData=res.rows || []
    // debugger
  }
  state.total=res.total;    
  // 是否所有数据已加载完
  state.isFinished = state.total <= state.tableData.length
};

// 加载
let num = 0 // 需要定义一个变量来存储请求次数,防止重复请求
const loadMore = async () => {
  // 防止重复加载
    num++
  if (num <= 1 && !state.isFinished) {
    pageParams.value.page++;
    await getDataList(true)
    num = 0
  }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值