網站首頁 編程語言 正文
本文實例為大家分享了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-11-05 pytest官方文檔解讀之安裝和使用插件的方法_python
- 2022-08-27 C#使用百度Ueditor富文本框實現上傳文件_C#教程
- 2022-05-29 C++中的類的大小詳解_C 語言
- 2023-01-14 Go?庫bytes.Buffer和strings.Builder使用及性能對比_Golang
- 2022-09-07 R語言列篩選的方法select實例詳解_R語言
- 2022-09-09 Python利用Turtle繪畫簡單圖形_python
- 2022-06-28 詳解Python中遞歸函數的原理與使用_python
- 2022-05-26 Flutter自定義年月日倒計時_Android
- 最近更新
-
- 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同步修改后的遠程分支