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

antd组件select下拉数据分页加载

import React, { useState, useRef, useCallback } from "react";
import { Select, Spin } from "antd";const { Option } = Select;
const PAGE_SIZE = 20;// 模拟接口
const fetchUsers = (q, page) =>new Promise((resolve) => {setTimeout(() => {const total = 100; // 假设总条数const start = (page - 1) * PAGE_SIZE;const list = Array.from({ length: Math.min(PAGE_SIZE, total - start) },(_, i) => ({id: start + i + 1,name: `${q || "User"} ${start + i + 1}`,}));resolve({ list, hasMore: start + list.length < total });}, 600);});// 防抖工具
const debounce = (fn, delay) => {let timer;return (...args) => {clearTimeout(timer);timer = setTimeout(() => fn(...args), delay);};
};const RemoteSelect = () => {const [data, setData] = useState([]);const [value, setValue] = useState([]);const [loading, setLoading] = useState(false);const [hasMore, setHasMore] = useState(true);const [query, setQuery] = useState("");const pageRef = useRef(1);const fetchingRef = useRef(false);// 加载数据const loadData = useCallback(debounce(async (q, page, concat = false) => {if (fetchingRef.current) return;fetchingRef.current = true;setLoading(true);const { list, hasMore: more } = await fetchUsers(q, page);setData((prev) => (concat ? [...prev, ...list] : list));setHasMore(more);setLoading(false);fetchingRef.current = false;}, 300),[]);// 搜索时重置const handleSearch = (q) => {setQuery(q);pageRef.current = 1;setHasMore(true);loadData(q, 1);};// 滚动到底部加载更多const handlePopupScroll = (e) => {const { target } = e;if (hasMore &&!loading &&!fetchingRef.current &&target.scrollTop + target.clientHeight === target.scrollHeight) {pageRef.current += 1;loadData(query, pageRef.current, true);}};return (<Selectmode="multiple"placeholder="请输入关键字搜索"value={value}onChange={setValue}filterOption={false}onSearch={handleSearch}onPopupScroll={handlePopupScroll}notFoundContent={loading ? <Spin size="small" /> : null}style={{ width: 300 }}>{data.map((user) => (<Option key={user.id} value={user.id}>{user.name}</Option>))}{loading && hasMore && (<Option disabled><Spin size="small" /></Option>)}</Select>);
};export default RemoteSelect;

使用

import React from 'react';
import ReactDOM from 'react-dom';
import RemoteSelect from './RemoteSelect';ReactDOM.render(<RemoteSelect />, document.getElementById('root'));

http://www.dtcms.com/a/317158.html

相关文章:

  • 学习 Android (十六) 学习 OpenCV (一)
  • Cglib的Enhancer实现动态代理?
  • 新能源汽车热管理系统核心零部件及工作原理详解
  • AI巨模型对决2025:五强争霸,谁能称王?
  • ORACLE 19C建库时卡在46%、36%
  • 【网络运维】Linux:简单DHCP服务器的部署
  • PyTorch入门引导
  • 识别 Base64 编码的 JSON、凭证和私钥
  • 接口自动化测试用例详解
  • 使用python与streamlit构建的空间微生物分析
  • RabbitMQ 全面指南:从基础概念到高级特性实现
  • 控制服务和守护进程-systemctl
  • python学智能算法(三十四)|SVM-KKT条件回顾
  • 系统的缓存(buff/cache)是如何影响系统性能的?
  • 【学习笔记之redis】删除缓存
  • 【Redis】hash哈希,List列表
  • app-3
  • Python day36
  • Java Stream API 详解(Java 8+)
  • IP与MAC地址的区别解析
  • 数据仓库命名规范
  • AS32S601 芯片 ADC 模块交流耦合测试:技术要点与实践
  • 使用 gptqmodel 量化 Qwen3-Coder-30B-A3B-Instruct
  • 大型音频语言模型论文总结
  • 【前端开发】三. JS运算符
  • MCU程序段的分类
  • 异世界历险之数据结构世界(非递归快排,归并排序(递归,非递归))
  • 搭建私有 Linux 镜像仓库
  • 算法训练营DAY55 第十一章:图论part05
  • 图论(邻接表)DFS