2016-11-21 11:29:55 +08:00
|
|
|
---
|
|
|
|
order: 23
|
|
|
|
title:
|
|
|
|
en-US: Editable Rows
|
|
|
|
zh-CN: 可编辑行
|
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
|
|
|
带行编辑功能的表格。
|
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
|
|
|
Table with editable rows.
|
|
|
|
|
2018-05-07 20:50:43 +08:00
|
|
|
```jsx
|
2019-05-07 14:57:32 +08:00
|
|
|
import { Table, Input, InputNumber, Popconfirm, Form } from 'antd';
|
2016-11-21 11:29:55 +08:00
|
|
|
|
2017-11-07 21:02:28 +08:00
|
|
|
const data = [];
|
|
|
|
for (let i = 0; i < 100; i++) {
|
|
|
|
data.push({
|
|
|
|
key: i.toString(),
|
|
|
|
name: `Edrward ${i}`,
|
|
|
|
age: 32,
|
|
|
|
address: `London Park no. ${i}`,
|
|
|
|
});
|
2016-11-21 11:29:55 +08:00
|
|
|
}
|
2018-05-07 20:50:43 +08:00
|
|
|
const FormItem = Form.Item;
|
|
|
|
const EditableContext = React.createContext();
|
2016-11-21 11:29:55 +08:00
|
|
|
|
2018-05-07 20:50:43 +08:00
|
|
|
class EditableCell extends React.Component {
|
|
|
|
getInput = () => {
|
|
|
|
if (this.props.inputType === 'number') {
|
2018-05-23 11:32:23 +08:00
|
|
|
return <InputNumber />;
|
2018-05-07 20:50:43 +08:00
|
|
|
}
|
2018-05-23 11:32:23 +08:00
|
|
|
return <Input />;
|
2018-05-07 20:50:43 +08:00
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2018-05-07 20:50:43 +08:00
|
|
|
render() {
|
2019-05-07 14:57:32 +08:00
|
|
|
const { editing, dataIndex, title, inputType, record, index, ...restProps } = this.props;
|
2018-05-07 20:50:43 +08:00
|
|
|
return (
|
|
|
|
<EditableContext.Consumer>
|
2019-05-07 14:57:32 +08:00
|
|
|
{form => {
|
2018-05-07 20:50:43 +08:00
|
|
|
const { getFieldDecorator } = form;
|
|
|
|
return (
|
|
|
|
<td {...restProps}>
|
|
|
|
{editing ? (
|
2018-05-23 11:32:23 +08:00
|
|
|
<FormItem style={{ margin: 0 }}>
|
2018-05-07 20:50:43 +08:00
|
|
|
{getFieldDecorator(dataIndex, {
|
2019-05-07 14:57:32 +08:00
|
|
|
rules: [
|
|
|
|
{
|
|
|
|
required: true,
|
|
|
|
message: `Please Input ${title}!`,
|
|
|
|
},
|
|
|
|
],
|
2018-05-07 20:50:43 +08:00
|
|
|
initialValue: record[dataIndex],
|
|
|
|
})(this.getInput())}
|
|
|
|
</FormItem>
|
2019-05-07 14:57:32 +08:00
|
|
|
) : (
|
|
|
|
restProps.children
|
|
|
|
)}
|
2018-05-07 20:50:43 +08:00
|
|
|
</td>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</EditableContext.Consumer>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-21 11:29:55 +08:00
|
|
|
class EditableTable extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2018-05-07 20:50:43 +08:00
|
|
|
this.state = { data, editingKey: '' };
|
|
|
|
this.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) => {
|
2019-03-18 15:28:42 +08:00
|
|
|
const { editingKey } = this.state;
|
2018-05-07 20:50:43 +08:00
|
|
|
const editable = this.isEditing(record);
|
|
|
|
return (
|
2018-05-23 11:32:23 +08:00
|
|
|
<div>
|
2018-05-07 20:50:43 +08:00
|
|
|
{editable ? (
|
2017-02-23 11:45:48 +08:00
|
|
|
<span>
|
2018-05-07 20:50:43 +08:00
|
|
|
<EditableContext.Consumer>
|
|
|
|
{form => (
|
|
|
|
<a
|
|
|
|
href="javascript:;"
|
|
|
|
onClick={() => this.save(form, record.key)}
|
2018-05-23 11:32:23 +08:00
|
|
|
style={{ marginRight: 8 }}
|
2018-05-07 20:50:43 +08:00
|
|
|
>
|
|
|
|
Save
|
|
|
|
</a>
|
|
|
|
)}
|
|
|
|
</EditableContext.Consumer>
|
2019-05-07 14:57:32 +08:00
|
|
|
<Popconfirm title="Sure to cancel?" onConfirm={() => this.cancel(record.key)}>
|
2017-02-23 11:45:48 +08:00
|
|
|
<a>Cancel</a>
|
|
|
|
</Popconfirm>
|
|
|
|
</span>
|
2018-05-07 20:50:43 +08:00
|
|
|
) : (
|
2019-05-07 14:57:32 +08:00
|
|
|
<a disabled={editingKey !== ''} onClick={() => this.edit(record.key)}>
|
|
|
|
Edit
|
|
|
|
</a>
|
2018-05-07 20:50:43 +08:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
2016-11-21 11:29:55 +08:00
|
|
|
},
|
2018-05-07 20:50:43 +08:00
|
|
|
];
|
2016-11-21 11:29:55 +08:00
|
|
|
}
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2018-11-28 15:00:03 +08:00
|
|
|
isEditing = record => record.key === this.state.editingKey;
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2018-11-28 15:00:03 +08:00
|
|
|
cancel = () => {
|
|
|
|
this.setState({ editingKey: '' });
|
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2018-05-23 10:55:20 +08:00
|
|
|
save(form, key) {
|
|
|
|
form.validateFields((error, row) => {
|
2018-05-07 20:50:43 +08:00
|
|
|
if (error) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const newData = [...this.state.data];
|
|
|
|
const index = newData.findIndex(item => key === item.key);
|
|
|
|
if (index > -1) {
|
|
|
|
const item = newData[index];
|
|
|
|
newData.splice(index, 1, {
|
|
|
|
...item,
|
|
|
|
...row,
|
|
|
|
});
|
|
|
|
this.setState({ data: newData, editingKey: '' });
|
|
|
|
} else {
|
2018-07-19 11:26:23 +08:00
|
|
|
newData.push(row);
|
2018-05-07 20:50:43 +08:00
|
|
|
this.setState({ data: newData, editingKey: '' });
|
|
|
|
}
|
|
|
|
});
|
2016-11-21 11:29:55 +08:00
|
|
|
}
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2018-11-28 15:00:03 +08:00
|
|
|
edit(key) {
|
|
|
|
this.setState({ editingKey: key });
|
|
|
|
}
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2016-11-21 11:29:55 +08:00
|
|
|
render() {
|
2018-05-07 20:50:43 +08:00
|
|
|
const components = {
|
|
|
|
body: {
|
|
|
|
cell: EditableCell,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-05-07 14:57:32 +08:00
|
|
|
const columns = this.columns.map(col => {
|
2018-05-07 20:50:43 +08:00
|
|
|
if (!col.editable) {
|
|
|
|
return col;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
...col,
|
|
|
|
onCell: record => ({
|
|
|
|
record,
|
|
|
|
inputType: col.dataIndex === 'age' ? 'number' : 'text',
|
|
|
|
dataIndex: col.dataIndex,
|
|
|
|
title: col.title,
|
|
|
|
editing: this.isEditing(record),
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
2019-03-18 15:28:42 +08:00
|
|
|
<EditableContext.Provider value={this.props.form}>
|
|
|
|
<Table
|
|
|
|
components={components}
|
|
|
|
bordered
|
|
|
|
dataSource={this.state.data}
|
|
|
|
columns={columns}
|
|
|
|
rowClassName="editable-row"
|
|
|
|
pagination={{
|
|
|
|
onChange: this.cancel,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</EditableContext.Provider>
|
2018-05-07 20:50:43 +08:00
|
|
|
);
|
2016-11-21 11:29:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-18 15:28:42 +08:00
|
|
|
const EditableFormTable = Form.create()(EditableTable);
|
|
|
|
|
|
|
|
ReactDOM.render(<EditableFormTable />, 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
|
|
|
```
|