diff --git a/src/BootstrapTable.js b/src/BootstrapTable.js index c4c290628..ce80adb8a 100644 --- a/src/BootstrapTable.js +++ b/src/BootstrapTable.js @@ -435,33 +435,24 @@ class BootstrapTable extends React.Component { } } - handleAddRow(newObj) { - let msg = null, result; + handleAddRowAtBegin(newObj) { + let result; try { - this.store.add(newObj); + this.store.addAtBegin(newObj); } catch (e) { return e; } + this._handleAfterAddingRow(newObj); + } - if (this.props.pagination) { - //if pagination is enabled and insert row be trigger, change to last page - const { sizePerPage } = this.state; - const currLastPage = Math.ceil(this.store.getDataNum() / sizePerPage); - result = this.store.page(currLastPage, sizePerPage).get(); - this.setState({ - data: result, - currPage: currLastPage, - }); - } else { - result = this.store.get(); - this.setState({ - data: result - }); - } - - if (this.props.options.afterInsertRow) { - this.props.options.afterInsertRow(newObj); + handleAddRow(newObj) { + let result; + try { + this.store.add(newObj); + } catch (e) { + return e; } + this._handleAfterAddingRow(newObj); } getSizePerPage() { @@ -678,6 +669,29 @@ class BootstrapTable extends React.Component { this.refs.header.fitHeader(headerProps, this.refs.body.refs.container.scrollHeight > this.refs.body.refs.container.clientHeight); } + + _handleAfterAddingRow(newObj) { + let result; + if (this.props.pagination) { + //if pagination is enabled and insert row be trigger, change to last page + const { sizePerPage } = this.state; + const currLastPage = Math.ceil(this.store.getDataNum() / sizePerPage); + result = this.store.page(currLastPage, sizePerPage).get(); + this.setState({ + data: result, + currPage: currLastPage, + }); + } else { + result = this.store.get(); + this.setState({ + data: result + }); + } + + if (this.props.options.afterInsertRow) { + this.props.options.afterInsertRow(newObj); + } + } } BootstrapTable.propTypes = { diff --git a/src/store/TableDataStore.js b/src/store/TableDataStore.js index b300d35cf..ee9f9ff9d 100644 --- a/src/store/TableDataStore.js +++ b/src/store/TableDataStore.js @@ -146,6 +146,23 @@ export class TableDataStore { return this; } + addAtBegin(newObj) { + if (!newObj[this.keyField] || newObj[this.keyField].toString() === '') { + throw this.keyField + " can't be empty value."; + } + let currentDisplayData = this.getCurrentDisplayData(); + currentDisplayData.forEach(function (row) { + if (row[this.keyField].toString() === newObj[this.keyField].toString()) { + throw this.keyField + " " + newObj[this.keyField] + " already exists"; + } + }, this); + console.log('@@'); + currentDisplayData.unshift(newObj); + if (this.isOnFilter) { + this.data.unshift(newObj); + } + } + add(newObj) { if (!newObj[this.keyField] || newObj[this.keyField].toString() === '') { throw this.keyField + " can't be empty value.";