首页 > 其他分享 >ProTable 如何做到下拉滚动加载数据

ProTable 如何做到下拉滚动加载数据

时间:2024-10-17 16:33:02浏览次数:1  
标签:const title ProTable setLoading hasMore cellNumber params 滚动 加载

1、这里主要是什么呢

这里的关键是$('.ant-table-body').on('scroll', handleScrollEvent); 监听滚动条事件。

1、然后再reuqest 里面设置,是因为ProTable 点击查询时,会进到这里。

2、监听page,是当滚动导致页数,发生变化时,重新加载数据!

import React, { useRef,useEffect, useState } from 'react';
import ProTable from '@ant-design/pro-table';
import documentUtil from '../js/documentUtil';
import {IncomingInspectionBatchView} from './incomingInspectionBatchView';
const $ = require('jquery');

const columns = [
    { "title": "序号", "dataIndex": "seq", "width": 60  , hideInSearch: true},
    {"title": "xxxx", "dataIndex": "materialName", "width": 100},
    {"title": "xxxx", "dataIndex": "materialNumber", "width": 100},
    {"title": "xxxx", "dataIndex": "processName", "width": 100},
    {"title": "xxx", "dataIndex": "feedingBatch", "width": 100},
    {"title": "xxxx", "dataIndex": "equipmentUsed", "width": 100},
    {"title": "xxxx", "dataIndex": "incomingInspectionBatch", "width": 100}
];

const hasMore = {}
export const MaterialDetailsUI = (props) => {
    const {cellNumber} = props
    const [dataSource, setDataSource] = useState([]);
    const [scrollY, setScrollY] = useState(400);
    const [loading,setLoading] = useState(false);
    const [queryValue,setQueryValue] = useState({});
    const [page, setPage] = useState(1);
    const formRef = useRef();  // 创建表单引用

    useEffect(() => {
        documentUtil.initHandleResize(setScrollY)
        const handleScrollEvent = (e) => documentUtil.handleScroll(e, loading,hasMore,setPage);
        $('.ant-table-body').on('scroll', handleScrollEvent);
        hasMore.current = true
        return () => {
            window.removeEventListener('resize',documentUtil.setTableHeight(setScrollY));
        };
      }, []);

      useEffect(() => {
        async function loadDataFunction(params) {
            documentUtil.loadData("getMaterialDetails",page,cellNumber,setLoading,dataSource,setDataSource,hasMore,queryValue);
        }
        if(page != 1){
            loadDataFunction()
        }
      }, [page]);
    return (
        <div id="bomProTableParentId">
            <ProTable
                formRef={formRef}  // 将表单引用传递给 ProTable
                columns={columns}
                dataSource={dataSource}
                request={(params, sorter, filter) => {
                    setQueryValue(params)
                    setPage(1)
                    documentUtil.loadData("getMaterialDetails",1,cellNumber,setLoading,dataSource,setDataSource,hasMore,params,true);
                }}
                rowKey="seq"
                pagination={false}
                search={{ 
                    labelWidth: 'auto' 
                }}
                loading={loading}
                scroll={{ y: 1000, y: scrollY }}
                headerTitle="用料明细"
            />
        </div>
    );
};


export default {
    MaterialDetailsUI
};

2、这里加上防抖函数,因为如果不加上防抖,会导致。

 // 防抖函数
 const debounce = (func, delay) => {
    let timeoutId;
    return (...args) => {
    if (timeoutId) clearTimeout(timeoutId);
    timeoutId = setTimeout(() => {
        func.apply(null, args);
    }, delay);
    };
};

export const handleScroll = debounce((e,loading,hasMore,setPage) => {
    const { scrollTop, clientHeight, scrollHeight } = e.currentTarget;
    if (scrollHeight - scrollTop <= clientHeight + 10 && !loading && hasMore.current) {
        setPage((prev) => prev + 1);
    }
}, 200); // 防抖时间为200ms

export const loadData = async (method,currentPage,cellNumber,
    setLoading,dataSource,setDataSource,hasMore,param,isRequest) => {
    // 假设 fetchData 是一个获取数据的 API
    if(currentPage == 3){
        console.log("测试....");
    }
    const newData = await getData(method,param,cellNumber,setLoading,currentPage);
    if (newData.length === 0) {
        if(isRequest){
            setDataSource(newData)
        }
        hasMore.current = false; // 确定是否还有更多数据可加载
    } else {
        if(newData.length < Constant.pageSize){
            hasMore.current = false;
        }else{
            hasMore.current = true
        }
        if(isRequest){
            setDataSource(newData)
            return
        }else{
            // 提取当前数据的 id 到一个 Set 中,以便于快速查找
            const existingSeqs = new Set(dataSource.map(item => item.seq));
            // 过滤掉 newData 中已有 id 的项
            const filteredNewData = newData.filter(item => !existingSeqs.has(item.seq));
            // 合并现有数据和过滤后的新数据
            const newTableData = [...dataSource, ...filteredNewData]
            console.log("newTableData = ",newTableData);
            setDataSource(newTableData)
        }
    }
};


