Skip to content

Commit f9abcf4

Browse files
committed
implement remote pagination
1 parent 47f86df commit f9abcf4

File tree

5 files changed

+74
-20
lines changed

5 files changed

+74
-20
lines changed

packages/react-bootstrap-table2-paginator/src/wrapper.js

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,46 @@ const wrapperFactory = baseElement =>
1717
this.handleChangePage = this.handleChangePage.bind(this);
1818
this.handleChangeSizePerPage = this.handleChangeSizePerPage.bind(this);
1919

20+
let currPage;
21+
let currSizePerPage;
2022
const options = props.pagination.options || {};
21-
const currPage = options.pageStartIndex || Const.PAGE_START_INDEX;
2223
const sizePerPageList = options.sizePerPageList || Const.SIZE_PER_PAGE_LIST;
23-
const currSizePerPage = typeof sizePerPageList[0] === 'object' ? sizePerPageList[0].value : sizePerPageList[0];
24+
25+
// initialize current page
26+
if (typeof options.page !== 'undefined') {
27+
currPage = options.page;
28+
} else if (typeof options.pageStartIndex !== 'undefined') {
29+
currPage = options.pageStartIndex;
30+
} else {
31+
currPage = Const.PAGE_START_INDEX;
32+
}
33+
34+
// initialize current sizePerPage
35+
if (typeof options.sizePerPage !== 'undefined') {
36+
currSizePerPage = options.sizePerPage;
37+
} else if (typeof sizePerPageList[0] === 'object') {
38+
currSizePerPage = sizePerPageList[0].value;
39+
} else {
40+
currSizePerPage = sizePerPageList[0];
41+
}
42+
2443
this.state = { currPage, currSizePerPage };
2544
}
2645

