From 05a98ebe87d7a88e8b96b144c53250a40d724ec3 Mon Sep 17 00:00:00 2001 From: Elias Schneider Date: Sat, 18 Jan 2025 22:29:20 +0100 Subject: [PATCH] fix: search input not displayed if response hasn't any items --- backend/internal/utils/paging_util.go | 7 +- .../src/lib/components/advanced-table.svelte | 221 +++++++++--------- 2 files changed, 119 insertions(+), 109 deletions(-) diff --git a/backend/internal/utils/paging_util.go b/backend/internal/utils/paging_util.go index 13143d7..8698bba 100644 --- a/backend/internal/utils/paging_util.go +++ b/backend/internal/utils/paging_util.go @@ -63,8 +63,13 @@ func Paginate(page int, pageSize int, query *gorm.DB, result interface{}) (Pagin return PaginationResponse{}, err } + totalPages := (totalItems + int64(pageSize) - 1) / int64(pageSize) + if totalItems == 0 { + totalPages = 1 + } + return PaginationResponse{ - TotalPages: (totalItems + int64(pageSize) - 1) / int64(pageSize), + TotalPages: totalPages, TotalItems: totalItems, CurrentPage: page, ItemsPerPage: pageSize, diff --git a/frontend/src/lib/components/advanced-table.svelte b/frontend/src/lib/components/advanced-table.svelte index c7ef605..c41a9f3 100644 --- a/frontend/src/lib/components/advanced-table.svelte +++ b/frontend/src/lib/components/advanced-table.svelte @@ -32,6 +32,8 @@ rows: Snippet<[{ item: T }]>; } = $props(); + let searchValue = $state(''); + if (!requestOptions) { requestOptions = { search: '', @@ -55,9 +57,10 @@ return true; }); - const onSearch = debounced(async (searchValue: string) => { - requestOptions.search = searchValue; - onRefresh(requestOptions); + const onSearch = debounced(async (search: string) => { + requestOptions.search = search; + await onRefresh(requestOptions); + searchValue = search; }, 300); async function onAllCheck(checked: boolean) { @@ -95,122 +98,124 @@ } -{#if items.data.length === 0} +{#if !withoutSearch} + onSearch((e.target as HTMLInputElement).value)} + /> +{/if} + +{#if items.data.length === 0 && searchValue === ''}

No items found

{:else} -
- {#if !withoutSearch} - onSearch((e.target as HTMLInputElement).value)} - /> - {/if} - - - - - {#if selectedIds} - - onAllCheck(c as boolean)} /> - - {/if} - {#each columns as column} - - {#if column.sortColumn} - - {:else} + + + + {#if selectedIds} + + onAllCheck(c as boolean)} /> + + {/if} + {#each columns as column} + + {#if column.sortColumn} + + {:else} + {column.label} {/if} - {@render rows({ item })} - + {/each} - - + + + + {#each items.data as item} + + {#if selectedIds} + + onCheck(c as boolean, item.id)} + /> + + {/if} + {@render rows({ item })} + + {/each} + + -
-
-

Items per page

- onPageSizeChange(v?.value as number)} - > - - {items.pagination.itemsPerPage} - - - {#each availablePageSizes as size} - {size} - {/each} - - -
- +
+

Items per page

+ onPageSizeChange(v?.value as number)} > - - - - - {#each pages as page (page.key)} - {#if page.type !== 'ellipsis'} - - - {page.value} - - - {/if} + + {items.pagination.itemsPerPage} + + + {#each availablePageSizes as size} + {size} {/each} - - - - - + +
+ + + + + + {#each pages as page (page.key)} + {#if page.type !== 'ellipsis' && page.value != 0} + + + {page.value} + + + {/if} + {/each} + + + + +
{/if}