当前位置: 首页 > news >正文

vue 2表格滚动加载

自定义指令

// 表格下拉滚动加载更多
Vue.directive('loadmore', {
  bind(el, binding) {
    // 节流函数
    const throttle = (fn, wait = 300) => {
      let inThrottle, lastFn, lastTime;
      return function() {
        const context = this,
              args = arguments;
        if (!inThrottle) {
          fn.apply(context, args);
          lastTime = Date.now();
          inThrottle = true;
        } else {
          clearTimeout(lastFn);
          lastFn = setTimeout(function() {
            if (Date.now() - lastTime >= wait) {
              fn.apply(context, args);
              lastTime = Date.now();
            }
          }, Math.max(wait - (Date.now() - lastTime), 0));
        }
      };
    };

    const selectWrap = el.querySelector('.el-table__body-wrapper');
    if (selectWrap) {
      const handleScroll = throttle(function() {
        let sign = 100;
        const scrollDistance = this.scrollHeight - this.scrollTop - this.clientHeight;
        if (scrollDistance <= sign) {
          // 触发加载更多
          binding.value();
        }
      }, 300);

      // 使用passive监听器提高滚动性能
      selectWrap.addEventListener('scroll', handleScroll, { passive: true });
    }
  }
});


<el-table
                        v-loading="loading"
                        :data="rankingData"
                        v-loadmore="loadMore"
                        height="525"
                        style="width: 100%;"
                        border
                    >
                        <el-table-column
                            prop="ranking"
                            label="排名"
                            width="100"
                            align="center"
                        />
                        <el-table-column
                            prop="loginTimes"
                            label="登录次数"
                            min-width="100"
                            align="center"
                        ></el-table-column>
                    </el-table>
// 滚动加载
        loadMore() {
            if (this.total > this.rankingData.length) {
                this.query.pageNum++;
                this.getRankingList();
            }
        }


// 去除重复数据
                const dataList = data.result;
                if (dataList.length) {
                    // 使用 Set 来去重
                    const appIds = new Set(this.rankingData.map(data => data.appId));
                    const notExistData = dataList.filter(data =>
                        !appIds.has(data.appId));
                    const rankingData = [...this.rankingData, ...notExistData];
                    // 升序排列
                    this.rankingData = rankingData.sort((a, b) =>
                        a.ranking - b.ranking);
                    this.total = data.count;
                    this.showMoreHint = this.total > this.rankingData.length;
                }


http://www.mrgr.cn/news/29726.html

相关文章:

  • 【VUE】快速上手
  • 心觉:不能成事的根本原因
  • 龙海家园的免费停车点探寻
  • 【C语言】带你手把手拿捏指针(3)(含转移表)
  • 中、美、德、日制造业理念差异
  • Linux常见查看文件命令
  • 数学学习记录
  • vue获取最近7天时间;获取任意时间段时间
  • Flutter 优化技巧分享
  • 基于python+django+vue的医院预约挂号系统
  • Mobile net V系列详解 理论+实战(1)
  • python中网络爬虫框架
  • 拦截器Interceptor
  • 一些学习three的小记录
  • A股上市公司企业创新能力、质量、效率-原始数据+dofile+结果(2006-2023年)
  • 策略模式结合反射在电商支付系统中的深入应用与实战解析
  • SIP Servlets学习
  • Android RecyclerView 缓存机制深度解析与面试题
  • C++调用C# DLL之踩坑记录
  • 实习项目|苍穹外卖|day11