網(wǎng)站首頁 編程語言 正文
如何實(shí)現(xiàn)在當(dāng)前表格直接更改數(shù)據(jù)
需求
用戶點(diǎn)擊修改按鈕時直接在彈出框的當(dāng)前頁面內(nèi)直接再次修改點(diǎn)擊行相關(guān)信息:
效果如下
點(diǎn)擊修改當(dāng)事人信息時,直接將當(dāng)前改為輸入框,并將信息展示,同時操作欄內(nèi)的內(nèi)容變?yōu)楸4婧腿∠?
具體做法
我這里是使用的antd組件內(nèi)的可編輯表格;當(dāng)然原生的也可以做,以前也做過;
這里的關(guān)鍵是點(diǎn)擊修改按鈕時,令當(dāng)前行的表格變?yōu)檩斎肟颍⒄故緮?shù)據(jù);
給數(shù)據(jù)每一項(xiàng)加上 editable: true屬性,并通過該屬性控制 渲染的是數(shù)據(jù)還是可修改的輸入框
這里是使用的useState()方法來進(jìn)行狀態(tài)控制的;
- 關(guān)于 useState 的用法是,需要傳入一個參數(shù)作為狀態(tài)的初始值,當(dāng)函數(shù)執(zhí)行后會返回兩個值,一個是當(dāng)前狀態(tài)的屬性,一個是修改狀態(tài)的方法。
- 使用方法更新數(shù)據(jù)后會觸發(fā)render()重新渲染數(shù)據(jù)
const [editingKey, setEditingKey] = useState('');
// 是否正在修改
const isEditing = (record: Item) => record.key === editingKey;
// 修改按鈕
const edit = (record: Item) => {
form.setFieldsValue({ ...record });
setEditingKey(record.key);
};
// 取消
const cancel = () => {
setEditingKey('');
};
// 保存
const save = async (id: React.Key) => {
try {
const row = (await form.validateFields())
console.log('row', row)
row.id = id
onSave(row)
setEditingKey('');
} catch (err) {
console.log(err)
}
};
我這里給useState一個初始值為空,點(diǎn)擊修改后使用setEditingKey()方法(useState返回的方法)將useState數(shù)據(jù)的值賦值為當(dāng)前行的唯一key值,這樣二者相等,就可以區(qū)別點(diǎn)擊的是哪一條數(shù)據(jù)的按鈕了;點(diǎn)擊取消setEditingKey(’’)重新置空;
判斷邏輯:
// 是否正在修改
const isEditing = (record: Item) => record.key === editingKey;
渲染數(shù)據(jù)前進(jìn)行判斷:
const mergedColumns = columns.map(col => {
if (!col.editable) {
return col;
}
return {
...col,
onCell: (record: Item) => ({
record,
dataIndex: col.dataIndex,
title: col.title,
editing: isEditing(record),
}),
};
});
根據(jù)數(shù)據(jù)狀態(tài)判斷渲染的是表格合適輸入框:
const EditableCell: React.FC<EditableCellProps> = ({
editing,
dataIndex,
title,
record,
index,
children,
...restProps
}) => {
return (
<td {...restProps}>
{editing ? (
<Form.Item
name={dataIndex}
style={{ margin: 0 }}
rules={[
{
required: true,
message: `請?zhí)顚?{title}!`,
},
]}
>
<Input />
</Form.Item>
) : (
children
)}
</td>
);
};
導(dǎo)出:
return (
<Form form={form} component={false}>
<Table
components={{
body: {
cell: EditableCell,
},
}}
bordered
pagination={false}
dataSource={dataSource}
{...otherProps}
columns={mergedColumns}
rowClassName="editable-row"
/>
</Form>
);
其中dataSource為數(shù)據(jù)源,
功能實(shí)現(xiàn)。
useState修改對象的字段
首先定義一個空對象
const [dataSelect, setDataSelect] = React.useState({})
給這個對象附上不同值,但不會把原來的覆蓋的掉
const select = (e, item, type) => {
const data = { ...dataSelect }
if (type == 'price') {
setSelectNO(e)
data.min_price = item.min_price
data.max_price = item.max_price
setDataSelect(data)
console.log(data)
return
}
if (type == 'optionsCity') {
setCity(e)
data.city = item.text
setDataSelect(data)
console.log(data)
return
}
}
原理用一個第三方的值,作為中間變量。每次都是附上最新的data。
原文鏈接:https://blog.csdn.net/weixin_44134588/article/details/111028654
相關(guān)推薦
- 2022-06-18 深入解析Go?變量字符串與字符編碼問題_Golang
- 2022-07-26 Spring底層核心原理解析
- 2022-08-10 C#并行編程Task類用法介紹_C#教程
- 2022-02-01 使用 file_get_contents 獲取網(wǎng)站信息報錯failed to open stream
- 2022-03-26 C語言實(shí)現(xiàn)簡單的猜數(shù)字游戲_C 語言
- 2022-10-20 Android開發(fā)App啟動流程與消息機(jī)制詳解_Android
- 2022-10-03 Objective-C優(yōu)雅使用KVO觀察屬性值變化_IOS
- 2022-10-01 Pycharm安裝scrapy及初始化爬蟲項(xiàng)目的完整步驟_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支