46+
isRemote() {
47+
const { remote } = this.props;
48+
return remote === true || (typeof remote === 'object' && remote.pagination);
49+
}
50+
2751
handleChangePage(currPage) {
28-
const { pagination: { options } } = this.props;
52+
const { currSizePerPage } = this.state;
53+
const { pagination: { options }, onRemotePageChange } = this.props;
2954
if (options.onPageChange) {
30-
options.onPageChange(currPage, this.state.currSizePerPage);
55+
options.onPageChange(currPage, currSizePerPage);
56+
}
57+
if (this.isRemote()) {
58+
onRemotePageChange(currPage, currSizePerPage);
59+
return;
3160
}
3261
this.setState(() => {
3362
return {
@@ -37,10 +66,14 @@ const wrapperFactory = baseElement =>
3766
}
3867

3968
handleChangeSizePerPage(currSizePerPage, currPage) {
40-
const { pagination: { options } } = this.props;
69+
const { pagination: { options }, onRemotePageChange } = this.props;
4170
if (options.onSizePerPageChange) {
4271
options.onSizePerPageChange(currSizePerPage, currPage);
4372
}
73+
if (this.isRemote()) {
74+
onRemotePageChange(currPage, currSizePerPage);
75+
return;
76+
}
4477
this.setState(() => {
4578
return {
4679
currPage,
@@ -60,25 +93,31 @@ const wrapperFactory = baseElement =>
6093
Const.HIDE_SIZE_PER_PAGE : options.hideSizePerPage;
6194
const hidePageListOnlyOnePage = typeof options.hidePageListOnlyOnePage === 'undefined' ?
6295
Const.HIDE_PAGE_LIST_ONLY_ONE_PAGE : options.hidePageListOnlyOnePage;
96+
const pageStartIndex = typeof options.pageStartIndex === 'undefined' ?
97+
Const.PAGE_START_INDEX : options.pageStartIndex;
98+
99+
const data = this.isRemote() ?
100+
this.props.data :
101+
store.getByCurrPage(currPage, currSizePerPage, pageStartIndex);
63102

64103
const base = baseElement({
65104
...this.props,
66105
key: 'table',
67-
data: store.getByCurrPage(currPage, currSizePerPage)
106+
data
68107
});
69108

70109
return [
71110
base,
72111
<Pagination
73112
key="pagination"
74-
dataSize={ this.props.store.getDataSize() }
113+
dataSize={ options.totalSize || store.getDataSize() }
75114
currPage={ currPage }
76115
currSizePerPage={ currSizePerPage }
77116
onPageChange={ this.handleChangePage }
78117
onSizePerPageChange={ this.handleChangeSizePerPage }
79118
sizePerPageList={ options.sizePerPageList || Const.SIZE_PER_PAGE_LIST }
80119
paginationSize={ options.paginationSize || Const.PAGINATION_SIZE }
81-
pageStartIndex={ options.pageStartIndex || Const.PAGE_START_INDEX }
120+
pageStartIndex={ pageStartIndex }
82121
withFirstAndLast={ withFirstAndLast }
83122
alwaysShowAllBtns={ alwaysShowAllBtns }
84123
hideSizePerPage={ hideSizePerPage }

packages/react-bootstrap-table2/src/bootstrap-table.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ BootstrapTable.propTypes = {
100100
keyField: PropTypes.string.isRequired,
101101
data: PropTypes.array.isRequired,
102102
columns: PropTypes.array.isRequired,
103+
remote: PropTypes.oneOfType([PropTypes.bool, PropTypes.shape({
104+
pagination: PropTypes.bool
105+
})]),
103106
store: PropTypes.object,
104107
noDataIndication: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
105108
striped: PropTypes.bool,
@@ -153,10 +156,12 @@ BootstrapTable.propTypes = {
153156
defaultSorted: PropTypes.arrayOf(PropTypes.shape({
154157
dataField: PropTypes.string.isRequired,
155158
order: PropTypes.oneOf([Const.SORT_DESC, Const.SORT_ASC]).isRequired
156-
}))
159+
})),
160+
onTableChange: PropTypes.func
157161
};
158162

159163
BootstrapTable.defaultProps = {
164+
remote: false,
160165
striped: false,
161166
bordered: true,
162167
hover: false,

packages/react-bootstrap-table2/src/container.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,18 @@ const withDataStore = Base =>
1818
super(props);
1919
this.store = new Store(props);
2020
this.handleUpdateCell = this.handleUpdateCell.bind(this);
21+
this.onRemotePageChange = this.onRemotePageChange.bind(this);
2122
}
2223

2324
componentWillReceiveProps(nextProps) {
2425
this.store.set(nextProps.data);
2526
}
2627

28+
onRemotePageChange(page, sizePerPage) {
29+
const newState = { page, sizePerPage };
30+
this.props.onTableChange(newState);
31+
}
32+
2733
handleUpdateCell(rowId, dataField, newValue) {
2834
const { cellEdit } = this.props;
2935
// handle cell editing internal
@@ -66,7 +72,10 @@ const withDataStore = Base =>
6672
} else if (this.props.columns.filter(col => col.sort).length > 0) {
6773
return wrapWithSort(baseProps);
6874
} else if (this.props.pagination) {
69-
return wrapWithPagination(baseProps);
75+
return wrapWithPagination({
76+
...baseProps,
77+
onRemotePageChange: this.onRemotePageChange
78+
});
7079
}
7180

7281
return React.createElement(Base, baseProps);

packages/react-bootstrap-table2/src/store/base.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,19 @@ export default class Store {
4040
return this.data;
4141
}
4242

43-
getByCurrPage(page, sizePerPage) {
44-
const end = (page * sizePerPage) - 1;
43+
getByCurrPage(page, sizePerPage, pageStartIndex) {
44+
const getNormalizedPage = () => {
45+
const offset = Math.abs(1 - pageStartIndex);
46+
return page + offset;
47+
};
48+
const end = (getNormalizedPage() * sizePerPage) - 1;
4549
const start = end - (sizePerPage - 1);
50+
const dataSize = this.getDataSize();
4651

4752
const result = [];
4853
for (let i = start; i <= end; i += 1) {
4954
result.push(this.data[i]);
50-
if (i + 1 === this.getDataSize()) break;
55+
if (i + 1 === dataSize) break;
5156
}
5257
return result;
5358
}

packages/react-bootstrap-table2/src/table-factory.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,9 @@ export const pureTable = props =>
2020
React.createElement(BootstrapTable, { ...props });
2121

2222
export const wrapWithPagination = (props) => {
23-
if (props.pagination) {
24-
const { wrapper } = props.pagination;
25-
const PaginationBase = wrapper(pureTable);
26-
return React.createElement(PaginationBase, { ...props });
27-
}
28-
29-
return pureTable(props);
23+
const { wrapper } = props.pagination;
24+
const PaginationBase = wrapper(pureTable);
25+
return React.createElement(PaginationBase, { ...props });
3026
};
3127
export const paginationElement = props => pureTable(props);
3228

0 commit comments

Comments
 (0)