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

241 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.
2017-02-13 10:55:53 +08:00
````jsx
2018-11-28 15:00:03 +08:00
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,
}
toggleEdit = () => {
const editing = !this.state.editing;
this.setState({ editing }, () => {
if (editing) {
this.input.focus();
}
});
2016-11-21 11:29:55 +08:00
}
2018-06-27 15:55:04 +08:00
2019-03-11 21:39:58 +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 });
});
2016-11-21 11:29:55 +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;
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>
{(form) => {
this.form = form;
return (
editing ? (
<FormItem style={{ margin: 0 }}>
{form.getFieldDecorator(dataIndex, {
rules: [{
required: true,
message: `${title} is required.`,
}],
initialValue: record[dataIndex],
})(
<Input
ref={node => (this.input = node)}
onPressEnter={this.save}
2019-02-21 20:23:00 +08:00
onBlur={this.save}
2018-07-10 21:10:48 +08:00
/>
)}
</FormItem>
) : (
<div
className="editable-cell-value-wrap"
style={{ paddingRight: 24 }}
onClick={this.toggleEdit}
>
{restProps.children}
</div>
)
);
}}
</EditableContext.Consumer>
) : restProps.children}
</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);
this.columns = [{
title: 'name',
dataIndex: 'name',
width: '30%',
2018-07-10 21:10:48 +08:00
editable: true,
2016-11-21 11:29:55 +08:00
}, {
title: 'age',
dataIndex: 'age',
}, {
title: 'address',
dataIndex: 'address',
}, {
title: 'operation',
dataIndex: 'operation',
2018-11-28 15:00:03 +08:00
render: (text, record) => (
this.state.dataSource.length >= 1
? (
<Popconfirm title="Sure to delete?" onConfirm={() => this.handleDelete(record.key)}>
<a href="javascript:;">Delete</a>
</Popconfirm>
) : null
),
2016-11-21 11:29:55 +08:00
}];
this.state = {
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',
}],
count: 2,
};
}
2018-06-27 15:55:04 +08:00
2018-07-10 21:10:48 +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) });
2016-11-21 11:29:55 +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,
});
}
2018-06-27 15:55:04 +08:00
2018-07-10 21:10:48 +08:00
handleSave = (row) => {
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 });
}
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,
},
};
const columns = this.columns.map((col) => {
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);
````
````css
.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
}
````