From 20051d72468684c3df0429d9a30efd985b931bc8 Mon Sep 17 00:00:00 2001 From: joan Date: Tue, 10 Jan 2023 14:58:24 +0100 Subject: [PATCH] Pagination changes --- src/components/PaginateData.vue | 121 ++++++++-------------------- src/composables/useArrayData.js | 80 +++++++++++++----- src/pages/Customer/CustomerList.vue | 33 +++++--- src/stores/useArrayDataStore.js | 13 ++- 4 files changed, 125 insertions(+), 122 deletions(-) diff --git a/src/components/PaginateData.vue b/src/components/PaginateData.vue index 8c297055a..5c2562d60 100644 --- a/src/components/PaginateData.vue +++ b/src/components/PaginateData.vue @@ -45,21 +45,7 @@ const $props = defineProps({ }); const emit = defineEmits(['onFetch', 'onPaginate']); -// defineExpose({ refresh }); - -onMounted(() => { - if ($props.autoLoad) paginate(); -}); - -watch( - () => $props.data, - () => { - rows.value = $props.data; - } -); - const isLoading = ref(false); -const hasMoreData = ref(false); const pagination = ref({ sortBy: $props.sortBy, rowsPerPage: $props.rowsPerPage, @@ -68,71 +54,50 @@ const pagination = ref({ const arrayData = useArrayData('customers', { url: $props.url, + filter: $props.filter, + where: $props.where, limit: $props.rowsPerPage, + order: $props.sortBy, }); -const rows = arrayData.data; +const store = arrayData.store; -// async function fetch() { -// const { page, rowsPerPage, sortBy } = pagination.value; +onMounted(() => { + if ($props.autoLoad) paginate(); +}); -// if (!$props.url) return; - -// const filter = { -// limit: rowsPerPage, -// skip: rowsPerPage * (page - 1), -// }; - -// Object.assign(filter, $props.filter); - -// if ($props.where) filter.where = $props.where; -// if ($props.sortBy) filter.order = $props.sortBy; -// if ($props.limit) filter.limit = $props.limit; - -// if (sortBy) filter.order = sortBy; - -// arrayData.apply() - -// const { data } = await axios.get($props.url, { -// params: { filter }, -// }); - -// isLoading.value = false; - -// return data; -// } +watch( + () => $props.data, + () => { + store.data = $props.data; + } +); async function paginate() { const { page, rowsPerPage, sortBy, descending } = pagination.value; if (!$props.url) return; - const filter = { - limit: rowsPerPage, - skip: rowsPerPage * (page - 1), - }; + // const filter = { + // limit: rowsPerPage, + // skip: rowsPerPage * (page - 1), + // }; - Object.assign(filter, $props.filter); + // Object.assign(filter, $props.filter); - if ($props.where) filter.where = $props.where; - if ($props.sortBy) filter.order = $props.sortBy; - if ($props.limit) filter.limit = $props.limit; + // if ($props.where) filter.where = $props.where; + // if ($props.sortBy) filter.order = $props.sortBy; + // if ($props.limit) filter.limit = $props.limit; - if (sortBy) filter.order = sortBy; + // if (sortBy) filter.order = sortBy; - const data = await arrayData.apply(filter); + await arrayData.loadMore(); - if (!data) { + if (!arrayData.hasMoreData.value) { isLoading.value = false; return; } - hasMoreData.value = data.length === rowsPerPage; - console.log(hasMoreData.value); - - // if (!rows.value) rows.value = []; - // for (const row of data) rows.value.push(row); - - pagination.value.rowsNumber = rows.length; + pagination.value.rowsNumber = store.data.length; pagination.value.page = page; pagination.value.rowsPerPage = rowsPerPage; pagination.value.sortBy = sortBy; @@ -140,48 +105,28 @@ async function paginate() { isLoading.value = false; - emit('onFetch', rows); - emit('onPaginate', data); + emit('onFetch', store.data); + emit('onPaginate'); } -// async function refresh() { -// const { rowsPerPage } = pagination.value; - -// const data = await fetch(); - -// if (!data) { -// isLoading.value = false; -// return; -// } - -// hasMoreData.value = data.length === rowsPerPage; - -// if (!rows.value) rows.value = []; -// rows.value = data; - -// isLoading.value = false; - -// emit('onFetch', rows); -// } - async function onLoad(...params) { const done = params[1]; - if (!rows || rows.length === 0 || !$props.url) return done(false); + if (!store.data || store.data.length === 0 || !$props.url) return done(false); pagination.value.page = pagination.value.page + 1; await paginate(); - const endOfPages = !hasMoreData.value; + const endOfPages = !arrayData.hasMoreData.value; done(endOfPages); }