Skip to content

Commit 40f0cdd

Browse files
committed
#114 Add option to show page index information by number of items displayed
1 parent d4c0824 commit 40f0cdd

File tree

7 files changed

+54
-18
lines changed

7 files changed

+54
-18
lines changed

src/lib/components/Table/TableContent.svelte

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
toggle = false, // Whether to display the fitToScreen toggle
5858
search = true, // Whether to display the search input
5959
pageSizes = [5, 10, 20, 50, 100], // Page sizes to display in the pagination component
60+
pageIndexStringType = 'pages', // pages by default
6061
fitToScreen = true, // Whether to fit the table to the screen,
6162
exportable = false, // Whether to display the export button and enable export functionality
6263
server
@@ -780,10 +781,11 @@
780781
{serverItemCount}
781782
{updateTable}
782783
{pageSizes}
784+
{pageIndexStringType}
783785
id={tableId}
784786
/>
785787
{:else}
786-
<TablePagination pageConfig={pluginStates.page} {pageSizes} id={tableId} />
788+
<TablePagination pageConfig={pluginStates.page} {pageSizes} id={tableId} {pageIndexStringType} />
787789
{/if}
788790
{/if}
789791
</div>

src/lib/components/Table/TablePagination.svelte

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
1212
export let pageConfig;
1313
export let pageSizes;
14+
export let pageIndexStringType;
1415
export let id;
16+
17+
let indexInformation = '';
1518
1619
const { pageIndex, pageCount, pageSize, hasNextPage, hasPreviousPage } = pageConfig;
1720
@@ -42,11 +45,22 @@
4245
closeQuery: '.listbox-item'
4346
};
4447
48+
const getIndexInfomationString = () => {
49+
if (pageIndexStringType === 'pages') {
50+
return $pageCount > 0 ? `Page ${$pageIndex + 1} of ${$pageCount}` : 'No pages';
51+
} else {
52+
return `Displaying items ${$pageIndex * $pageSize + 1} - ${($pageIndex + 1) * $pageSize} of ${
53+
$pageCount * $pageSize
54+
}`;
55+
}
56+
};
57+
4558
$: goToFirstPageDisabled = !$pageIndex;
4659
$: goToLastPageDisabled = $pageIndex == $pageCount - 1;
4760
$: goToNextPageDisabled = !$hasNextPage;
4861
$: goToPreviousPageDisabled = !$hasPreviousPage;
4962
$: $pageSize = pageSizeDropdownValue;
63+
$: $pageCount, $pageIndex, $pageSize, (indexInformation = getIndexInfomationString());
5064
</script>
5165

