網站首頁 編程語言 正文
如何實現在當前表格直接更改數據
需求
用戶點擊修改按鈕時直接在彈出框的當前頁面內直接再次修改點擊行相關信息:
效果如下
點擊修改當事人信息時,直接將當前改為輸入框,并將信息展示,同時操作欄內的內容變為保存和取消;
具體做法
我這里是使用的antd組件內的可編輯表格;當然原生的也可以做,以前也做過;
這里的關鍵是點擊修改按鈕時,令當前行的表格變為輸入框,并展示數據;
給數據每一項加上 editable: true屬性,并通過該屬性控制 渲染的是數據還是可修改的輸入框
這里是使用的useState()方法來進行狀態控制的;
- 關于 useState 的用法是,需要傳入一個參數作為狀態的初始值,當函數執行后會返回兩個值,一個是當前狀態的屬性,一個是修改狀態的方法。
- 使用方法更新數據后會觸發render()重新渲染數據
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一個初始值為空,點擊修改后使用setEditingKey()方法(useState返回的方法)將useState數據的值賦值為當前行的唯一key值,這樣二者相等,就可以區別點擊的是哪一條數據的按鈕了;點擊取消setEditingKey(’’)重新置空;
判斷邏輯:
// 是否正在修改
const isEditing = (record: Item) => record.key === editingKey;
渲染數據前進行判斷:
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),
}),
};
});
根據數據狀態判斷渲染的是表格合適輸入框:
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: `請填寫${title}!`,
},
]}
>
<Input />
</Form.Item>
) : (
children
)}
</td>
);
};
導出:
return (
<Form form={form} component={false}>
<Table
components={{
body: {
cell: EditableCell,
},
}}
bordered
pagination={false}
dataSource={dataSource}
{...otherProps}
columns={mergedColumns}
rowClassName="editable-row"
/>
</Form>
);
其中dataSource為數據源,
功能實現。
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
相關推薦
- 2021-12-14 如何利用C語言輸出3D立體感心形圖詳解_C 語言
- 2022-12-10 C++模擬實現string的方法詳解_C 語言
- 2022-02-24 解決:this is incompatible with sql_mode=only_full_gr
- 2022-08-17 R語言繪制corrplot相關熱圖分析美化示例及詳細圖解_R語言
- 2022-07-23 SQL?Server刪除表中的重復數據_MsSql
- 2022-12-23 go語言優雅地處理error工具及技巧詳解_Golang
- 2022-06-12 Pandas對CSV文件讀寫操作詳解_python
- 2022-01-30 uniapp H5刷新404問題解決 apache配置
- 最近更新
-
- 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同步修改后的遠程分支