AntD 可編輯行表格

本地數據代碼模板自用,官網例子改改

// 編輯行的自定義表格
import React, { useState } from "react";
import {Table,Input,InputNumber,Popconfirm,Form,Typography,Divider,
} from "antd";interface Item {key: string;name: string;age: number;address: string;
}const originData: Item[] = [];
for (let i = 0; i < 100; i++) {originData.push({key: i.toString(),name: `Edrward ${i}`,age: 32,address: `London Park no. ${i}`,});
}
interface EditableCellProps extends React.HTMLAttributes<HTMLElement> {editing: boolean;dataIndex: string;title: any;inputType: "number" | "text";record: Item;index: number;children: React.ReactNode;
}const EditableCell: React.FC<EditableCellProps> = ({editing,dataIndex,title,inputType,record,index,children,...restProps
}) => {const inputNode = inputType === "number" ? <InputNumber /> : <Input />;return (<td {...restProps}>{editing ? (<Form.Itemname={dataIndex}style={{ margin: 0 }}rules={[{required: true,message: `Please Input ${title}!`,},]}>{inputNode}</Form.Item>) : (children)}</td>);
};const EditableTable = () => {const [form] = Form.useForm();const [data, setData] = useState(originData);const [editingKey, setEditingKey] = useState("");const isEditing = (record: Item) => record.key === editingKey;const edit = (record: Partial<Item> & { key: React.Key }) => {form.setFieldsValue({ name: "", age: "", address: "", ...record });setEditingKey(record.key);};const cancel = () => {setEditingKey("");};// 刪除const deleteRow = (record: Item) => {setData(data.filter((item: Item) => item.key !== record.key));};const save = async (key: React.Key) => {try {const row = (await form.validateFields()) as Item;const newData = [...data];const index = newData.findIndex((item) => key === item.key);if (index > -1) {const item = newData[index];newData.splice(index, 1, {...item,...row,});setData(newData);setEditingKey("");} else {newData.push(row);setData(newData);setEditingKey("");}} catch (errInfo) {console.log("Validate Failed:", errInfo);}};const columns = [{title: "name",dataIndex: "name",width: "25%",editable: true,},{title: "age",dataIndex: "age",width: "15%",editable: true,},{title: "address",dataIndex: "address",width: "40%",editable: true,},{title: "operation",dataIndex: "operation",render: (_: any, record: Item) => {const editable = isEditing(record);return editable ? (<span><Typography.LinkonClick={() => save(record.key)}style={{ marginRight: 8 }}>Save</Typography.Link><Typography.LinkonClick={() => cancel()}style={{ marginRight: 8 }}>Cancel</Typography.Link></span>) : (<><Typography.Linkdisabled={editingKey !== ""}onClick={() => edit(record)}>Edit</Typography.Link><Divider type='vertical' /><Popconfirmtitle='Sure to delete?'onConfirm={() => deleteRow(record)}><Typography.Link disabled={editingKey !== ""}>Delete</Typography.Link></Popconfirm></>);},},];const mergedColumns = columns.map((col) => {if (!col.editable) {return col;}return {...col,onCell: (record: Item) => ({record,inputType: col.dataIndex === "age" ? "number" : "text", // 設置類型dataIndex: col.dataIndex,title: col.title,editing: isEditing(record),}),};});return (<Form form={form} component={false}><Tablecomponents={{body: {cell: EditableCell,},}}bordereddataSource={data}columns={mergedColumns}rowClassName='editable-row'pagination={{onChange: cancel,}}/></Form>);
};export default EditableTable;

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/247216.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/247216.shtml
英文地址,請注明出處:http://en.pswp.cn/news/247216.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

SharePoint 2013 - System Features

1. Embed Information & Convert to PDF 功能&#xff0c;在文檔的preview界面&#xff08;hover panel&#xff09;; 2. Share功能可以選擇是否發送郵件 -- Done 4. Shredded Storage, 將文檔的內容和每次的更改分開存儲&#xff0c;每次只存儲更改的內容&#xff0c;而不…

三心二意,助你好運?

經驗說&#xff1a;做事要專心致志。 實驗說&#xff1a;專心致志常常讓人缺少一雙發現的眼睛。 專心致志從來都被當做一個美德來歌頌。從來我們就認為要想成為偉大的人就必須要像牛頓老師那樣把鐘當成吃的放到鍋里煮才行&#xff0c;至少至少也得有能在集市上看書的本事。否則…

React Antd Upload自定義上傳customRequest

單獨的上傳圖片接口要傳參,action方式不太適合,需要使用自定義上傳customRequest覆蓋 公司代碼不可弄,就發一個可用的demo例子 import React, { useState } from "react"; import { render } from "react-dom"; import "antd/dist/antd.css"; i…

HDU 4109 Instrction Arrangement

題目鏈接&#xff1a;https://vjudge.net/problem/HDU-4109 題目大意 有 N 個指令&#xff0c;標號從 0 ~ N - 1&#xff0c;和 M 個指令間的先后關系&#xff0c;每個關系都有一個權值 w&#xff0c;表示后一個指令在前一個指令開始時間之后 w 納秒才開始執行。現在要并發執行…

養成一個習慣需要多久?

有人說養成一個習慣需要21天&#xff0c;有人說30天……養成習慣到底需要多少天&#xff1f; _hzw 2012-11-09 18:04又快到年末了。 年初時&#xff0c;你曾許諾要養成一些好習慣&#xff0c;比如要常運動、多讀書、少微博。有人說說養成一個習慣只需要堅持21天&#xff1b;21天…

vue3 eslint The template root requires exactly one element問題

