Skip to content

Commit 39c5a34

Browse files
committed
Fix page count when filteredCount is falsy
1 parent 5b63a35 commit 39c5a34

4 files changed

Lines changed: 33 additions & 6 deletions

File tree

src/directives/pagination.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ export interface PaginationDirectiveConfiguration<T> {
3939

4040
export const paginationDirective = <T>({table}: PaginationDirectiveConfiguration<T>): PaginationDirective => {
4141
let {slice: {page: currentPage, size: currentSize}} = table.getTableState();
42-
let itemListLength = table.filteredCount;
43-
let pageCount = currentSize ? Math.ceil(itemListLength / currentSize) : 1;
42+
let itemListLength = table.filteredCount || 0;
43+
let pageCount = currentSize && itemListLength ? Math.ceil(itemListLength / currentSize) : 1;
4444

4545
const proxy = <PaginationProxy>sliceListener({emitter: table});
4646

@@ -72,8 +72,8 @@ export const paginationDirective = <T>({table}: PaginationDirectiveConfiguration
7272
directive.onSummaryChange(({page: p, size: s, filteredCount}: Summary) => {
7373
currentPage = p;
7474
currentSize = s;
75-
itemListLength = filteredCount;
76-
pageCount = currentSize ? Math.ceil(itemListLength / currentSize) : 1;
75+
itemListLength = filteredCount || 0;
76+
pageCount = currentSize && itemListLength ? Math.ceil(itemListLength / currentSize) : 1;
7777
});
7878

7979
return directive;

src/directives/table.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export const tableDirective = <T>({sortFactory, tableState, data, filterFactory,
8484

8585
// We need to register in case the summary comes from outside (like server data)
8686
table.on(SmartTableEvents.SUMMARY_CHANGED, ({filteredCount: count}) => {
87-
filteredCount = count;
87+
filteredCount = count || 0;
8888
});
8989

9090
const safeAssign = newState => Object.assign({}, newState);

test/directives/pagination.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,11 @@ export default ({test}) => {
8383
table.dispatch(evts.SUMMARY_CHANGED, {page: 1, size: 0});
8484
t.deepEqual(dir.state().pageCount, 1);
8585
});
86-
}
86+
test('pagination directive accepts falsy value for filteredCount', t => {
87+
const table = fakeTable({page: 1, size: 1});
88+
const dir = pagination({table});
89+
t.deepEqual(dir.state().pageCount, 1);
90+
table.dispatch(evts.SUMMARY_CHANGED, {page: 1, size: 1, filteredCount: 0});
91+
t.deepEqual(dir.state().pageCount, 1);
92+
});
93+
}

test/directives/table.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,26 @@ export default ({test}) => {
316316
t.equal(table.filteredCount, 2, 'filtered count should have been updated');
317317
});
318318

319+
test('filteredCount allows falsy value', t => {
320+
const tableState = {
321+
sort: {pointer: 'id', direction: SortDirection.DESC},
322+
search: {},
323+
filter: {name: [{value: 'b'}]},
324+
slice: {page: 1, size: 1}
325+
};
326+
const table = tableFactory({
327+
data: [
328+
{id: 1, name: 'foo'},
329+
{id: 2, name: 'blah'},
330+
{id: 3, name: 'bip'}
331+
],
332+
tableState
333+
});
334+
t.equal(table.filteredCount, 3, 'initially with the length of data array');
335+
table.dispatch(evts.SUMMARY_CHANGED, {});
336+
t.equal(table.filteredCount, 0, 'filteredCount should be a number');
337+
});
338+
319339
test('getTableState should return a deep copy of the tableState', t => {
320340
const tableState = {
321341
sort: {pointer: 'foo'},

0 commit comments

Comments
 (0)