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

239 lines
5.3 KiB
Markdown
Raw Normal View History

2016-11-21 11:29:55 +08:00
---
order: 22
title:
en-US: Editable Cells
zh-CN: 可编辑单元格
---
## zh-CN
带单元格编辑功能的表格。
## en-US
Table with editable cells.
2019-05-07 14:57:32 +08:00
```jsx
import { Table, Input, Button, Popconfirm, Form } from 'antd';
2018-07-10 21:10:48 +08:00
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);
2016-11-21 11:29:55 +08:00
class EditableCell extends React.Component {
state = {
2018-07-10 21:10:48 +08:00
editing: false,
2019-05-07 14:57:32 +08:00
};
2018-07-10 21:10:48 +08:00
toggleEdit = () => {
const editing = !this.state.editing;
this.setState({ editing }, () => {
if (editing) {
this.input.focus();
}
});
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +08:00
2019-05-07 14:57:32 +08:00
save = e => {
2018-07-10 21:10:48 +08:00
const { record, handleSave } = this.props;
this.form.validateFields((error, values) => {
2019-03-11 21:39:58 +08:00
if (error && error[e.currentTarget.id]) {
2018-07-10 21:10:48 +08:00
return;
}
this.toggleEdit();
handleSave({ ...record, ...values });
});
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +08:00
2016-11-21 11:29:55 +08:00
render() {
2018-07-10 21:10:48 +08:00
const { editing } = this.state;
2019-05-07 14:57:32 +08:00
const { editable, dataIndex, title, record, index, handleSave, ...restProps } = this.props;
2017-02-23 11:45:48 +08:00
return (
2019-03-11 21:39:58 +08:00
<td {...restProps}>
2018-07-10 21:10:48 +08:00
{editable ? (
<EditableContext.Consumer>
2019-05-07 14:57:32 +08:00
{form => {
2018-07-10 21:10:48 +08:00
this.form = form;
2019-05-07 14:57:32 +08:00
return editing ? (
<FormItem style={{ margin: 0 }}>
{form.getFieldDecorator(dataIndex, {
rules: [
{
2018-07-10 21:10:48 +08:00
required: true,
message: `${title} is required.`,
2019-05-07 14:57:32 +08:00
},
],
initialValue: record[dataIndex],
})(
<Input
ref={node => (this.input = node)}
onPressEnter={this.save}
onBlur={this.save}
/>,
)}
</FormItem>
) : (
<div
className="editable-cell-value-wrap"
style={{ paddingRight: 24 }}
onClick={this.toggleEdit}
>
{restProps.children}
</div>
2018-07-10 21:10:48 +08:00
);
}}
</EditableContext.Consumer>
2019-05-07 14:57:32 +08:00
) : (
restProps.children
)}
2018-07-10 21:10:48 +08:00
</td>
2017-02-23 11:45:48 +08:00
);
2016-11-21 11:29:55 +08:00
}
}
class EditableTable extends React.Component {
constructor(props) {
super(props);
2019-05-07 14:57:32 +08:00
this.columns = [
{
title: 'name',
dataIndex: 'name',
width: '30%',
editable: true,
},
{
title: 'age',
dataIndex: 'age',
},
{
title: 'address',
dataIndex: 'address',
},
{
title: 'operation',
dataIndex: 'operation',
render: (text, record) =>
this.state.dataSource.length >= 1 ? (
2018-11-28 15:00:03 +08:00
<Popconfirm title="Sure to delete?" onConfirm={() => this.handleDelete(record.key)}>
<a href="javascript:;">Delete</a>
</Popconfirm>
2019-05-07 14:57:32 +08:00
) : null,
},
];
2016-11-21 11:29:55 +08:00
this.state = {
2019-05-07 14:57:32 +08:00
dataSource: [
{
key: '0',
name: 'Edward King 0',
age: '32',
address: 'London, Park Lane no. 0',
},
{
key: '1',
name: 'Edward King 1',
age: '32',
address: 'London, Park Lane no. 1',
},
],
2016-11-21 11:29:55 +08:00
count: 2,
};
}
2018-06-27 15:55:04 +08:00
2019-05-07 14:57:32 +08:00
handleDelete = key => {
2017-02-08 14:42:17 +08:00
const dataSource = [...this.state.dataSource];
2017-08-25 16:37:00 +08:00
this.setState({ dataSource: dataSource.filter(item => item.key !== key) });
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +08:00
2016-11-21 11:29:55 +08:00
handleAdd = () => {
const { count, dataSource } = this.state;
const newData = {
key: count,
name: `Edward King ${count}`,
age: 32,
address: `London, Park Lane no. ${count}`,
};
this.setState({
dataSource: [...dataSource, newData],
count: count + 1,
});
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +08:00
2019-05-07 14:57:32 +08:00
handleSave = row => {
2018-07-10 21:10:48 +08:00
const newData = [...this.state.dataSource];
const index = newData.findIndex(item => row.key === item.key);
const item = newData[index];
newData.splice(index, 1, {
...item,
...row,
});
this.setState({ dataSource: newData });
2019-05-07 14:57:32 +08:00
};
2018-07-10 21:10:48 +08:00
2016-11-21 11:29:55 +08:00
render() {
const { dataSource } = this.state;
2018-07-10 21:10:48 +08:00
const components = {
body: {
row: EditableFormRow,
cell: EditableCell,
},
};
2019-05-07 14:57:32 +08:00
const columns = this.columns.map(col => {
2018-07-10 21:10:48 +08:00
if (!col.editable) {
return col;
}
return {
...col,
onCell: record => ({
record,
editable: col.editable,
dataIndex: col.dataIndex,
title: col.title,
handleSave: this.handleSave,
}),
};
});
2017-02-23 11:45:48 +08:00
return (
<div>
<Button onClick={this.handleAdd} type="primary" style={{ marginBottom: 16 }}>
Add a row
</Button>
2018-07-10 21:10:48 +08:00
<Table
components={components}
rowClassName={() => 'editable-row'}
bordered
dataSource={dataSource}
columns={columns}
/>
2017-02-23 11:45:48 +08:00
</div>
);
2016-11-21 11:29:55 +08:00
}
}
ReactDOM.render(<EditableTable />, mountNode);
2019-05-07 14:57:32 +08:00
```
2016-11-21 11:29:55 +08:00
2019-05-07 14:57:32 +08:00
```css
2016-11-21 11:29:55 +08:00
.editable-cell {
position: relative;
}
2018-07-10 21:10:48 +08:00
.editable-cell-value-wrap {
padding: 5px 12px;
2016-11-21 11:29:55 +08:00
cursor: pointer;
}
2018-07-10 21:10:48 +08:00
.editable-row:hover .editable-cell-value-wrap {
border: 1px solid #d9d9d9;
border-radius: 4px;
padding: 4px 11px;
2016-11-21 11:29:55 +08:00
}
2019-05-07 14:57:32 +08:00
```