In my application i using Ant design Table my code is like below :
<Table size="small" dataSource={this.props.actionArray}>
<Column title="Name" dataIndex="id" key="name"/>
<Column title="Action" key="action"
render={(text,record)=>(
<span>
<a href="" >Edit</a>
<span className="ant-divider"/>
<a href="" >Delete</a>
</span>
)}
/>
</Table>
i want when user click on Edit the entire row of table render as a <Input type="text"/> instead of normal text , so users can edit the row data, also a custom save button when user click on it call a function(ex save() )
but i don't know how to this .
Try something like this. Save the editting record id in the state and according to that show or hide the input:
columns = [
{
title: 'Name',
render: (text, record) =>
record.id === this.state.editingId ? (
<Input type="text"/>
) : (
text
),
},
{
title: "Action",
render: (text, record) => (
<span>
<a href="" >Edit</a>
<span className="ant-divider"/>
<a href="" >Delete</a>
</span>
)}
}
]
Antd has editable rows feature for antd <Table>
Editable rows documentation
App.js
import React, { useState } from "react";
import "antd/dist/antd.css";
import "./index.css";
import { Form, Input, Popconfirm, Table, Typography } from "antd";
const originData = [
{
key: 1,
name: `Edrward`,
address: `London Park no. 1`
},
{
key: 2,
name: `James`,
address: `London Park no. 2`
}
];
const EditableCell = ({
editing,
dataIndex,
title,
inputType,
record,
index,
children,
...restProps
}) => {
return (
<td {...restProps}>
{editing ? (
<Form.Item
name={dataIndex}
style={{
margin: 0
}}
rules={[
{
required: true,
message: `Please Input ${title}!`
}
]}
>
<Input />
</Form.Item>
) : (
children
)}
</td>
);
};
const App = () => {
const [form] = Form.useForm();
const [data, setData] = useState(originData);
const [editingKey, setEditingKey] = useState("");
const isEditing = (record) => record.key === editingKey;
const edit = (record) => {
form.setFieldsValue({
name: "",
address: "",
...record
});
setEditingKey(record.key);
};
const cancel = () => {
setEditingKey("");
};
const save = async (key) => {
try {
const row = await form.validateFields();
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: "Address",
dataIndex: "address",
width: "40%",
editable: true
},
{
title: "Action",
dataIndex: "operation",
render: (_, record) => {
const editable = isEditing(record);
return editable ? (
<span>
<Typography.Link
onClick={() => save(record.key)}
style={{
marginRight: 8
}}
>
Save
</Typography.Link>
<Popconfirm title="Sure to cancel?" onConfirm={cancel}>
<a>Cancel</a>
</Popconfirm>
</span>
) : (
<Typography.Link
disabled={editingKey !== ""}
onClick={() => edit(record)}
>
Edit
</Typography.Link>
);
}
}
];
const mergedColumns = columns.map((col) => {
if (!col.editable) {
return col;
}
return {
...col,
onCell: (record) => ({
record,
inputType: "text",
dataIndex: col.dataIndex,
title: col.title,
editing: isEditing(record)
})
};
});
return (
<Form form={form} component={false}>
<Table
components={{
body: {
cell: EditableCell
}
}}
dataSource={data}
columns={mergedColumns}
rowClassName="editable-row"
pagination={{
onChange: cancel
}}
/>
</Form>
);
};
export default App;
Output:


If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With