diff --git a/src/components/TablePagination.vue b/src/components/TablePagination.vue new file mode 100644 index 000000000..02ed5960a --- /dev/null +++ b/src/components/TablePagination.vue @@ -0,0 +1,239 @@ + + + + diff --git a/src/composables/usePagination.ts b/src/composables/usePagination.ts new file mode 100644 index 000000000..cee46621d --- /dev/null +++ b/src/composables/usePagination.ts @@ -0,0 +1,54 @@ +import { ref, computed } from "vue"; +import type { PropsTable } from "@/interface/index/PropsTable"; + +export function usePagination( + defaultSort = "", + fetchFunction?: () => Promise +) { + const pagination = ref({ + sortBy: defaultSort, + descending: true, + page: 1, + rowsPerPage: 10, + rowsNumber: 0, + }); + + const params = computed(() => { + const queryParams: Record = { + page: pagination.value.page.toString(), + pageSize: pagination.value.rowsPerPage.toString(), + }; + + if (pagination.value.sortBy) { + queryParams.sortBy = pagination.value.sortBy; + queryParams.descending = ( + pagination.value.descending ?? false + ).toString(); + } + + return queryParams; + }); + + async function onRequest(props: PropsTable.RequestProps) { + const newPagination = props?.pagination || props; + if (!newPagination?.page || !newPagination?.rowsPerPage) return; + + pagination.value = { ...newPagination }; + if (fetchFunction) { + await fetchFunction(); // เรียกฟังก์ชันที่ส่งเข้ามา + } + } + + async function checkAndUpdatePage(totalRows: number) { + if (totalRows === 1 && pagination.value.page > 1) { + pagination.value.page = pagination.value.page - 1; + } + } + + return { + pagination, + params, + onRequest, + checkAndUpdatePage, + }; +} diff --git a/src/interface/index/PropsTable.ts b/src/interface/index/PropsTable.ts new file mode 100644 index 000000000..b9d4fe063 --- /dev/null +++ b/src/interface/index/PropsTable.ts @@ -0,0 +1,36 @@ +/** Namespace สำหรับ Table-related types */ +export namespace PropsTable { + /** Interface สำหรับ pagination object */ + export interface Pagination { + /** หน้าปัจจุบัน (เริ่มจาก 1) */ + page: number; + /** จำนวนแถวต่อหน้า */ + rowsPerPage: number; + /** จำนวนแถวทั้งหมด */ + rowsNumber?: number; + /** คอลัมน์ที่ใช้ sort */ + sortBy?: string; + /** เรียงจากมากไปน้อย */ + descending?: boolean; + + rowsTotal?: number; + } + + /** Interface สำหรับ request props จาก d-table */ + export interface RequestProps { + /** ข้อมูล pagination */ + pagination: Pagination; + /** ตัวกรองข้อมูล */ + filter?: any; + /** function สำหรับดึงค่าจาก cell */ + getCellValue?: (col: any, row: any) => any; + } + + /** Union type สำหรับ handleRequest function */ + export type HandleRequestProps = RequestProps; +} + +// Export แบบเดิมเพื่อ backward compatibility +export type TablePagination = PropsTable.Pagination; +export type TableRequestProps = PropsTable.RequestProps; +export type HandleRequestProps = PropsTable.HandleRequestProps; diff --git a/src/main.ts b/src/main.ts index e179635f1..570937672 100644 --- a/src/main.ts +++ b/src/main.ts @@ -73,5 +73,10 @@ app.component( defineAsyncComponent(() => import("@/components/Table.vue")) ); +app.component( + "p-table", + defineAsyncComponent(() => import("@/components/TablePagination.vue")) +); + app.config.globalProperties.$http = http; app.mount("#app"); diff --git a/src/modules/01_masterdata/components/competency/01ListCompetency.vue b/src/modules/01_masterdata/components/competency/01ListCompetency.vue index ee6fcfc0e..aa3ab91e0 100644 --- a/src/modules/01_masterdata/components/competency/01ListCompetency.vue +++ b/src/modules/01_masterdata/components/competency/01ListCompetency.vue @@ -1,5 +1,5 @@ @@ -436,7 +426,7 @@ onMounted(async () => {
- { :paging="true" :rows-per-page-options="[20, 50, 100]" :visible-columns="visibleColumns" - @update:pagination="updatePageSize" + @request="onRequest" + v-model:pagination="pagination" > > - - +
diff --git a/src/modules/05_placement/components/probation/MainProbation.vue b/src/modules/05_placement/components/probation/MainProbation.vue index 3af5063cf..31c5a2227 100644 --- a/src/modules/05_placement/components/probation/MainProbation.vue +++ b/src/modules/05_placement/components/probation/MainProbation.vue @@ -1,5 +1,5 @@