fix(registry): sort data
This commit is contained in:
parent
8d9059ad32
commit
e3345d7be3
8 changed files with 444 additions and 266 deletions
47
src/composables/usePagination.ts
Normal file
47
src/composables/usePagination.ts
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
import { ref, computed } from "vue";
|
||||
import type { PropsTable } from "@/interface/index/PropsTable";
|
||||
|
||||
export function usePagination(
|
||||
defaultSort = "",
|
||||
fetchFunction?: () => Promise<any>
|
||||
) {
|
||||
const pagination = ref<PropsTable.Pagination>({
|
||||
sortBy: defaultSort,
|
||||
descending: true,
|
||||
page: 1,
|
||||
rowsPerPage: 10,
|
||||
rowsNumber: 0,
|
||||
});
|
||||
|
||||
const params = computed(() => {
|
||||
const queryParams: Record<string, string> = {
|
||||
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(); // เรียกฟังก์ชันที่ส่งเข้ามา
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
pagination,
|
||||
params,
|
||||
onRequest,
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue