Skip to content

Commit

Permalink
fix #1418
Browse files Browse the repository at this point in the history
  • Loading branch information
AllenFang committed Jun 24, 2017
1 parent 778c2a0 commit b6da50e
Showing 1 changed file with 79 additions and 71 deletions.
150 changes: 79 additions & 71 deletions src/BootstrapTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,87 +212,93 @@ class BootstrapTable extends Component {
componentWillReceiveProps(nextProps) {
this.initTable(nextProps);
const { options, selectRow } = nextProps;
let { replace } = nextProps;
replace = replace || this.props.replace;

this.store.setData(nextProps.data.slice());

// from #481
let page = this.state.currPage;
if (this.props.options.page !== options.page) {
page = options.page;
}
// from #481
let sizePerPage = this.state.sizePerPage;
if (this.props.options.sizePerPage !== options.sizePerPage) {
sizePerPage = options.sizePerPage;
}

if (this.isRemoteDataSource()) {
let data = nextProps.data.slice();
if (nextProps.pagination && !this.allowRemote(Const.REMOTE_PAGE)) {
data = this.store.page(page, sizePerPage).get();
if (!replace) {
// from #481
let page = this.state.currPage;
if (this.props.options.page !== options.page) {
page = options.page;
}
this.setState(() => {
return {
data,
currPage: page,
sizePerPage,
reset: false
};
});
} else {
// #125
// remove !options.page for #709
if (page > Math.ceil(nextProps.data.length / sizePerPage)) {
page = 1;
}
const sortList = this.store.getSortInfo();
const sortField = options.sortName;
const sortOrder = options.sortOrder;
if (sortField && sortOrder) {
this.store.setSortInfo(sortOrder, sortField);
this.store.sort();
} else if (sortList.length > 0) {
this.store.sort();
// from #481
let sizePerPage = this.state.sizePerPage;
if (this.props.options.sizePerPage !== options.sizePerPage) {
sizePerPage = options.sizePerPage;
}
const data = this.store.page(page, sizePerPage).get();
this.setState(() => {
return {
data,
currPage: page,
sizePerPage,
reset: false
};
});

if (this.store.isSearching && options.afterSearch) {
options.afterSearch(this.store.searchText, this.store.getDataIgnoringPagination());
}
if (this.isRemoteDataSource()) {
let data = nextProps.data.slice();
if (nextProps.pagination && !this.allowRemote(Const.REMOTE_PAGE)) {
data = this.store.page(page, sizePerPage).get();
}
this.setState(() => {
return {
data,
currPage: page,
sizePerPage,
reset: false
};
});
} else {
// #125
// remove !options.page for #709
if (page > Math.ceil(nextProps.data.length / sizePerPage)) {
page = 1;
}
const sortList = this.store.getSortInfo();
const sortField = options.sortName;
const sortOrder = options.sortOrder;
if (sortField && sortOrder) {
this.store.setSortInfo(sortOrder, sortField);
this.store.sort();
} else if (sortList.length > 0) {
this.store.sort();
}
const data = this.store.page(page, sizePerPage).get();
this.setState(() => {
return {
data,
currPage: page,
sizePerPage,
reset: false
};
});

if (this.store.isSearching && options.afterSearch) {
options.afterSearch(this.store.searchText, this.store.getDataIgnoringPagination());
}

if (this.store.isFiltering && options.afterColumnFilter) {
options.afterColumnFilter(this.store.filterObj, this.store.getDataIgnoringPagination());
if (this.store.isFiltering && options.afterColumnFilter) {
options.afterColumnFilter(this.store.filterObj, this.store.getDataIgnoringPagination());
}
}
}

// If setting the expanded rows is being handled externally
// then overwrite the current expanded rows.
if (this.props.options.expanding !== options.expanding) {
this.setState(() => {
return {
expanding: options.expanding || []
};
});
}
// If setting the expanded rows is being handled externally
// then overwrite the current expanded rows.
if (this.props.options.expanding !== options.expanding) {
this.setState(() => {
return {
expanding: options.expanding || []
};
});
}

if (selectRow && selectRow.selected) {
// set default select rows to store.
const copy = selectRow.selected.slice();
this.store.setSelectedRowKey(copy);
this.setState(() => {
return {
selectedRowKeys: copy,
reset: false
};
});
if (selectRow && selectRow.selected) {
// set default select rows to store.
const copy = selectRow.selected.slice();
this.store.setSelectedRowKey(copy);
this.setState(() => {
return {
selectedRowKeys: copy,
reset: false
};
});
}
} else {
this.reset();
}
}

Expand Down Expand Up @@ -1421,6 +1427,7 @@ BootstrapTable.propTypes = {
maxHeight: PropTypes.oneOfType([ PropTypes.string, PropTypes.number ]),
data: PropTypes.oneOfType([ PropTypes.array, PropTypes.object ]),
remote: PropTypes.oneOfType([ PropTypes.bool, PropTypes.func ]), // remote data, default is false
replace: PropTypes.oneOfType([ PropTypes.bool, PropTypes.func ]),
scrollTop: PropTypes.oneOfType([ PropTypes.string, PropTypes.number ]),
striped: PropTypes.bool,
bordered: PropTypes.bool,
Expand Down Expand Up @@ -1574,6 +1581,7 @@ BootstrapTable.propTypes = {
})
};
BootstrapTable.defaultProps = {
replace: false,
scrollTop: undefined,
expandComponent: undefined,
expandableRow: undefined,
Expand Down

0 comments on commit b6da50e

Please sign in to comment.