2016-11-21 11:29:55 +08:00
|
|
|
---
|
2019-11-15 14:35:25 +08:00
|
|
|
order: 24
|
2016-11-21 11:29:55 +08:00
|
|
|
title:
|
|
|
|
en-US: Editable Rows
|
|
|
|
zh-CN: 可编辑行
|
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
|
|
|
带行编辑功能的表格。
|
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
|
|
|
Table with editable rows.
|
|
|
|
|
2019-07-03 20:14:39 +08:00
|
|
|
```tsx
|
2020-01-22 12:11:49 +08:00
|
|
|
import React, { useState } from 'react';
|
2019-05-07 14:57:32 +08:00
|
|
|
import { Table, Input, InputNumber, Popconfirm, Form } from 'antd';
|
2016-11-21 11:29:55 +08:00
|
|
|
|
2019-07-03 20:14:39 +08:00
|
|
|
interface Item {
|
|
|
|
key: string;
|
|
|
|
name: string;
|
|
|
|
age: number;
|
|
|
|
address: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const originData: Item[] = [];
|
2017-11-07 21:02:28 +08:00
|
|
|
for (let i = 0; i < 100; i++) {
|
2019-07-03 20:14:39 +08:00
|
|
|
originData.push({
|
2017-11-07 21:02:28 +08:00
|
|
|
key: i.toString(),
|
|
|
|
name: `Edrward ${i}`,
|
|
|
|
age: 32,
|
|
|
|
address: `London Park no. ${i}`,
|
|
|
|
});
|
2016-11-21 11:29:55 +08:00
|
|
|
}
|
2020-03-06 12:50:45 +08:00
|
|
|
interface EditableCellProps extends React.HTMLAttributes<HTMLElement> {
|
2019-07-03 20:14:39 +08:00
|
|
|
editing: boolean;
|
|
|
|
dataIndex: string;
|
2020-03-06 12:50:45 +08:00
|
|
|
title: any;
|
2019-07-03 20:14:39 +08:00
|
|
|
inputType: 'number' | 'text';
|
|
|
|
record: Item;
|
|
|
|
index: number;
|
|
|
|
children: React.ReactNode;
|
|
|
|
}
|
2016-11-21 11:29:55 +08:00
|
|
|
|
2019-07-03 20:14:39 +08:00
|
|
|
const EditableCell: React.FC<EditableCellProps> = ({
|
|
|
|
editing,
|
|
|
|
dataIndex,
|
|
|
|
title,
|
|
|
|
inputType,
|
|
|
|
record,
|
|
|
|
index,
|
|
|
|
children,
|
|
|
|
...restProps
|
|
|
|
}) => {
|
|
|
|
const inputNode = inputType === 'number' ? <InputNumber /> : <Input />;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<td {...restProps}>
|
|
|
|
{editing ? (
|
|
|
|
<Form.Item
|
|
|
|
name={dataIndex}
|
|
|
|
style={{ margin: 0 }}
|
|
|
|
rules={[
|
|
|
|
{
|
|
|
|
required: true,
|
|
|
|
message: `Please Input ${title}!`,
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
>
|
|
|
|
{inputNode}
|
|
|
|
</Form.Item>
|
|
|
|
) : (
|
|
|
|
children
|
|
|
|
)}
|
|
|
|
</td>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const EditableTable = () => {
|
|
|
|
const [form] = Form.useForm();
|
2020-01-22 12:11:49 +08:00
|
|
|
const [data, setData] = useState(originData);
|
|
|
|
const [editingKey, setEditingKey] = useState('');
|
2019-07-03 20:14:39 +08:00
|
|
|
|
2020-03-06 12:50:45 +08:00
|
|
|
const isEditing = (record: Item) => record.key === editingKey;
|
2019-07-03 20:14:39 +08:00
|
|
|
|
2020-03-06 12:50:45 +08:00
|
|
|
const edit = (record: Item) => {
|
2019-07-03 20:14:39 +08:00
|
|
|
form.setFieldsValue({ ...record });
|
|
|
|
setEditingKey(record.key);
|
2018-05-07 20:50:43 +08:00
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2019-07-03 20:14:39 +08:00
|
|
|
const cancel = () => {
|
|
|
|
setEditingKey('');
|
2019-05-14 12:16:13 +08:00
|
|
|
};
|
|
|
|
|
2020-03-06 12:50:45 +08:00
|
|
|
const save = async (key: React.Key) => {
|
2019-07-03 20:14:39 +08:00
|
|
|
try {
|
2020-03-06 12:50:45 +08:00
|
|
|
const row = (await form.validateFields()) as Item;
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2019-07-03 20:14:39 +08:00
|
|
|
const newData = [...data];
|
2018-05-07 20:50:43 +08:00
|
|
|
const index = newData.findIndex(item => key === item.key);
|
|
|
|
if (index > -1) {
|
|
|
|
const item = newData[index];
|
|
|
|
newData.splice(index, 1, {
|
|
|
|
...item,
|
|
|
|
...row,
|
|
|
|
});
|
2019-07-03 20:14:39 +08:00
|
|
|
setData(newData);
|
|
|
|
setEditingKey('');
|
2018-05-07 20:50:43 +08:00
|
|
|
} else {
|
2018-07-19 11:26:23 +08:00
|
|
|
newData.push(row);
|
2019-07-03 20:14:39 +08:00
|
|
|
setData(newData);
|
|
|
|
setEditingKey('');
|
2018-05-07 20:50:43 +08:00
|
|
|
}
|
2019-07-03 20:14:39 +08:00
|
|
|
} catch (errInfo) {
|
|
|
|
console.log('Validate Failed:', errInfo);
|
|
|
|
}
|
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2019-07-03 20:14:39 +08:00
|
|
|
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',
|
2020-03-06 12:50:45 +08:00
|
|
|
render: (_: any, record: Item) => {
|
2019-07-03 20:14:39 +08:00
|
|
|
const editable = isEditing(record);
|
|
|
|
return editable ? (
|
|
|
|
<span>
|
|
|
|
<a href="javascript:;" onClick={() => save(record.key)} style={{ marginRight: 8 }}>
|
|
|
|
Save
|
|
|
|
</a>
|
2020-03-06 12:50:45 +08:00
|
|
|
<Popconfirm title="Sure to cancel?" onConfirm={cancel}>
|
2019-07-03 20:14:39 +08:00
|
|
|
<a>Cancel</a>
|
|
|
|
</Popconfirm>
|
|
|
|
</span>
|
|
|
|
) : (
|
|
|
|
<a disabled={editingKey !== ''} onClick={() => edit(record)}>
|
|
|
|
Edit
|
|
|
|
</a>
|
|
|
|
);
|
2018-05-07 20:50:43 +08:00
|
|
|
},
|
2019-07-03 20:14:39 +08:00
|
|
|
},
|
|
|
|
];
|
2018-05-07 20:50:43 +08:00
|
|
|
|
2019-07-03 20:14:39 +08:00
|
|
|
const mergedColumns = columns.map(col => {
|
|
|
|
if (!col.editable) {
|
|
|
|
return col;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
...col,
|
2020-03-06 12:50:45 +08:00
|
|
|
onCell: (record: Item) => ({
|
2019-07-03 20:14:39 +08:00
|
|
|
record,
|
|
|
|
inputType: col.dataIndex === 'age' ? 'number' : 'text',
|
|
|
|
dataIndex: col.dataIndex,
|
|
|
|
title: col.title,
|
|
|
|
editing: isEditing(record),
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
});
|
2019-03-18 15:28:42 +08:00
|
|
|
|
2019-07-03 20:14:39 +08:00
|
|
|
return (
|
|
|
|
<Form form={form} component={false}>
|
|
|
|
<Table
|
2020-03-06 12:50:45 +08:00
|
|
|
components={{
|
|
|
|
body: {
|
|
|
|
cell: EditableCell,
|
|
|
|
},
|
|
|
|
}}
|
2019-07-03 20:14:39 +08:00
|
|
|
bordered
|
|
|
|
dataSource={data}
|
|
|
|
columns={mergedColumns}
|
|
|
|
rowClassName="editable-row"
|
|
|
|
pagination={{
|
|
|
|
onChange: cancel,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
ReactDOM.render(<EditableTable />, mountNode);
|
2018-05-07 20:50:43 +08:00
|
|
|
```
|
2016-11-21 11:29:55 +08:00
|
|
|
|
2018-05-07 20:50:43 +08:00
|
|
|
```css
|
2018-05-23 11:32:23 +08:00
|
|
|
.editable-row .ant-form-explain {
|
|
|
|
position: absolute;
|
|
|
|
font-size: 12px;
|
|
|
|
margin-top: -4px;
|
2016-11-21 11:29:55 +08:00
|
|
|
}
|
2018-05-07 20:50:43 +08:00
|
|
|
```
|