To account for different viewing devices with varying screen dimensions, it is custom to show or hide columns in a table depending on the viewport width. Vuetify already offers classes to do so, which can easily be applied in the table items slot of <v-data-table>
:
~vue
{{ props.item.first_name }}
{{ props.item.last_name }}
{{ props.item.email }}
{{ props.item.phone }}
{{ props.item.country }}
~
However, for the table headers this is much more difficult to achieve. It is of course possible to create custom header markup which contains the display classes within <template slot="headers" scope="props">
, but, as far as I know, this also forces one to wire up all the code for pagination and sorting himself, instead of relying on the built-in features.
Would you think it is feasible and desirable to add an extra field to the header items object that controls for visibility? e.g.
~javascript
headers: [
{
text: 'Contact',
value: 'contact.name',
align: 'left',
},
{
text: 'E-mail',
value: 'contact.email',
align: 'left',
visibility: 'hidden-sm-and-down'
},
{
text: 'Created',
value: 'created_at',
align: 'left',
visibility: 'hidden-xs-only'
}
],
~
I currently implemented this functionality with:
~vue
:class="[
'column sortable',
table.pagination.descending ? 'desc' : 'asc',
header.value === table.pagination.sortBy ? 'active' : '',
header.align === 'left' ? 'text-xs-left' : '',
header.visibility''
]"
@click="changeSort(header.value)"
>
{{ header.text }}
~
and for the header data:
~~~javascript
headers: [
{
text: 'Contact',
value: 'contact.first_name',
align: 'left',
},
{
text: 'E-mail',
value: 'contact.email',
align: 'left',
visibility: 'hidden-xs-only'
},
{
text: 'Created',
value: 'created_at',
align: 'left'
},
{}
],
~~~
So the only line that I think needs to be added to make this work in the internal header code of v-data-table is
~
header.visibility
~
to the header fields class definitions.
I would suggest naming the field class
or classes
instead of visibility
to make the solution more generic
Agreed. Probably there are more situations in which you want to attach custom classes to the headers.
Awesome!!
Nice work-around, but I'd really like to see native to vuetify style and class properties for headers that get passed to the th.
Why wouldn't that be possible with this method?
But how to hide all header only for xs ?? I do this but sorting not run well
<template slot="headers" slot-scope="props">
<tr v-if="$vuetify.breakpoint.xs" style="display:none">
</tr>
<tr v-else>
<th v-for="header in props.headers" :key="header.text" >
{{ header.text }}
</th>
</tr>
</template>
@hscstudio If you have any additional questions, please direct them to the official Discord server.
Most helpful comment
I currently implemented this functionality with:
~vue
:class="[
arrow_upward
arrow_upward
'column sortable',
table.pagination.descending ? 'desc' : 'asc',
header.value === table.pagination.sortBy ? 'active' : '',
header.align === 'left' ? 'text-xs-left' : '',
header.visibility''
]"
@click="changeSort(header.value)"
>
{{ header.text }}
~
and for the header data:
~~~javascript
headers: [
{
text: 'Contact',
value: 'contact.first_name',
align: 'left',
},
{
text: 'E-mail',
value: 'contact.email',
align: 'left',
visibility: 'hidden-xs-only'
},
{
text: 'Created',
value: 'created_at',
align: 'left'
},
{}
],
~~~
So the only line that I think needs to be added to make this work in the internal header code of v-data-table is
~header.visibility
~
to the header fields class definitions.