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

ANTD-TABLE表格字段明细展示

文章目录

  • 1、字段渲染
  • 2、异步请求展示明细
  • 3、hover展示问题
    • 3.1 基本逻辑
    • 3.2 hover时长判断
    • 3.3 render+hover

表格字段明细展示,属于比较小的需求,但是也有一定交互细节,本文选取部分场景。

1、字段渲染

  • render和渲染组件是有区别的。
  • render常见为函数转化,利用页面和全局的变量state来渲染,最终返回Dom结构,偏向于展示和数据处理。函数参数和原来一致。
  • 渲染组件提供了自己的状态,有利于该字段的交互逻辑集中在组件中,尽量少利用外部变量。注意函数参数的变化,由props引入。
  • 命名方式略有不同,函数驼峰即可,组件大写字母开头+驼峰。
import { Table } from "antd";
import React, { useState } from "react";const RenderNameCom = ({ text }) => {const [loading, setLoading] = useState(false);return (<divonClick={() => {setLoading(true);setTimeout(() => {setLoading(false);}, 2000);}}>{loading ? "Loading..." : text}</div>);
};const renderNameFun = (text) => {const textLast = text > 10 ? "..." : text;return <div>{textLast}</div>;
};export const columns = [{title: "Name",dataIndex: "name",render: (text) => <RenderNameCom text={text} />,},{title: "Age",dataIndex: "age",render: renderNameFun,},
];const testPage = () => {return (<Table columns={columns} dataSource={[{ name: "John Doe", age: 32 }]} />);
};export default testPage;

2、异步请求展示明细

  • click点击比较常见,单次点击也比较保守和稳定,配合disabled(或者loading)保证同一时间不在接受触发事件。
  • 获取展开状态visible(新版本使用open属性),可以进行灵活判断,不再进行触发请求。
  • 如果没有disabled禁用,可能连续点击触发多次。
import { Table } from "antd";
import React, { useState } from "react";
import { Button, Popover, Spin, Divider } from "antd";const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));const RenderNameCom = ({ text }) => {const [show, setShow] = useState(false);const [loading, setLoading] = useState(false);const [data, setData] = useState({width: 0,height: 0,top: 0,left: 0,});const onClick = async () => {// 当未展开时,点击可以发起请求;展开后,默认会关闭,阻止多余请求if (show) {return;}try {setLoading(true);await delay(2000);setData({width: 100,height: 100,top: 100,left: 100,});console.log("clicked");setLoading(false);} catch (error) {console.log(error);setLoading(false);}};return (<Popovercontent={loading ? (<Spin />) : (<div><p>Name: {text}</p><Divider /><p>Width: {data?.width}</p><p>Height: {data?.height}</p><p>Top: {data?.top}</p><p>Left: {data?.left}</p></div>)}trigger="click"visible={show}onVisibleChange={(visible) => setShow(visible)}><Button type="link" onClick={onClick} disabled={loading}>{text}</Button></Popover>);
};export const columns = [{title: "Name",dataIndex: "name",render: (text) => <RenderNameCom text={text} />,},
];const testPage = () => {return (<Table columns={columns} dataSource={[{ name: "John Doe", age: 32 }]} />);
};export default testPage;

3、hover展示问题

3.1 基本逻辑

  • 用click的基础代码,改为hover触发基本完成任务
  • 问题在于hover存在鼠标滑过频率很快的问题,误触发概率很大。
import { Table } from "antd";
import React, { useState } from "react";
import { Button, Popover, Spin, Divider } from "antd";const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));const RenderNameCom = ({ text }) => {const [show, setShow] = useState(false);const [loading, setLoading] = useState(false);const [data, setData] = useState({width: 0,height: 0,top: 0,left: 0,});const onClick = async () => {console.log("clicked start");if (show) {return;}try {setLoading(true);await delay(2000);setData({width: 100,height: 100,top: 100,left: 100,});console.log("clicked");setLoading(false);} catch (error) {console.log(error);setLoading(false);}};return (<Popovercontent={loading ? (<Spin />) : (<div><p>Name: {text}</p><Divider /><p>Width: {data?.width}</p><p>Height: {data?.height}</p><p>Top: {data?.top}</p><p>Left: {data?.left}</p></div>)}trigger="hover"visible={show}onVisibleChange={(visible) => setShow(visible)}><Button type="link" onMouseEnter={onClick} disabled={loading}>{text}</Button></Popover>);
};export const columns = [{title: "Name",dataIndex: "name",render: (text) => <RenderNameCom text={text} />,},
];const testPage = () => {return (<Tablestyle={{ paddingTop: "100px" }}columns={columns}dataSource={[{ name: "John Doe", age: 32 }]}/>);
};export default testPage;

