Skip to content

Commit

Permalink
fix #307
Browse files Browse the repository at this point in the history
  • Loading branch information
AllenFang committed Mar 4, 2016
1 parent c71f508 commit 881cb13
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 21 deletions.
56 changes: 35 additions & 21 deletions src/BootstrapTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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 = {
Expand Down
17 changes: 17 additions & 0 deletions src/store/TableDataStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.";
Expand Down

0 comments on commit 881cb13

Please sign in to comment.