Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/directives/pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export interface PaginationDirectiveConfiguration<T> {

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

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

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

return directive;
Expand Down
2 changes: 1 addition & 1 deletion src/directives/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export const tableDirective = <T>({sortFactory, tableState, data, filterFactory,

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

const safeAssign = newState => Object.assign({}, newState);
Expand Down
9 changes: 8 additions & 1 deletion test/directives/pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,11 @@ export default ({test}) => {
table.dispatch(evts.SUMMARY_CHANGED, {page: 1, size: 0});
t.deepEqual(dir.state().pageCount, 1);
});
}
test('pagination directive accepts falsy value for filteredCount', t => {
const table = fakeTable({page: 1, size: 1});
const dir = pagination({table});
t.deepEqual(dir.state().pageCount, 1);
table.dispatch(evts.SUMMARY_CHANGED, {page: 1, size: 1, filteredCount: 0});
t.deepEqual(dir.state().pageCount, 1);
});
}
20 changes: 20 additions & 0 deletions test/directives/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,26 @@ export default ({test}) => {
t.equal(table.filteredCount, 2, 'filtered count should have been updated');
});

test('filteredCount allows falsy value', t => {
const tableState = {
sort: {pointer: 'id', direction: SortDirection.DESC},
search: {},
filter: {name: [{value: 'b'}]},
slice: {page: 1, size: 1}
};
const table = tableFactory({
data: [
{id: 1, name: 'foo'},
{id: 2, name: 'blah'},
{id: 3, name: 'bip'}
],
tableState
});
t.equal(table.filteredCount, 3, 'initially with the length of data array');
table.dispatch(evts.SUMMARY_CHANGED, {});
t.equal(table.filteredCount, 0, 'filteredCount should be a number');
});

test('getTableState should return a deep copy of the tableState', t => {
const tableState = {
sort: {pointer: 'foo'},
Expand Down