Skip to content

Commit 892c504

Browse files
committed
fix #1181
1 parent 3b67e19 commit 892c504

File tree

4 files changed

+42
-20
lines changed

4 files changed

+42
-20
lines changed

src/BootstrapTable.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,7 @@ class BootstrapTable extends Component {
10251025
}
10261026

10271027
renderToolBar() {
1028-
const { exportCSV, selectRow, insertRow, deleteRow, search, children } = this.props;
1028+
const { exportCSV, selectRow, insertRow, deleteRow, search, children, keyField } = this.props;
10291029
const enableShowOnlySelected = selectRow && selectRow.showOnlySelected;
10301030
const print = typeof this.props.options.printToolBar === 'undefined' ?
10311031
true : this.props.options.printToolBar;
@@ -1041,10 +1041,13 @@ class BootstrapTable extends Component {
10411041
if (Array.isArray(children)) {
10421042
columns = children.map((column, r) => {
10431043
const { props } = column;
1044+
const isKey = props.isKey || keyField === props.dataField;
10441045
return {
1046+
isKey,
10451047
name: props.headerText || props.children,
10461048
field: props.dataField,
10471049
hiddenOnInsert: props.hiddenOnInsert,
1050+
keyValidator: props.keyValidator,
10481051
// when you want same auto generate value and not allow edit, example ID field
10491052
autoValue: props.autoValue || false,
10501053
// for create editor, no params for column.editable() indicate that editor for new row
@@ -1059,7 +1062,8 @@ class BootstrapTable extends Component {
10591062
name: children.props.headerText || children.props.children,
10601063
field: children.props.dataField,
10611064
editable: children.props.editable,
1062-
hiddenOnInsert: children.props.hiddenOnInsert
1065+
hiddenOnInsert: children.props.hiddenOnInsert,
1066+
keyValidator: children.props.keyValidator
10631067
} ];
10641068
}
10651069
return (
@@ -1101,7 +1105,8 @@ class BootstrapTable extends Component {
11011105
searchPanel={ this.props.options.searchPanel }
11021106
btnGroup={ this.props.options.btnGroup }
11031107
toolBar={ this.props.options.toolBar }
1104-
reset={ this.state.reset } />
1108+
reset={ this.state.reset }
1109+
isValidKey={ this.store.isValidKey } />
11051110
</div>
11061111
);
11071112
} else {

src/TableHeaderColumn.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,8 @@ TableHeaderColumn.propTypes = {
269269
expandable: PropTypes.bool,
270270
tdAttr: PropTypes.object,
271271
tdStyle: PropTypes.object,
272-
thStyle: PropTypes.object
272+
thStyle: PropTypes.object,
273+
keyValidator: PropTypes.bool
273274
};
274275

275276
TableHeaderColumn.defaultProps = {
@@ -303,7 +304,8 @@ TableHeaderColumn.defaultProps = {
303304
expandable: true,
304305
tdAttr: undefined,
305306
tdStyle: undefined,
306-
thStyle: undefined
307+
thStyle: undefined,
308+
keyValidator: false
307309
};
308310

309311
export default TableHeaderColumn;

src/store/TableDataStore.js

+12-9
Original file line numberDiff line numberDiff line change
@@ -216,23 +216,26 @@ export class TableDataStore {
216216
}
217217

218218
add(newObj) {
219-
if (!newObj[this.keyField] || newObj[this.keyField].toString() === '') {
220-
throw new Error(`${this.keyField} can't be empty value.`);
221-
}
222-
const currentDisplayData = this.getCurrentDisplayData();
223-
currentDisplayData.forEach(function(row) {
224-
if (row[this.keyField].toString() === newObj[this.keyField].toString()) {
225-
throw new Error(`${this.keyField} ${newObj[this.keyField]} already exists`);
226-
}
227-
}, this);
219+
const e = this.isValidKey(newObj[this.keyField]);
220+
if (e) throw new Error(e);
228221

222+
const currentDisplayData = this.getCurrentDisplayData();
229223
currentDisplayData.push(newObj);
230224
if (this.isOnFilter) {
231225
this.data.push(newObj);
232226
}
233227
this._refresh(false);
234228
}
235229

230+
isValidKey = key => {
231+
if (!key || key.toString() === '') {
232+
return `${this.keyField} can't be empty value.`;
233+
}
234+
const currentDisplayData = this.getCurrentDisplayData();
235+
const exist = currentDisplayData.find(row => row[this.keyField].toString() === key.toString());
236+
if (exist) return `${this.keyField} ${key} already exists`;
237+
}
238+
236239
remove(rowKey) {
237240
const currentDisplayData = this.getCurrentDisplayData();
238241
const result = currentDisplayData.filter(row => {

src/toolbar/ToolBar.js

+18-6
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,32 @@ class ToolBar extends Component {
6464
}
6565
}
6666

67+
displayCommonMessage = () => {
68+
this.refs.notifier.notice(
69+
'error',
70+
'Form validate errors, please checking!',
71+
'Pressed ESC can cancel');
72+
}
73+
6774
validateNewRow(newRow) {
6875
const validateState = {};
6976
let isValid = true;
7077
let tempMsg;
7178
let responseType;
7279

7380
this.props.columns.forEach(column => {
74-
if (column.editable && column.editable.validator) { // process validate
81+
if (column.isKey && column.keyValidator) { // key validator for checking exist key
82+
tempMsg = this.props.isValidKey(newRow[column.field]);
83+
if (tempMsg) {
84+
this.displayCommonMessage();
85+
isValid = false;
86+
validateState[column.field] = tempMsg;
87+
}
88+
} else if (column.editable && column.editable.validator) { // process validate
7589
tempMsg = column.editable.validator(newRow[column.field]);
7690
responseType = typeof tempMsg;
7791
if (responseType !== 'object' && tempMsg !== true) {
78-
this.refs.notifier.notice(
79-
'error',
80-
'Form validate errors, please checking!',
81-
'Pressed ESC can cancel');
92+
this.displayCommonMessage();
8293
isValid = false;
8394
validateState[column.field] = tempMsg;
8495
} else if (responseType === 'object' && tempMsg.isValid !== true) {
@@ -469,7 +480,8 @@ ToolBar.propTypes = {
469480
btnGroup: PropTypes.func,
470481
toolBar: PropTypes.func,
471482
searchPosition: PropTypes.string,
472-
reset: PropTypes.bool
483+
reset: PropTypes.bool,
484+
isValidKey: PropTypes.func
473485
};
474486

475487
ToolBar.defaultProps = {

0 commit comments

Comments
 (0)