async function getData(method,params,cellNumber,setLoading,page){
    let data = []
    try {
        setLoading(true)
        const url = "http://"+Constant.urlHead+"/api/mes/xxx/"+method
        params['cellNumber'] = cellNumber
        params['current'] = page
        params['pageSize'] = Constant.pageSize
        stringUtil.removeExtraSpaces(params)
        const result = await axios.post(url,params);
        if(result.data.code == 200){
            data = result.data.data
        }
    } catch (error) {
        console.log("error = ",error);
        message.error(error.message || 'Something went wrong')
    }finally{
        setLoading(false)
        return data
    }
}

  

 

 

 

 

  

 

标签:const,title,ProTable,setLoading,hasMore,cellNumber,params,滚动,加载
From: https://www.cnblogs.com/wwssgg/p/18472596

相关文章

  • 滑动阻尼,惯性滚动列表,边界回弹,惯性回弹
    https://juejin.cn/post/7426280686695759882<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0">&......
  • 如何在CSS中修改滚动条样式
    修改滚动条样式在CSS中是一个有趣而常见的需求,特别是当你希望网页设计更加个性化时。虽然并不是所有浏览器都完全支持修改滚动条样式,但我们可以使用一些专门的CSS选择器来控制滚动条外观。以下是一个详细讲解,适合整理成博客发布。如何在CSS中修改滚动条样式在网页设计中,......
  • 当 smartcardsimulator.dll 加载错误时的解决策略
    smartcardsimulator.dll文件通常与智能卡模拟器或智能卡相关的应用程序有关。智能卡模拟器是一种软件工具,用于模拟智能卡的行为,以便开发人员测试和调试智能卡应用。smartcardsimulator.dll文件负责处理智能卡模拟的相关功能。当您看到“smartcardsimulator.dll加载错误”......
  • 2024-10-16 前端图片加载方式优化(webp)
    把上传的图片文件格式转化为webp图片格式。前置条件:图片文件存在阿里云oss。阿里云的对象存储服务(OSS)支持在URL中直接指定图片处理参数,这样可以在不下载原始图片到服务器的情况下,直接由OSS服务端处理图片。假设你有一张存在阿里云的图片链接,那么只需要在该链接的末尾加上?x......
  • python批处理,一键打开vscode窗口,分别加载jeecg前后端项目,并运行前后端服务.
    importsubprocess#VsCode打开后端项目cmd1=["code","D:\pro\JeecgBoot-v3.7.1\jeecg-boot"]process1=subprocess.Popen(cmd1,stdout=subprocess.PIPE,shell=True)output1,_=process1.communicate()print(output1.decode('utf-8')......
  • 关于Gmap.Net在WPF中的运用笔记(一)初步加载高德地图
    一、前言最近公司需要开发一个车辆在途轨迹追踪的软件,结合现有系统和技术体系,最终敲定使用WPF+Gmap.Net来实现,这里将一些坑踩一下,做个笔记记录一下。二、项目搭建本项目基于.Net6.0+Gmap.Net.Core+Gmap.Net.WinPresentation,前面是用到的框架版本,后面则是需要用到的地图包,可通......
  • 优化el-popover在列表等地方使用时,会大量渲染,造成页面首次加载卡顿、加载时间长的问题
    vue2项目中,在列表等需要循环渲染的地方,使用el-popover时,数据量大了以后,会造成页面卡顿。解决方案:基于el-popver二次封装 <template><divclass="my-popover-container"><spanref="referenceRef"class="comp-reference"@click="triggerPop"......
  • vue 动态加载路由,渲染左侧菜单栏
    需求我们在route文件中定义的路由是由子路由包裹进去的,它可能是无限级的。如何在vue的模板中渲染形成菜单栏。如图: 解决方法将菜单栏单独写成子组件(注意头部标签:element-plus中是el-menu)仍然在父组件中。将配置路由数据传入到子组件。子组件渲染一级路由。一级路由再调用......
  • 数据结构与算法篇(回溯&递归&分治 - 刷题篇)(目前一天图片上传太多加载不出来)(后续更新)
    目录1.没有重复项数字的全排列(中等)1.1.题目描述1.2解题思路1.3代码实现方法一:递归方法二:非递归版2.有重复项数字的全排列(中等)2.1.题目描述2.2.解题思路2.3.代码实现递归+回溯(推荐使用)3.岛屿数量(中等)3.1.题目描述3.2.解题思路3.3代码实现方法一:dfs......
  • 远程代码加载方案
    原理通过http获取到远程代码,并下载加来替换掉本次使用的代码编辑器启动本地服务器(代码示例)#if!NO_HYBRIDCLRusingHybridCLR;#endifusingSystem;usingSystem.Reflection;usingUnityEngine;///<summary>///启动流程-加载并应用dll///</summary>publicclass......