react中useState使用:如何實(shí)現(xiàn)在當(dāng)前表格直接更改數(shù)據(jù)
如何實(shí)現(xiàn)在當(dāng)前表格直接更改數(shù)據(jù)
需求
用戶點(diǎn)擊修改按鈕時(shí)直接在彈出框的當(dāng)前頁(yè)面內(nèi)直接再次修改點(diǎn)擊行相關(guān)信息:

效果如下

點(diǎn)擊修改當(dāng)事人信息時(shí),直接將當(dāng)前改為輸入框,并將信息展示,同時(shí)操作欄內(nèi)的內(nèi)容變?yōu)楸4婧腿∠?
具體做法
我這里是使用的antd組件內(nèi)的可編輯表格;當(dāng)然原生的也可以做,以前也做過(guò);
這里的關(guān)鍵是點(diǎn)擊修改按鈕時(shí),令當(dāng)前行的表格變?yōu)檩斎肟颍⒄故緮?shù)據(jù);
給數(shù)據(jù)每一項(xiàng)加上 editable: true屬性,并通過(guò)該屬性控制 渲染的是數(shù)據(jù)還是可修改的輸入框
這里是使用的useState()方法來(lái)進(jìn)行狀態(tài)控制的;
- 關(guān)于 useState 的用法是,需要傳入一個(gè)參數(shù)作為狀態(tài)的初始值,當(dāng)函數(shù)執(zhí)行后會(huì)返回兩個(gè)值,一個(gè)是當(dāng)前狀態(tài)的屬性,一個(gè)是修改狀態(tài)的方法。
- 使用方法更新數(shù)據(jù)后會(huì)觸發(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一個(gè)初始值為空,點(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: `請(qǐng)?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修改對(duì)象的字段
首先定義一個(gè)空對(duì)象
const [dataSelect, setDataSelect] = React.useState({})
給這個(gè)對(duì)象附上不同值,但不會(huì)把原來(lái)的覆蓋的掉
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
}
}
原理用一個(gè)第三方的值,作為中間變量。每次都是附上最新的data。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
ReactJS?應(yīng)用兼容ios9對(duì)標(biāo)ie11解決方案
這篇文章主要為大家介紹了ReactJS?應(yīng)用兼容ios9對(duì)標(biāo)ie11解決方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
React 項(xiàng)目遷移 Webpack Babel7的實(shí)現(xiàn)
這篇文章主要介紹了React 項(xiàng)目遷移 Webpack Babel7的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
在React中如何優(yōu)雅的處理事件響應(yīng)詳解
這篇文章主要給大家介紹了關(guān)于在React中如何優(yōu)雅處理事件響應(yīng)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-07-07