vue項目中手動引入eslint npm install eslint --save-dev eslint初始化 npx eslint --init 運行時npm run lint 簡單語法問題--fix會自動修復 但是vue3項目出現只能有一個根節點的問題 原因在于 eslintrc.js里extends中plugin:vue/essential的問題 看eslint-plugin-vue官網…

你所知道的學習方法,都是錯的!

你所知道的學習方法&#xff0c;都是錯的&#xff01; 上課的時候記筆記&#xff1f;哪門功課不行&#xff0c;就集中精力專項突擊&#xff1f;自習的時候不要晃&#xff0c;選好一個地兒安安穩穩地待那兒學習&#xff1f;你還在這樣學習嗎&#xff1f;不要被騙了&#xff1a;這…

BZOJ 1683.City skyline 城市地平線

傳送門 從左到右掃一遍&#xff0c;考慮什么時候會和之前形成同一幢房子從而不用統計 顯然是當前的高度和之前某個點高度相同&#xff0c;并且它們之間沒有更矮的建筑 考慮用一個單調棧維護一個單調上升的房子輪廓&#xff0c;然后對于掃到的每一個高度&#xff0c;看看棧里有沒…

Vue3 eslint 編譯器宏和 defineProps,defineEmits,no-undef 規則警告

.eslintrc.js修改 定義全局globals module.exports {globals: {defineProps: readonly,defineEmits: readonly,defineExpose: readonly,withDefaults: readonly},env: {browser: true,es2021: true},extends: [plugin:vue/vue3-strongly-recommended, standard],parserOptio…

Java生鮮電商平臺-SpringCloud微服務開發中的數據架構設計實戰精講

Java生鮮電商平臺-SpringCloud微服務開發中的數據架構設計實戰精講 Java生鮮電商平臺: 微服務是當前非常流行的技術框架&#xff0c;通過服務的小型化、原子化以及分布式架構的彈性伸縮和高可用性&#xff0c;可以實現業務之間的松耦合、業務的靈活調整組合以及系統的高可用性…

JS兩個對象數組過濾掉相同的對象

let arr1 [{uid:1,name:張三},{uid:2,name:李四}]; let arr2 [{id:1,name:張三},{id:2,name:王五,age:23},{id:3,name:羅翔}]; 去掉arr2中包含的arr1相同name的對象: //ES6的方法&#xff0c;得到新數組 const newArr arr2.filter((item) > {return !arr1.some(ele>…

亞倫?斯沃茨:提升時間的品質

亞倫?斯沃茨&#xff1a;提升時間的品質 不休不眠地工作是低效的&#xff0c;怎樣發揮時間的最大效率&#xff1f; 伏維閣主 2013-01-18 16:45圖片來源&#xff1a;personalitytutor.com &#xff08;亞倫?斯沃茨Aaron Swartz / 文&#xff09;《 怎樣有效利用時間 》中給出的…

useEffect中使用異步函數

useEffect是不能直接用 async await 語法糖的 /* 錯誤用法 &#xff0c;effect不支持直接 async await*/useEffect(async ()>{/* 請求數據 */const res await getData()},[]) useEffect 的回調參數返回的是一個清除副作用的 clean-up 函數。因此無法返回 Promise&#xf…

Flex4_HttpService組件

1、在JavaWeb項目中新建Servlet(FlexLoginServelt) &#xff1a; public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html");response.setContentType("text…

想要提高記憶力?做夢去吧!

想要提高記憶力&#xff1f;做夢去吧&#xff01; 豆豆助 2010-11-26 11:47經驗說&#xff1a;不好好學習&#xff0c;就知道睡覺 實驗說&#xff1a;要想記憶好&#xff0c;睡覺不能少。做夢可以增強我們的記憶。 有沒有辦法能讓人不費吹灰之力增強記憶力&#xff1f; 做夢吧你…

This beta version of Typora is expired, please download and install a newer version. 解決方案

原文鏈接&#xff1a;https://blog.csdn.net/yyywxk/article/details/125133205 問題描述 今日打開 Typora 軟件時&#xff0c;突然報錯&#xff0c;提示軟件版本已過期&#xff0c;需要安裝新版本&#xff0c;如下圖所示。 關閉錯誤提示框后會自動跳轉到&#xff1a;https://…

MySQL查詢count(*)、count(1)、count(field)的區別收集

count(*)對行的數目進行計算&#xff0c;包含NULLcount(column)對特定的列的值具有的行數進行計算&#xff0c;不包含NULL值。 count()還有一種使用方式&#xff0c;count(1)這個用法和count(*)的結果是一樣的。 任何情況下select count(*) from tablename是最優選擇&#xff1…

真想學習?扔掉你詳細的計劃吧!

真想學習&#xff1f;扔掉你詳細的計劃吧&#xff01; 該不該定個計劃&#xff1f;詳細的計劃確實可以更高效&#xff0c;也許可以將你減肥的速度加快一倍。但是如果想學習&#xff0c;還是扔掉詳細的計劃吧&#xff01;因為它會占用你太多的認知資源&#xff0c;只顧著悶頭做事…

Swift中文件和圖片上傳處理

前言&#xff1a;基于Alamofire4.5網絡框架開發的Swift項目中上傳文件和圖片代碼參考。 上傳文件 extension HttpRequestService {typealias UploadDoneBlock (_ dict:[String : Any]) -> Void//成功blockfunc uploadEcgFileWithGatherRecord(model :RecordModel, fileName…

下載各種在線視頻及字幕

1. http://en.savefrom.net/ 支持30多個有名的網頁呢 &#xff0c;YouTube等等 Supported resourcesHow to use?filefactory.comsendspace.com youtube.commetacafe.combreak.comdailymotion.comvimeo.comsevenload.commail.rusmotri.comyandex.rurambler.rutvigle.ruintv.ru…