refactor(pagination): move pagination info display to component and improve layout

- Consolidate pagination info display logic in pagination component
- Remove duplicate computed properties from list components
- Improve pagination layout with better spacing and responsiveness
- Add skeleton loading support to data tables
This commit is contained in:
Khafid Prayoga
2025-09-04 11:26:58 +07:00
parent 6c70beb882
commit 3dbc1b8fd1
3 changed files with 40 additions and 99 deletions
+8 -45
View File
@@ -7,7 +7,7 @@ interface Props {
paginationMeta?: PaginationMeta paginationMeta?: PaginationMeta
} }
const props = defineProps<Props>() defineProps<Props>()
const emit = defineEmits<{ const emit = defineEmits<{
pageChange: [page: number] pageChange: [page: number]
@@ -16,57 +16,20 @@ const emit = defineEmits<{
function handlePageChange(page: number) { function handlePageChange(page: number) {
emit('pageChange', page) emit('pageChange', page)
} }
// Computed properties for formatted display
const formattedRecordCount = computed(() => {
if (!props.paginationMeta) return '0'
const count = props.paginationMeta.recordCount
if (count == null || count === undefined) return '0'
return Number(count).toLocaleString('id-ID')
})
const startRecord = computed(() => {
if (!props.paginationMeta) return 1
return ((props.paginationMeta.page - 1) * props.paginationMeta.pageSize) + 1
})
const endRecord = computed(() => {
if (!props.paginationMeta) return 0
return Math.min(props.paginationMeta.page * props.paginationMeta.pageSize, props.paginationMeta.recordCount)
})
</script> </script>
<template> <template>
<div class="space-y-4"> <div class="space-y-4">
<PubBaseDataTable <PubBaseDataTable
:rows="data" :rows="data" :cols="cols" :header="header" :keys="keys" :func-parsed="funcParsed"
:cols="cols" :func-html="funcHtml" :func-component="funcComponent" :skeleton-size="paginationMeta?.pageSize"
:header="header" />
:keys="keys"
:func-parsed="funcParsed"
:func-html="funcHtml"
:func-component="funcComponent"
/>
<!-- Data info and pagination --> <template v-if="paginationMeta">
<div v-if="paginationMeta" class="flex align-center justify-between">
<!-- Data info - always show -->
<div class="flex items-center justify-between px-2 mb-4">
<div class="flex-1 text-sm text-muted-foreground">
Menampilkan {{ startRecord }}
hingga {{ endRecord }}
dari {{ formattedRecordCount }} data
</div>
</div>
<!-- Pagination controls - only show when multiple pages -->
<div v-if="paginationMeta.totalPage > 1"> <div v-if="paginationMeta.totalPage > 1">
<PubCustomUiPagination <PubCustomUiPagination :pagination-meta="paginationMeta" @page-change="handlePageChange" />
:pagination-meta="paginationMeta"
:show-info="false"
@page-change="handlePageChange"
/>
</div> </div>
</div> </template>
</div> </div>
</template> </template>
+6 -33
View File
@@ -7,7 +7,7 @@ interface Props {
paginationMeta?: PaginationMeta paginationMeta?: PaginationMeta
} }
const props = defineProps<Props>() defineProps<Props>()
const emit = defineEmits<{ const emit = defineEmits<{
pageChange: [page: number] pageChange: [page: number]
@@ -16,48 +16,21 @@ const emit = defineEmits<{
function handlePageChange(page: number) { function handlePageChange(page: number) {
emit('pageChange', page) emit('pageChange', page)
} }
// Computed properties for formatted display
const formattedRecordCount = computed(() => {
if (!props.paginationMeta) return '0'
const count = props.paginationMeta.recordCount
if (count == null || count === undefined) return '0'
return Number(count).toLocaleString('id-ID')
})
const startRecord = computed(() => {
if (!props.paginationMeta) return 1
return ((props.paginationMeta.page - 1) * props.paginationMeta.pageSize) + 1
})
const endRecord = computed(() => {
if (!props.paginationMeta) return 0
return Math.min(props.paginationMeta.page * props.paginationMeta.pageSize, props.paginationMeta.recordCount)
})
</script> </script>
<template> <template>
<div class="space-y-4"> <div class="space-y-4">
<PubBaseDataTable <PubBaseDataTable
:rows="data" :cols="cols" :header="header" :keys="keys" :func-parsed="funcParsed" :rows="data" :cols="cols" :header="header" :keys="keys" :func-parsed="funcParsed"
:func-html="funcHtml" :func-component="funcComponent" :func-html="funcHtml" :func-component="funcComponent" :skeleton-size="paginationMeta?.pageSize"
/> />
<!-- Data info and pagination --> <!-- Data info and pagination -->
<div v-if="paginationMeta" class="flex align-center justify-between"> <template v-if="paginationMeta">
<!-- Data info - always show -->
<div class="flex items-center justify-between px-2 mb-4">
<div class="flex-1 text-sm text-muted-foreground">
Menampilkan {{ startRecord }}
hingga {{ endRecord }}
dari {{ formattedRecordCount }} data
</div>
</div>
<!-- Pagination controls - only show when multiple pages -->
<div v-if="paginationMeta.totalPage > 1"> <div v-if="paginationMeta.totalPage > 1">
<PubCustomUiPagination :pagination-meta="paginationMeta" :show-info="false" @page-change="handlePageChange" /> <PubCustomUiPagination :pagination-meta="paginationMeta" @page-change="handlePageChange" />
</div> </div>
</div> </template>
</div> </div>
</template> </template>
@@ -87,39 +87,44 @@ function getButtonClass(pageNumber: number) {
</script> </script>
<template> <template>
<div class="flex items-center justify-between px-2"> <div class="flex items-center justify-between px-2 py-2 w-full min-w-0">
<!-- Info text --> <!-- Info text -->
<div v-if="showInfo" class="flex-1 text-sm text-muted-foreground"> <div v-if="showInfo" class="text-sm text-muted-foreground shrink-0">
Menampilkan {{ startRecord }} Menampilkan {{ startRecord }}
hingga {{ endRecord }} hingga {{ endRecord }}
dari {{ formattedRecordCount }} data dari {{ formattedRecordCount }} data
</div> </div>
<div v-else class="flex-1"></div> <div v-else class="shrink-0"></div>
<!-- Spacer untuk memastikan ada ruang di tengah -->
<div class="flex-1 min-w-4"></div>
<!-- Pagination controls --> <!-- Pagination controls -->
<Pagination <div class="shrink-0">
<Pagination
v-slot="{ page }" :total="paginationMeta.recordCount" :sibling-count="1" :page="paginationMeta.page" v-slot="{ page }" :total="paginationMeta.recordCount" :sibling-count="1" :page="paginationMeta.page"
:items-per-page="paginationMeta.pageSize" show-edges :items-per-page="paginationMeta.pageSize" show-edges
> >
<PaginationList v-slot="{ items }" class="flex items-center gap-1"> <PaginationList v-slot="{ items }" class="flex items-center gap-1">
<PaginationFirst :disabled="!paginationMeta.hasPrev" @click="handleFirst" /> <PaginationFirst :disabled="!paginationMeta.hasPrev" @click="handleFirst" />
<PaginationPrev :disabled="!paginationMeta.hasPrev" @click="handlePrev" /> <PaginationPrev :disabled="!paginationMeta.hasPrev" @click="handlePrev" />
<template v-for="(item, index) in items"> <template v-for="(item, index) in items">
<PaginationListItem <PaginationListItem
v-if="item.type === 'page'" :key="index" :value="item.value" as-child v-if="item.type === 'page'" :key="index" :value="item.value" as-child
@click="handlePageChange(item.value)" @click="handlePageChange(item.value)"
> >
<Button :class="getButtonClass(item.value)" :variant="item.value === page ? 'default' : 'outline'"> <Button :class="getButtonClass(item.value)" :variant="item.value === page ? 'default' : 'outline'">
{{ item.value }} {{ item.value }}
</Button> </Button>
</PaginationListItem> </PaginationListItem>
<PaginationEllipsis v-else :key="item.type" :index="index" /> <PaginationEllipsis v-else :key="item.type" :index="index" />
</template> </template>
<PaginationNext :disabled="!paginationMeta.hasNext" @click="handleNext" /> <PaginationNext :disabled="!paginationMeta.hasNext" @click="handleNext" />
<PaginationLast :disabled="!paginationMeta.hasNext" @click="handleLast" /> <PaginationLast :disabled="!paginationMeta.hasNext" @click="handleLast" />
</PaginationList> </PaginationList>
</Pagination> </Pagination>
</div>
</div> </div>
</template> </template>