Skip to content
This repository was archived by the owner on Jun 4, 2024. It is now read-only.

Commit c6aa878

Browse files
author
Shammamah Hossain
committed
Use max_page_count for backend pagination.
1 parent 9608dfa commit c6aa878

File tree

5 files changed

+55
-13
lines changed

5 files changed

+55
-13
lines changed

src/dash-table/components/ControlledTable/index.tsx

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,8 @@ export default class ControlledTable extends PureComponent<ControlledTableProps>
829829
merge_duplicate_headers
830830
};
831831

832+
const lastPage = this.lastPage();
833+
832834
return (<div
833835
id={id}
834836
onCopy={this.onCopy}
@@ -869,13 +871,40 @@ export default class ControlledTable extends PureComponent<ControlledTableProps>
869871
</div>))}
870872
</div>
871873
</div>
872-
{!this.displayPagination || this.lastPage() === 0 ? null : (
874+
{!this.displayPagination || lastPage === 0 ? null : (
873875
<div className='previous-next-container'>
874-
<button className='first-page' onClick={this.loadFirst} disabled={this.props.page_current === 0}><FontAwesomeIcon icon='angle-double-left' /></button>
875-
<button className='previous-page' onClick={this.loadPrevious} disabled={this.props.page_current === 0}><FontAwesomeIcon icon='angle-left' /></button>
876-
<div className='current-page'>{this.props.page_current + 1} / {this.lastPage() + 1}</div>
877-
<button className='next-page' onClick={this.loadNext} disabled={this.props.page_current === this.lastPage()}><FontAwesomeIcon icon='angle-right' /></button>
878-
<button className='last-page' onClick={this.loadLast} disabled={this.props.page_current === this.lastPage()}><FontAwesomeIcon icon='angle-double-right' /></button>
876+
<button
877+
className='first-page'
878+
onClick={this.loadFirst}
879+
disabled={this.props.page_current === 0}>
880+
<FontAwesomeIcon icon='angle-double-left' />
881+
</button>
882+
883+
<button
884+
className='previous-page'
885+
onClick={this.loadPrevious}
886+
disabled={this.props.page_current === 0}>
887+
<FontAwesomeIcon icon='angle-left' />
888+
</button>
889+
890+
<div className='current-page'>
891+
{this.props.page_current + 1} {lastPage !== undefined ? ' / ' : ''}
892+
{lastPage !== undefined ? lastPage + 1 : ''}
893+
</div>
894+
895+
<button
896+
className='next-page'
897+
onClick={this.loadNext}
898+
disabled={this.props.page_current === lastPage}>
899+
<FontAwesomeIcon icon='angle-right' />
900+
</button>
901+
902+
<button
903+
className='last-page'
904+
onClick={this.loadLast}
905+
disabled={this.props.page_current === lastPage || lastPage === undefined}>
906+
<FontAwesomeIcon icon='angle-double-right' />
907+
</button>
879908
</div>
880909
)}
881910
</div>);

src/dash-table/components/Table/controlledPropsHelper.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export default () => {
3737
page_action,
3838
page_current,
3939
page_size,
40+
max_page_count,
4041
selected_columns,
4142
selected_rows,
4243
sort_action,
@@ -92,6 +93,7 @@ export default () => {
9293
page_action,
9394
page_current,
9495
page_size,
96+
max_page_count,
9597
setProps,
9698
virtual.data
9799
);

src/dash-table/components/Table/props.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ export interface IProps {
318318
style_as_list_view?: boolean;
319319
page_action?: TableAction;
320320
page_current?: number;
321+
max_page_count?: number | undefined;
321322
page_size: number;
322323

323324
style_data?: Style;

src/dash-table/dash/DataTable.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,14 @@ export const propTypes = {
731731
*/
732732
page_current: PropTypes.number,
733733

734+
/**
735+
* `max_page_count` represents the number of the last page of the
736+
* paginated table. This is really only useful when performing
737+
* backend pagination, since the front end is unable to use the
738+
* full size of the table to calculate the max page number.
739+
*/
740+
max_page_count: PropTypes.number,
741+
734742
/**
735743
* `page_size` represents the number of rows that will be
736744
* displayed on a particular page when `page_action` is `'custom'` or `'native'`

src/dash-table/derived/paginator.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export interface IPaginator {
1313
loadPrevious(): void;
1414
loadFirst(): void;
1515
loadLast(): void;
16-
lastPage: number;
16+
lastPage: number | undefined;
1717
}
1818

1919
export function lastPage(data: Data, page_size: number) {
@@ -23,8 +23,7 @@ export function lastPage(data: Data, page_size: number) {
2323
function getBackEndPagination(
2424
page_current: number,
2525
setProps: SetProps,
26-
data: Data,
27-
page_size: number
26+
max_page_count: number | undefined
2827
): IPaginator {
2928
return {
3029
loadNext: () => {
@@ -44,10 +43,12 @@ function getBackEndPagination(
4443
setProps({ page_current, ...clearSelection });
4544
},
4645
loadLast: () => {
47-
page_current = lastPage(data, page_size);
48-
setProps({ page_current, ...clearSelection });
46+
if (max_page_count !== undefined) {
47+
page_current = max_page_count;
48+
setProps({ page_current, ...clearSelection });
49+
}
4950
},
50-
lastPage: lastPage(data, page_size)
51+
lastPage: max_page_count
5152
};
5253
}
5354

@@ -102,6 +103,7 @@ const getter = (
102103
page_action: TableAction,
103104
page_current: number,
104105
page_size: number,
106+
max_page_count: number | undefined,
105107
setProps: SetProps,
106108
data: Data
107109
): IPaginator => {
@@ -111,7 +113,7 @@ const getter = (
111113
case TableAction.Native:
112114
return getFrontEndPagination(page_current, page_size, setProps, data);
113115
case TableAction.Custom:
114-
return getBackEndPagination(page_current, setProps, data, page_size);
116+
return getBackEndPagination(page_current, setProps, max_page_count);
115117
default:
116118
throw new Error(`Unknown pagination mode: '${page_action}'`);
117119
}

0 commit comments

Comments
 (0)