Skip to content

feat(VTable): make it responsive #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/components/data-table/VTable.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<div class="border border-2 rounded-2 overflow-hidden position-relative bg-body">
<table class="table mb-0">
<VTableHeader :columns="columns"/>
<VTableHeader :columns="columns" :class="{ 'd-none': isMobile }"/>
<VTableBody
:columns="columns"
:row-slot="rowSlot"
Expand All @@ -27,6 +27,9 @@
<script>
import { computed } from 'vue';

// Utils
import Breakpoints from '@/utils/breakpoints';

// Components
import VTableHeader from '@/components/data-table/VTableHeader.vue';
import VTableBody from '@/components/data-table/VTableBody.vue';
Expand Down Expand Up @@ -66,6 +69,8 @@
emits: ['update:page', 'update:itemsPerPage'],

setup(props, context) {
const isMobile = Breakpoints.down(Breakpoints.SM);

const rowSlot = computed(() => context.slots.row);
const columns = computed(() => context.slots.default());

Expand All @@ -83,6 +88,8 @@
rowSlot,
columns,

isMobile,

pageRef,
itemsPerPageRef,

Expand Down
34 changes: 33 additions & 1 deletion src/components/data-table/VTableBody.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
:columns="renderColumns(item, rowIndex)"
:item="item"
:rowIndex="rowIndex"
:class="{ 'border-bottom': isMobile }"
/>
</template>

<template v-else>
<tr
v-for="(item, rowIndex) of items"
:key="rowIndex"
:class="{ 'border-bottom': isMobile }"
>
<component
:is="column"
Expand All @@ -39,6 +41,7 @@

// Utils
import { hasVNodeSlot } from '@/utils/vue';
import Breakpoints from '@/utils/breakpoints';

export default {
name: 'VTableBody',
Expand All @@ -62,17 +65,44 @@
},

setup(props) {
const isMobile = Breakpoints.down(Breakpoints.SM);

function renderColumns(item, rowIndex) {
return props.columns.map(function (column) {
let content;
let title;

if (hasVNodeSlot(column, 'body')) {
content = h(column.children.body, { field: column.props.field, item, rowIndex });
} else {
content = resolveField(column, item);
}

if (hasVNodeSlot(column, 'header') && isMobile) {
title = h(column.children.header, { field: column.props.field, item, rowIndex });
} else if (isMobile){
title = column.props.header;
}

if (isMobile) {
return h(
"td",
{ class: "d-flex justify-content-between align-items-center border-bottom-0" },
[
h(
"div",
{ class: "fs-3 bg-transparent fw-medium" },
title
),
h(
"div",
{ class: "fs-3 bg-transparent" },
content
)
]
);
}

return h(
"td",
{ class: "bg-transparent" },
Expand All @@ -94,7 +124,9 @@
}

return {
renderColumns
renderColumns,

isMobile
};
}
};
Expand Down
12 changes: 10 additions & 2 deletions src/components/data-table/VTableServer.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<template>
<div class="border border-2 rounded-2 overflow-hidden position-relative bg-body">
<table class="table mb-0">
<VTableHeader :columns="columns"/>
<VTableHeader :columns="columns" :class="{ 'd-none': isMobile }"/>

<VTableBody
:columns="columns"
:row-slot="rowSlot"
Expand Down Expand Up @@ -29,6 +30,9 @@
<script>
import { computed } from 'vue';

// Utils
import Breakpoints from '@/utils/breakpoints';

// Components
import VTableHeader from '@/components/data-table/VTableHeader.vue';
import VTableBody from '@/components/data-table/VTableBody.vue';
Expand Down Expand Up @@ -69,6 +73,8 @@
emits: ['update:page', 'update:itemsPerPage'],

setup(props, context) {
const isMobile = Breakpoints.down(Breakpoints.SM);

const rowSlot = computed(() => context.slots.row);
const columns = computed(() => context.slots.default());

Expand All @@ -85,7 +91,9 @@
updateItemsPerPage,

columns,
rowSlot
rowSlot,

isMobile
};
}
};
Expand Down