Skip to content

Commit 8fd5354

Browse files
committed
small change
1 parent 9fe86f6 commit 8fd5354

File tree

6 files changed

+377
-45
lines changed

6 files changed

+377
-45
lines changed

_antd/table/editable-row.less

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,9 @@
44
font-size: 12px;
55
margin-top: -4px;
66
}
7+
8+
.ant-table-thead > tr > th,
9+
.ant-table-tbody > tr > td {
10+
padding: 4px 8px;
11+
}
712
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
:global {
2+
.editable-cell {
3+
position: relative;
4+
}
5+
6+
.editable-cell-value-wrap {
7+
padding: 5px 12px;
8+
cursor: pointer;
9+
}
10+
11+
.editable-row:hover .editable-cell-value-wrap {
12+
border: 1px solid #d9d9d9;
13+
border-radius: 4px;
14+
padding: 4px 11px;
15+
}
16+
17+
18+
.ant-table-tbody > tr > td {
19+
padding: 4px 16px;
20+
}
21+
22+
.ant-form-item-control {
23+
line-height: 40px;
24+
}
25+
}

_antd/table/editable-row-mouse.js renamed to _antd/table/editable/editable-row-cell-only-sth-mistake.js

Lines changed: 72 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Table, Input, InputNumber, Popconfirm, Form } from 'antd';
2-
2+
import { isEqual } from 'lodash'
33
import "./editable-row.less"
44

55
const data = [];
@@ -11,14 +11,15 @@ for (let i = 0; i < 100; i++) {
1111
address: `London Park no. ${i}`,
1212
});
1313
}
14+
1415
const EditableContext = React.createContext();
1516

1617
class EditableCell extends React.Component {
1718
getInput = () => {
1819
if (this.props.inputType === 'number') {
19-
return <InputNumber />;
20+
return <InputNumber style={{ width: '100%'}} />;
2021
}
21-
return <Input />;
22+
return <Input style={{ width: '100%'}} />;
2223
};
2324

2425
renderCell = ({ getFieldDecorator }) => {
@@ -37,12 +38,6 @@ class EditableCell extends React.Component {
3738
{editing ? (
3839
<Form.Item style={{ margin: 0 }}>
3940
{getFieldDecorator(dataIndex, {
40-
rules: [
41-
{
42-
required: true,
43-
message: `Please Input ${title}!`,
44-
},
45-
],
4641
initialValue: record[dataIndex],
4742
})(this.getInput())}
4843
</Form.Item>
@@ -61,7 +56,11 @@ class EditableCell extends React.Component {
6156
class EditableTable extends React.Component {
6257
constructor(props) {
6358
super(props);
64-
this.state = { data, editingKey: '' };
59+
this.state = {
60+
data,
61+
editingKey: ''
62+
};
63+
6564
this.columns = [
6665
{
6766
title: 'name',
@@ -81,35 +80,6 @@ class EditableTable extends React.Component {
8180
width: '40%',
8281
editable: true,
8382
},
84-
{
85-
title: 'operation',
86-
dataIndex: 'operation',
87-
render: (text, record) => {
88-
const { editingKey } = this.state;
89-
const editable = this.isEditing(record);
90-
return editable ? (
91-
<span>
92-
<EditableContext.Consumer>
93-
{form => (
94-
<a
95-
onClick={() => this.save(form, record.key)}
96-
style={{ marginRight: 8 }}
97-
>
98-
Save
99-
</a>
100-
)}
101-
</EditableContext.Consumer>
102-
<Popconfirm title="Sure to cancel?" onConfirm={() => this.cancel(record.key)}>
103-
<a>Cancel</a>
104-
</Popconfirm>
105-
</span>
106-
) : (
107-
<a disabled={editingKey !== ''} onClick={() => this.edit(record.key)}>
108-
Edit
109-
</a>
110-
);
111-
},
112-
},
11383
];
11484
}
11585

@@ -119,27 +89,67 @@ class EditableTable extends React.Component {
11989
this.setState({ editingKey: '' });
12090
};
12191

122-
save(form, key) {
92+
93+
handleSave = (key, record) => {
94+
const { form } = this.props
95+
96+
// debugger
12397
form.validateFields((error, row) => {
12498
if (error) {
99+
this.setState({
100+
editingKey: ''
101+
})
125102
return;
126103
}
104+
105+
if (isEqual(Object.assign({}, record, row), record)) {
106+
console.log('数据没啥变化')
107+
this.setState({
108+
editingKey: ''
109+
})
110+
return;
111+
}
112+
113+
console.log(Object.assign({}, record, row))
114+
console.log(record)
127115
const newData = [...this.state.data];
128116
const index = newData.findIndex(item => key === item.key);
117+
console.log(`修改第${index}行`)
118+
129119
if (index > -1) {
130120
const item = newData[index];
131121
newData.splice(index, 1, {
132122
...item,
133123
...row,
134124
});
135-
this.setState({ data: newData, editingKey: '' });
136-
} else {
137-
newData.push(row);
138-
this.setState({ data: newData, editingKey: '' });
125+
126+
this.setState({
127+
data: newData,
128+
editingKey: ''
129+
});
130+
131+
console.log({
132+
...item,
133+
...row,
134+
})
139135
}
136+
137+
// else {
138+
// newData.push(row);
139+
// this.setState({ data: newData, editingKey: '' });
140+
// }
141+
142+
this.setState({
143+
editingKey: ''
144+
})
145+
return;
146+
140147
});
148+
141149
}
142150

151+
152+
143153
edit(key) {
144154
this.setState({ editingKey: key });
145155
}
@@ -155,6 +165,7 @@ class EditableTable extends React.Component {
155165
if (!col.editable) {
156166
return col;
157167
}
168+
158169
return {
159170
...col,
160171
onCell: record => ({
@@ -175,6 +186,23 @@ class EditableTable extends React.Component {
175186
dataSource={this.state.data}
176187
columns={columns}
177188
rowClassName="editable-row"
189+
onRow={record => {
190+
191+
return {
192+
// 鼠标移入行
193+
onMouseEnter: event => {
194+
// debugger
195+
this.setState({
196+
editingKey: record.key
197+
})
198+
},
199+
onMouseLeave: event => {
200+
((key, record) => {
201+
this.handleSave(key)
202+
})(record.key, {...record})
203+
},
204+
};
205+
}}
178206
pagination={{
179207
onChange: this.cancel,
180208
}}

0 commit comments

Comments
 (0)