5266
<div class="flex justify-between w-full items-stretch gap-10">
@@ -124,12 +138,6 @@
124138
>
125139
</div>
126140
<div class="flex justify-end items-center">
127-
<span class="text-sm text-gray-500">
128-
{#if $pageCount > 0}
129-
Page {$pageIndex + 1} of {$pageCount}
130-
{:else}
131-
No pages
132-
{/if}
133-
</span>
141+
<span class="text-sm text-gray-500">{indexInformation}</span>
134142
</div>
135143
</div>

src/lib/components/Table/TablePaginationServer.svelte

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
export let id; // Unique table ID
1111
export let pageIndex;
1212
export let pageSize;
13+
export let pageIndexStringType;
1314
export let pageSizes; // Available page sizes
1415
export let serverItemCount; // Total number of items expected from the server. `serverSide` must be true on table config.
1516
export let updateTable; // Function to update table
@@ -20,6 +21,9 @@
2021
let goToNextPageDisabled = true;
2122
let goToPreviousPageDisabled = true;
2223
24+
// Index information string
25+
let indexInformation = '';
26+
2327
// Handles the input change event
2428
const handleChange = (e) => {
2529
const value = e.target.value;
@@ -58,12 +62,23 @@
5862
updateTable();
5963
};
6064
65+
const getIndexInfomationString = () => {
66+
if (pageIndexStringType === 'pages') {
67+
return pageCount > 0 ? `Page ${$pageIndex + 1} of ${pageCount}` : 'No pages';
68+
} else {
69+
return `Showing ${$pageIndex * $pageSize + 1} - ${($pageIndex + 1) * $pageSize} of ${
70+
pageCount * $pageSize
71+
} items`;
72+
}
73+
};
74+
6175
$: pageCount = Math.ceil($serverItemCount / $pageSize);
6276
$: goToFirstPageDisabled = !$pageIndex;
6377
$: goToLastPageDisabled = $pageIndex == pageCount - 1;
6478
$: goToNextPageDisabled = $pageIndex == pageCount - 1;
6579
$: goToPreviousPageDisabled = !$pageIndex;
6680
$: $pageSize && updateTable(); // Update query when page size changes
81+
$: pageCount, $pageIndex, $pageSize, (indexInformation = getIndexInfomationString());
6782
6883
updateTable();
6984
</script>
@@ -127,15 +142,7 @@
127142
</div>
128143
<div class="flex justify-end items-center">
129144
<span class="text-sm text-gray-500">
130-
{#if pageCount > 0}
131-
{#if pageCount == 1}
132-
1 page
133-
{:else}
134-
{pageCount} pages
135-
{/if}
136-
{:else}
137-
No pages
138-
{/if}
145+
{indexInformation}
139146
</span>
140147
</div>
141148
</div>

src/lib/models/Models.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ export interface TableConfig<T> {
127127
exportable?: boolean; // false by default
128128
pageSizes?: number[]; // [5, 10, 20, 50, 100] by default
129129
defaultPageSize?: number; // 10 by default
130+
pageIndexStringType?: 'items' | 'pages'; // pages by default
130131
optionsComponent?: typeof SvelteComponent;
131132

132133
server?: ServerConfig;

src/routes/components/table/data/codeBlocks.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ export const usersBDHTML = `
146146
const usersBDConfig: TableConfig<UserBD> = {
147147
id: 'usersBD',
148148
data: usersBDStore,
149+
pageIndexStringType: 'items',
149150
columns: {
150151
dateOfBirth: {
151152
header: 'Date of birth',
@@ -213,6 +214,7 @@ export interface TableConfig<T> {
213214
exportable?: boolean; // false by default
214215
pageSizes?: number[]; // [5, 10, 20, 50, 100] by default
215216
defaultPageSize?: number; // 10 by default
217+
pageIndexStringType?: 'items' | 'pages'; // pages by default
216218
optionsComponent?: typeof SvelteComponent;
217219
218220
server?: ServerConfig;

src/routes/components/table/docs/TableConfigDocs.svelte

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,21 @@
190190
</p>
191191
</div>
192192

193+
<div class="items-center">
194+
<div class="flex gap-2">
195+
<div class="italic">pageIndexStringType:</div>
196+
<div class="font-bold"><i>"items"</i> or <i>"pages"</i></div>
197+
</div>
198+
199+
<p class="text-xl pl-10">
200+
Whether the table should display page index information by number of items or pages. <code
201+
class="!text-xl bg-tertiary-300 dark:bg-tertiary-700/50 rounded-md p-0.5 text-primary-500"
202+
>"pages"</code
203+
>
204+
by default.
205+
</p>
206+
</div>
207+
193208
<div class="items-center">
194209
<div class="flex gap-2">
195210
<div class="italic">optionsComponent:</div>

src/routes/components/table/examples/TableDate.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
const usersBDConfig: TableConfig<UserBD> = {
1010
id: 'usersBD',
1111
data: usersBDStore,
12+
pageIndexStringType: 'items',
1213
columns: {
1314
dateOfBirth: {
1415
header: 'Date of birth',
@@ -24,7 +25,7 @@
2425
</script>
2526

2627
<div id="usersBD">
27-
<CodeContainer title="Date" svelte={usersBDHTML} data={usersBDStoreCode}>
28+
<CodeContainer title="Date (+ page index type of items)" svelte={usersBDHTML} data={usersBDStoreCode}>
2829
<Table config={usersBDConfig} />
2930
</CodeContainer>
3031
</div>

0 commit comments

Comments
 (0)