|
|
|
---
|
|
|
|
order: 23
|
|
|
|
title:
|
|
|
|
en-US: Editable Rows
|
|
|
|
zh-CN: 可编辑行
|
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
|
|
|
带行编辑功能的表格。
|
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
|
|
|
Table with editable rows.
|
|
|
|
|
|
|
|
```jsx
|
|
|
|
import { Table, Input, InputNumber, Popconfirm, Form } from 'antd';
|
|
|
|
|
|
|
|
const data = [];
|
|
|
|
for (let i = 0; i < 100; i++) {
|
|
|
|
data.push({
|
|
|
|
key: i.toString(),
|
|
|
|
name: `Edrward ${i}`,
|
|
|
|
age: 32,
|
|
|
|
address: `London Park no. ${i}`,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
const FormItem = Form.Item;
|
|
|
|
const EditableContext = React.createContext();
|
|
|
|
|
|
|
|
const EditableRow = ({ form, index, ...props }) => (
|
|
|
|
<EditableContext.Provider value={form}>
|
|
|
|
<tr {...props} />
|
|
|
|
</EditableContext.Provider>
|
|
|
|
);
|
|
|
|
|
|
|
|
const EditableFormRow = Form.create()(EditableRow);
|
|
|
|
|
|
|
|
class EditableCell extends React.Component {
|
|
|
|
getInput = () => {
|
|
|
|
if (this.props.inputType === 'number') {
|
|
|
|
return <InputNumber />;
|
|
|
|
}
|
|
|
|
return <Input />;
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const {
|
|
|
|
editing,
|
|
|
|
dataIndex,
|
|
|
|
title,
|
|
|
|
inputType,
|
|
|
|
record,
|
|
|
|
index,
|
|
|
|
...restProps
|
|
|
|
} = this.props;
|
|
|
|
return (
|
|
|
|
<EditableContext.Consumer>
|
|
|
|
{(form) => {
|
|
|
|
const { getFieldDecorator } = form;
|
|
|
|
return (
|
|
|
|
<td {...restProps}>
|
|
|
|
{editing ? (
|
|
|
|
<FormItem style={{ margin: 0 }}>
|
|
|
|
{getFieldDecorator(dataIndex, {
|
|
|
|
rules: [{
|
|
|
|
required: true,
|
|
|
|
message: `Please Input ${title}!`,
|
|
|
|
}],
|
|
|
|
initialValue: record[dataIndex],
|
|
|
|
})(this.getInput())}
|
|
|
|
</FormItem>
|
|
|
|
) : restProps.children}
|
|
|
|
</td>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</EditableContext.Consumer>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class EditableTable extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
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) => {
|
|
|
|
const editable = this.isEditing(record);
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
{editable ? (
|
|
|
|
<span>
|
|
|
|
<EditableContext.Consumer>
|
|
|
|
{form => (
|
|
|
|
<a
|
|
|
|
href="javascript:;"
|
|
|
|
onClick={() => this.save(form, record.key)}
|
|
|
|
style={{ marginRight: 8 }}
|
|
|
|
>
|
|
|
|
Save
|
|
|
|
</a>
|
|
|
|
)}
|
|
|
|
</EditableContext.Consumer>
|
|
|
|
<Popconfirm
|
|
|
|
title="Sure to cancel?"
|
|
|
|
onConfirm={() => this.cancel(record.key)}
|
|
|
|
>
|
|
|
|
<a>Cancel</a>
|
|
|
|
</Popconfirm>
|
|
|
|
</span>
|
|
|
|
) : (
|
|
|
|
<a onClick={() => this.edit(record.key)}>Edit</a>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
|