fix load Table
This commit is contained in:
parent
1ec4a97538
commit
d39753fbde
56 changed files with 684 additions and 978 deletions
|
|
@ -10,13 +10,16 @@
|
|||
dense
|
||||
:pagination-label="paginationLabel"
|
||||
v-model:pagination="pagination"
|
||||
@request="onRequest"
|
||||
:grid="!$q.screen.gt.xs"
|
||||
:rows-per-page-options="[10, 25, 50, 100]"
|
||||
:loading="loading"
|
||||
>
|
||||
<template v-slot:pagination="scope">
|
||||
ทั้งหมด {{ attrs.rows.length }} รายการ
|
||||
ทั้งหมด {{ pagination.rowsNumber || attrs.rows.length }} รายการ
|
||||
<q-pagination
|
||||
v-model="pagination.page"
|
||||
@update:model-value="handlePageChange"
|
||||
active-color="primary"
|
||||
color="dark"
|
||||
:max="scope.pagesNumber"
|
||||
|
|
@ -27,9 +30,25 @@
|
|||
></q-pagination>
|
||||
</template>
|
||||
|
||||
<template v-for="(_, slot) in slots" v-slot:[slot]="scope">
|
||||
<template v-for="(_, slot) in slots" v-slot:[slot]="scope" :key="slot">
|
||||
<slot :name="slot" v-bind="scope || {}" />
|
||||
</template>
|
||||
|
||||
<template v-slot:loading>
|
||||
<q-inner-loading showing class="q-mt-lg">
|
||||
<q-spinner-dots color="primary" size="40px" />
|
||||
</q-inner-loading>
|
||||
</template>
|
||||
|
||||
<template v-slot:no-data>
|
||||
<div
|
||||
v-if="!loading && attrs.rows.length === 0"
|
||||
class="full-width row flex-center q-pa-sm rounded-borders text-weight-medium"
|
||||
>
|
||||
<span> ไม่พบข้อมูล </span>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- <template #item="props">
|
||||
<div class="q-pa-xs col-xs-12 col-sm-6 col-md-4 col-lg-3">
|
||||
<q-card bordered flat>
|
||||
|
|
@ -55,22 +74,45 @@
|
|||
</q-table>
|
||||
</template>
|
||||
<script setup lang="ts">
|
||||
import { ref, useAttrs, useSlots } from "vue";
|
||||
import { ref, useAttrs, useSlots, computed, watch } from "vue";
|
||||
const attrs = ref<any>(useAttrs());
|
||||
const slots = ref<any>(useSlots());
|
||||
|
||||
const emit = defineEmits(["update:pagination", "request"]);
|
||||
|
||||
const props = defineProps({
|
||||
paging: {
|
||||
type: Boolean,
|
||||
defualt: false,
|
||||
default: false,
|
||||
},
|
||||
loading: {
|
||||
type: Boolean,
|
||||
default: false,
|
||||
},
|
||||
pagination: {
|
||||
type: Object,
|
||||
default: () => ({
|
||||
sortBy: "desc",
|
||||
descending: false,
|
||||
page: 1,
|
||||
rowsPerPage: 10,
|
||||
// rowsNumber: 0,
|
||||
}),
|
||||
},
|
||||
});
|
||||
|
||||
const pagination = ref({
|
||||
sortBy: "desc",
|
||||
descending: false,
|
||||
page: 1,
|
||||
rowsPerPage: 10,
|
||||
// Internal pagination state ที่จะจัดการเอง
|
||||
const internalPagination = ref({
|
||||
...props.pagination,
|
||||
});
|
||||
|
||||
// ใช้ computed เพื่อ sync กับ props และ internal state
|
||||
const pagination = computed({
|
||||
get: () => internalPagination.value,
|
||||
set: (value) => {
|
||||
internalPagination.value = { ...value };
|
||||
emit("update:pagination", value);
|
||||
},
|
||||
});
|
||||
|
||||
const paginationLabel = (start: string, end: string, total: string) => {
|
||||
|
|
@ -78,6 +120,57 @@ const paginationLabel = (start: string, end: string, total: string) => {
|
|||
return " " + start + " ถึง " + end + " จากจำนวน " + total + " รายการ";
|
||||
else return start + "-" + end + " ใน " + total;
|
||||
};
|
||||
|
||||
function onRequest(requestProp: any) {
|
||||
if (!requestProp || !requestProp.pagination) {
|
||||
return;
|
||||
}
|
||||
|
||||
const { pagination: newPagination } = requestProp;
|
||||
|
||||
// ถ้าเป็นการเปลี่ยนหน้า (page หรือ rowsPerPage เปลี่ยน) ให้ยิง request
|
||||
const isPageChange =
|
||||
internalPagination.value.page !== newPagination.page ||
|
||||
internalPagination.value.rowsPerPage !== newPagination.rowsPerPage;
|
||||
|
||||
internalPagination.value = {
|
||||
...internalPagination.value,
|
||||
...newPagination,
|
||||
};
|
||||
|
||||
if (isPageChange) {
|
||||
emit("request", requestProp);
|
||||
}
|
||||
}
|
||||
|
||||
function handlePageChange(newPage: number) {
|
||||
if (!newPage || newPage < 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
internalPagination.value = {
|
||||
...internalPagination.value,
|
||||
page: newPage,
|
||||
};
|
||||
|
||||
// สร้าง request object เหมือนกับ onRequest
|
||||
const requestProp = {
|
||||
pagination: internalPagination.value,
|
||||
filter: null,
|
||||
getCellValue: () => {},
|
||||
};
|
||||
|
||||
emit("request", requestProp);
|
||||
}
|
||||
|
||||
// Watch props เพื่ออัปเดต internal state เมื่อ parent เปลี่ยน
|
||||
watch(
|
||||
() => props.pagination,
|
||||
(newPagination) => {
|
||||
internalPagination.value = { ...newPagination };
|
||||
},
|
||||
{ deep: true, immediate: true }
|
||||
);
|
||||
</script>
|
||||
|
||||
<style lang="scss">
|
||||
|
|
@ -113,5 +206,33 @@ const paginationLabel = (start: string, end: string, total: string) => {
|
|||
margin-bottom: 0 !important;
|
||||
min-height: 0px !important;
|
||||
}
|
||||
|
||||
// Loading styles
|
||||
.loading-overlay {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.loading-row {
|
||||
background: white;
|
||||
}
|
||||
|
||||
.loading-cell {
|
||||
text-align: center;
|
||||
padding: 60px 20px;
|
||||
border: none;
|
||||
}
|
||||
|
||||
.loading-content {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
gap: 16px;
|
||||
}
|
||||
|
||||
.loading-text {
|
||||
color: #666;
|
||||
font-size: 14px;
|
||||
font-weight: 500;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue