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

200 lines
4.0 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
2016-11-21 11:29:55 +08:00
import { Table, Input, Icon, Button, Popconfirm } from 'antd';
class EditableCell extends React.Component {
state = {
value: this.props.value,
editable: false,
}
2018-06-27 15:55:04 +08:00
2016-11-21 11:29:55 +08:00
handleChange = (e) => {
const value = e.target.value;
this.setState({ value });
}
2018-06-27 15:55:04 +08:00
2016-11-21 11:29:55 +08:00
check = () => {
this.setState({ editable: false });
if (this.props.onChange) {
this.props.onChange(this.state.value);
}
}
2018-06-27 15:55:04 +08:00
2016-11-21 11:29:55 +08:00
edit = () => {
this.setState({ editable: true });
}
2018-06-27 15:55:04 +08:00
2016-11-21 11:29:55 +08:00
render() {
const { value, editable } = this.state;
2017-02-23 11:45:48 +08:00
return (
<div className="editable-cell">
{
editable ? (
<Input
value={value}
onChange={this.handleChange}
onPressEnter={this.check}
2018-06-27 15:55:04 +08:00
suffix={(
<Icon
type="check"
className="editable-cell-icon-check"
onClick={this.check}
/>
2018-06-27 15:55:04 +08:00
)}
/>
) : (
<div style={{ paddingRight: 24 }}>
2017-02-23 11:45:48 +08:00
{value || ' '}
<Icon
type="edit"
className="editable-cell-icon"
onClick={this.edit}
/>
</div>
)
2017-02-23 11:45:48 +08:00
}
</div>
);
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%',
2017-08-25 16:37:00 +08:00
render: (text, record) => (
2016-11-21 11:29:55 +08:00
<EditableCell
value={text}
2017-08-25 16:37:00 +08:00
onChange={this.onCellChange(record.key, 'name')}
2016-11-21 11:29:55 +08:00
/>
),
}, {
title: 'age',
dataIndex: 'age',
}, {
title: 'address',
dataIndex: 'address',
}, {
title: 'operation',
dataIndex: 'operation',
2017-08-25 16:37:00 +08:00
render: (text, record) => {
2016-11-21 11:29:55 +08:00
return (
2018-06-27 15:55:04 +08:00
this.state.dataSource.length > 1
? (
<Popconfirm title="Sure to delete?" onConfirm={() => this.onDelete(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
2017-08-25 16:37:00 +08:00
onCellChange = (key, dataIndex) => {
2016-11-21 11:29:55 +08:00
return (value) => {
const dataSource = [...this.state.dataSource];
2017-08-25 16:37:00 +08:00
const target = dataSource.find(item => item.key === key);
if (target) {
target[dataIndex] = value;
this.setState({ dataSource });
}
2016-11-21 11:29:55 +08:00
};
}
2018-06-27 15:55:04 +08:00
2017-08-25 16:37:00 +08:00
onDelete = (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
2016-11-21 11:29:55 +08:00
render() {
const { dataSource } = this.state;
const columns = this.columns;
2017-02-23 11:45:48 +08:00
return (
<div>
<Button onClick={this.handleAdd} type="primary" style={{ marginBottom: 16 }}>
Add a row
</Button>
2017-02-23 11:45:48 +08:00
<Table bordered dataSource={dataSource} columns={columns} />
</div>
);
2016-11-21 11:29:55 +08:00
}
}
ReactDOM.render(<EditableTable />, mountNode);
````
````css
.editable-cell {
position: relative;
}
.editable-cell-icon,
.editable-cell-icon-check {
cursor: pointer;
}
.editable-cell-icon {
line-height: 14px;
position: absolute;
right: 0;
top: 50%;
margin-top: -7px;
2016-11-21 11:29:55 +08:00
display: none;
}
td:hover .editable-cell-icon {
2016-11-21 11:29:55 +08:00
display: inline-block;
}
.editable-cell-icon:hover,
.editable-cell-icon-check:hover {
2017-02-23 11:45:48 +08:00
color: #108ee9;
2016-11-21 11:29:55 +08:00
}
````