From 294f8a45ff5aa6e8f11bf2fdfd44071ef6814923 Mon Sep 17 00:00:00 2001 From: Geriano Date: Fri, 30 Sep 2022 13:19:34 +0700 Subject: [PATCH] refactoring data table builder --- resources/css/app.css | 12 ++ resources/js/Components/DataTable/Builder.vue | 103 ++++++------------ resources/js/Components/DataTable/Th.vue | 17 ++- 3 files changed, 64 insertions(+), 68 deletions(-) diff --git a/resources/css/app.css b/resources/css/app.css index b5c61c9..9f83daa 100644 --- a/resources/css/app.css +++ b/resources/css/app.css @@ -1,3 +1,15 @@ @tailwind base; @tailwind components; @tailwind utilities; + +table { + @apply border-collapse w-full; +} + +thead, tfoot { + @apply bg-gray-200 dark:bg-gray-800 dark:text-gray-200; +} + +thead tr, tfoot tr { + @apply bg-inherit text-inherit; +} diff --git a/resources/js/Components/DataTable/Builder.vue b/resources/js/Components/DataTable/Builder.vue index 2ce8bb3..f548d1c 100644 --- a/resources/js/Components/DataTable/Builder.vue +++ b/resources/js/Components/DataTable/Builder.vue @@ -2,7 +2,8 @@ import { useForm } from '@inertiajs/inertia-vue3' import axios from 'axios' import Swal from 'sweetalert2' -import { getCurrentInstance, onMounted, onUpdated, ref } from 'vue' +import { getCurrentInstance, nextTick, onMounted, onUnmounted, onUpdated, ref } from 'vue' +import { cloneDeep } from 'lodash' const self = getCurrentInstance() const { url, sticky } = defineProps({ @@ -13,7 +14,9 @@ const { url, sticky } = defineProps({ }, }) -const paginator = ref({}) +const paginator = ref({ + data: [], +}) const processing = ref(false) const last = ref(null) const config = useForm({ @@ -33,17 +36,18 @@ const goTo = link => { } config.page = Number(link.url.match(/page=([\d]+)/)[1]) - paginator.value.current_page !== config.page && fetch() + paginator.value.current_page !== config.page && refresh() } -const fetch = async u => { +const refresh = async u => { try { processing.value = true - const a = last.value = u || url - const response = await axios.post(a || last.value, config.data()) - paginator.value = response.data + const prev = last.value = u || url + const { data } = await axios.post(prev || last.value, config.data()) + paginator.value = data + self.proxy.$forceUpdate() } catch (e) { - const respose = await Swal.fire({ + const { isConfirmed } = await Swal.fire({ title: 'error', text: `${e}`, icon: 'error', @@ -51,67 +55,27 @@ const fetch = async u => { showCloseButton: true, }) - if (respose.isConfirmed) { - return fetch(last.value) + if (isConfirmed) { + return await refresh(last.value) } + } finally { + processing.value = false } - processing.value = false } -const createFloatingTh = () => { - const { thead, tfoot, tbody } = self.refs +onMounted(refresh) - if (thead) { - let ths = thead.querySelectorAll('th') - ths.forEach((th, i) => { - const floating = document.createElement('div') - floating.className = 'absolute top-0 left-0 w-full h-full border border-1 border-inherit' - i === 0 && floating.classList.add('rounded-tl-md') - i + 1 === ths.length && floating.classList.add('rounded-tr-md') - - th.append(floating) - }) - } - - if (tfoot) { - let ths = tfoot.querySelectorAll('th') - ths.forEach((th, i) => { - const floating = document.createElement('div') - floating.className = 'absolute bottom-0 left-0 w-full h-full border border-1 border-inherit' - i === 0 && floating.classList.add('rounded-bl-md') - i + 1 === ths.length && floating.classList.add('rounded-br-md') - - th.append(floating) - }) - } +const all = { + url, + config, + refresh, + paginator: paginator.value, + data: paginator.value.data, + processing: !processing.value && !paginator.value?.data?.length, } -const inherit = () => { - const { thead, tfoot } = self.refs - - if (thead && tfoot) { - thead.querySelectorAll('th').forEach(th => th.classList.add('bg-inherit', 'border-inherit')) - tfoot.querySelectorAll('th').forEach(th => th.classList.add('bg-inherit', 'border-inherit')) - } -} - -const rounded = () => { - const { links } = self.refs - - links && links.firstElementChild?.classList.add('rounded-l-md') - links && links.lastElementChild?.classList.add('rounded-r-md') -} - -defineExpose({ - refresh: fetch, -}) - -onMounted(fetch) -onMounted(() => config.sticky && createFloatingTh()) -onMounted(() => inherit()) -onMounted(() => rounded()) -onUpdated(() => rounded()) +defineExpose(all)