ant-design/components/table/demo/edit-row.md

211 lines
4.0 KiB
Markdown
Raw Normal View History

2016-11-21 11:29:55 +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.
```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
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++) {
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
}
interface EditableCellProps {
editing: boolean;
dataIndex: string;
title: React.ReactNode;
inputType: 'number' | 'text';
record: Item;
index: number;
children: React.ReactNode;
}
2016-11-21 11:29:55 +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('');
const isEditing = record => record.key === editingKey;
const edit = record => {
form.setFieldsValue({ ...record });
setEditingKey(record.key);
};
2018-06-27 15:55:04 +08:00
const cancel = () => {
setEditingKey('');
};
const save = async key => {
try {
const row = await form.validateFields();
2018-06-27 15:55:04 +08:00
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 {
2018-07-19 11:26:23 +08:00
newData.push(row);
setData(newData);
setEditingKey('');
}
} catch (errInfo) {
console.log('Validate Failed:', errInfo);
return;
}
};
2018-06-27 15:55:04 +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',
render: (text, record) => {
const editable = isEditing(record);
return editable ? (
<span>
<a href="javascript:;" onClick={() => save(record.key)} style={{ marginRight: 8 }}>
Save
</a>
<Popconfirm title="Sure to cancel?" onConfirm={() => cancel(record.key)}>
<a>Cancel</a>
</Popconfirm>
</span>
) : (
<a disabled={editingKey !== ''} onClick={() => edit(record)}>
Edit
</a>
);
},
},
];
const components = {
body: {
cell: EditableCell,
},
};
2016-11-21 11:29:55 +08:00
const mergedColumns = columns.map(col => {
if (!col.editable) {
return col;
}
return {
...col,
onCell: record => ({
record,
inputType: col.dataIndex === 'age' ? 'number' : 'text',
dataIndex: col.dataIndex,
title: col.title,
editing: isEditing(record),
}),
};
});
return (
<Form form={form} component={false}>
<Table
components={components}
bordered
dataSource={data}
columns={mergedColumns}
rowClassName="editable-row"
pagination={{
onChange: cancel,
}}
/>
</Form>
);
};
ReactDOM.render(<EditableTable />, mountNode);
```
2016-11-21 11:29:55 +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
}
```