3.2 hover时长判断

判断鼠标hover时长,决定是否触发事件;基础代码模拟。

const HoverTimer = () => {const [loading, setLoading] = useState(false);const timer = useRef(null);const onMouseEnter = async () => {timer.current = setTimeout(async () => {try {setLoading(true);await delay(2000);console.log("clicked");setLoading(false);} catch (error) {console.log(error);setLoading(false);}}, 1000);};const onMouseLeave = () => {if (timer.current) {clearTimeout(timer.current);}};return (<Buttontype="link"onMouseEnter={onMouseEnter}onMouseLeave={onMouseLeave}loading={loading}>Hover me</Button>);
};

3.3 render+hover

  • hover会自动展开和关闭,可以不再设置show的状态。
  • 注意hover刚开始,定时器未执行,利用defaultData的初始状态进行设置loading
import { Table } from "antd";
import React, { useState, useRef } from "react";
import { Button, Popover, Spin, Divider } from "antd";const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));const RenderNameCom = ({ text }) => {const [loading, setLoading] = useState(false);const defaultData = {width: 0,height: 0,top: 0,left: 0,};const [data, setData] = useState(defaultData);const timer = useRef(null);const onMouseEnter = async () => {console.log("clicked start");setData(defaultData); // 同步清空timer.current = setTimeout(async () => {try {setLoading(true);await delay(2000);setData({width: 100,height: 100,top: 100,left: 100,});console.log("clicked");setLoading(false);} catch (error) {console.log(error);setLoading(false);}}, 1000);};const onMouseLeave = () => {if (timer.current) {clearTimeout(timer.current);}};return (<Popovercontent={loading || data?.width === 0 ? (<Spin />) : (<div><p>Name: {text}</p><Divider /><p>Width: {data?.width}</p><p>Height: {data?.height}</p><p>Top: {data?.top}</p><p>Left: {data?.left}</p></div>)}trigger="hover"><Buttontype="link"onMouseEnter={onMouseEnter}onMouseLeave={onMouseLeave}disabled={loading}>{text}</Button></Popover>);
};export const columns = [{title: "Name",dataIndex: "name",render: (text) => <RenderNameCom text={text} />,},
];const testPage = () => {return (<div><Tablestyle={{ paddingTop: "100px" }}columns={columns}dataSource={[{ name: "John Doe", age: 32 }]}/></div>);
};export default testPage;
http://www.dtcms.com/a/362357.html

相关文章:

  • (Redis)Redis 分布式锁及改进策略详解
  • UE5 为啥原生的NotifyState写逻辑会有问题
  • Java异常处理详解:掌握try-catch-finally与try-with-resources,避开空指针等踩坑点
  • 20250901的学习笔记
  • 全栈智算系列直播回顾 | 智算中心对网络的需求与应对策略(下)
  • 【LeetCode】3670. 没有公共位的整数最大乘积 (SOSDP)
  • 笔记:人工神经网络
  • Vue基础知识-Vue中:class与:style动态绑定样式
  • DiffusionGPT-LLM驱动的文本生成图像系统
  • OpenStack网络类型解析
  • Markdown 语法全面指南
  • EXPLAIN 和 EXPLAIN ANALYZE
  • 【AI报表】JimuReport 积木报表 v2.1.3 版本发布,免费可视化报表和大屏
  • Python 爬虫案例:爬取豆瓣电影 Top250 数据
  • 【开题答辩全过程】以 基于SSM的高校疫情防控管理系统为例,包含答辩的问题和答案
  • docker中的命令(六)
  • 轻量实现 OCPP 1.6 JSON 协议(欧洲版)的充电桩调试平台
  • AI使用指南:9月开学季,自动生成教学PPT
  • C++ 用于运行时类型识别的typeinfo库使用指南
  • 飞致云开源社区月度动态报告(2025年8月)
  • 苍穹外卖项目实战(日记十三)-记录实战教程及问题的解决方法-(day3-5) 修改菜品功能实现
  • C# FlaUI win 自动化框架,介绍
  • 用只能以关键字指定和只能按位置传入的参数来设计清晰的接口(Effective Python 第25条)
  • 利用 DrissionPage 精准获取淘宝商品描述:Python 爬虫实战指南
  • shell之扩展
  • 奇瑞QQ的后轮制动器设计cad+三维图+设计说明书
  • 【Java】谈谈IdentityHashMap
  • 前阿里专家揭秘:你对中国十大GEO专家的认知,99%都是错的
  • 苹果ipa应用安装包ios系统闪退问题
  • 携程旅行网景区,评论数据爬虫项目数据库保存附源码