--- order: 23 title: en-US: Editable Rows zh-CN: 可编辑行 --- ## zh-CN 带行编辑功能的表格。 ## en-US Table with editable rows. ````jsx import { Table, Input, Popconfirm } 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 EditableCell = ({ editable, value, onChange }) => (
{editable ? onChange(e.target.value)} /> : value }
); class EditableTable extends React.Component { constructor(props) { super(props); this.columns = [{ title: 'name', dataIndex: 'name', width: '25%', render: (text, record) => this.renderColumns(text, record, 'name'), }, { title: 'age', dataIndex: 'age', width: '15%', render: (text, record) => this.renderColumns(text, record, 'age'), }, { title: 'address', dataIndex: 'address', width: '40%', render: (text, record) => this.renderColumns(text, record, 'address'), }, { title: 'operation', dataIndex: 'operation', render: (text, record) => { const { editable } = record; return (
{ editable ? this.save(record.key)}>Save this.cancel(record.key)}> Cancel : this.edit(record.key)}>Edit }
); }, }]; this.state = { data }; this.cacheData = data.map(item => ({ ...item })); } renderColumns(text, record, column) { return ( this.handleChange(value, record.key, column)} /> ); } handleChange(value, key, column) { const newData = [...this.state.data]; const target = newData.filter(item => key === item.key)[0]; if (target) { target[column] = value; this.setState({ data: newData }); } } edit(key) { const newData = [...this.state.data]; const target = newData.filter(item => key === item.key)[0]; if (target) { target.editable = true; this.setState({ data: newData }); } } save(key) { const newData = [...this.state.data]; const target = newData.filter(item => key === item.key)[0]; if (target) { delete target.editable; this.setState({ data: newData }); this.cacheData = newData.map(item => ({ ...item })); } } cancel(key) { const newData = [...this.state.data]; const target = newData.filter(item => key === item.key)[0]; if (target) { Object.assign(target, this.cacheData.filter(item => key === item.key)[0]); delete target.editable; this.setState({ data: newData }); } } render() { return ; } } ReactDOM.render(, mountNode); ```` ````css .editable-row-operations a { margin-right: 8px; } ````