網站首頁 編程語言 正文
本文實例為大家分享了react+antd動態編輯表格數據的具體代碼,供大家參考,具體內容如下
在項目中,我們會遇到一種需求,為用戶提供一份表格去編輯,而且表格中各個單元格是相關影響的,因此在一個單元格中編輯過會影響另外一個單元格。
小需求
在一個表格中:
1.有兩行數據,一行是數據1,一行是數據2;
2.而且只能數據1的單元格可以進行編輯;
3.只能輸入數字,要是輸入其他的,則顯示編輯之前的數值
4. 當數據1的那行數據發生改變的時候,數據2對應的單元格的數據也會自動加1
例子圖片
示例代碼
import React, { useState } from 'react';
import {Table, Typography, message} from 'antd'
const { Paragraph } = Typography;
function Index(props){
? // 判斷是否時數字
? function isNumber(val){
? ? ? var regPos = /^\d+(\.\d+)?$/; //非負浮點數
? ? ? var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //負浮點數
? ? ? if(regPos.test(val) || regNeg.test(val)){
? ? ? ? ? return true;
? ? ? }else{
? ? ? ? ? return false;
? ? ? }
? }
? // 表格數據源
? const [ data , setData ] = useState([
? ? {
? ? ? 'num1':1
? ? },
? ? {
? ? ? 'num1':2
? ? },
? ? {
? ? ? 'num1':3
? ? },
? ? {
? ? ? 'num1':4
? ? },
? ? {
? ? ? 'num1':5
? ? }
? ])
? // 定義表頭
? const columns = [
? ? {
? ? ? title: '數據1',
? ? ? dataIndex: 'num1',
? ? ? key: 'num1',
? ? ? render: (text, record, index) => (
? ? ? ? ? <Paragraph editable={{onChange:(value)=>{
? ? ? ? ? ? let date = 0
? ? ? ? ? ? // 這里是:只能輸入數字,要是輸入其他的值,則提示并且顯示編輯之前的值
? ? ? ? ? ? if(isNumber(value)){
? ? ? ? ? ? ? ? date = value
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? if(value != ''){
? ? ? ? ? ? ? ? ? ? message.warn('只能輸入數字')
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? date = data[index]['num1']
? ? ? ? ? ? }
? ? ? ? ? ? // 處理表格中的值,使得動態更新表格中的值
? ? ? ? ? ? let obj = [...data]
? ? ? ? ? ? setData([])
? ? ? ? ? ? obj[index]['num1'] = parseInt(date)
? ? ? ? ? ? setData(obj)
? ? ? ? ? }}} value={text}>{text}</Paragraph>
? ? ? ),
? ? },
? ? {
? ? ? title: '數據2',
? ? ? dataIndex: 'num2',
? ? ? key: 'num2',
? ? ? render: (text, record, index) => (
? ? ? ? ? <span>{record['num1'] + 1}</span>
? ? ? ),
? ? },
? ]
? return (
? ? <div>
? ? ? ? <Table dataSource={data} columns={columns} />
? ? </div>
? )
}
export default Index
總結
這篇博文就簡單的分享到這里,這里只是一個基礎。學會了這個還可以根據這個屬性造很多自己的輪子。
原文鏈接:https://blog.csdn.net/qq_35905501/article/details/120343985
相關推薦
- 2022-10-24 解析Golang中引用類型是否進行引用傳遞_Golang
- 2022-08-20 ORACLE中dbms_output.put_line輸出問題的解決過程_oracle
- 2023-02-17 python中列表推導式與生成器表達式對比詳解_python
- 2022-08-11 C#實現快速查詢文件的方法_C#教程
- 2023-05-10 Numpy中np.dot與np.matmul的區別詳解_python
- 2023-03-25 Python?flask?框架使用flask-login?模塊的詳細過程_python
- 2022-07-19 Python數據分析?Numpy?的使用方法_python
- 2022-07-10 Ubuntu 硬盤分區
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支