question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

v-data-table: add header parameter to control visibility

See original GitHub issue

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>:

<template slot="items" scope="props">
    <td>{{ props.item.first_name }}</td>
    <td>{{ props.item.last_name }}</td>
    <td class="hidden-xs-only">{{ props.item.email }}</td>
    <td class="hidden-xs-only">{{ props.item.phone }}</td>
    <td class="hidden-sm-and-down">{{ props.item.country }}</td>
    <td class="text-xs-right hidden-xs-only">{{ props.item.quotes_count }}</td>
</template>

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.

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'
    }
],

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:5
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
dschreijcommented, Aug 31, 2017

I currently implemented this functionality with:

<template slot="headers" scope="props">
    <tr>
        <th v-for="header in props.headers" :key="header.text"
            :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)"
        >
            <v-icon v-if="!header.align || header.align === 'right'">arrow_upward</v-icon>
            {{ header.text }}
            <v-icon v-if="header.align === 'left'">arrow_upward</v-icon>
        </th>
    </tr>
</template>

and for the header data:

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.

3reactions
nekosaurcommented, Sep 4, 2017

I would suggest naming the field class or classes instead of visibility to make the solution more generic

Read more comments on GitHub >

github_iconTop Results From Across the Web

v-data-table API - Vuetify
An array of objects that each describe a header column. See the example below for a definition of all properties. { text: string,...
Read more >
Vuetify insert action button in data-table and get row data
Add one more column to your headers definition: { text: "", value: "controls", sortable: false }; Do not override item slot (row rendering)....
Read more >
DataTables example - Show / hide columns dynamically
Name Position Office Age Start date Salary Airi Satou Accountant Tokyo 33 2008‑11‑28 $162,700 Angelica Ramos Chief Executive Officer (CEO) London 47 2009‑10‑09 $1,200,000 Ashton Cox...
Read more >
Datatable Header Menu - Webix
Datatable built-in header menu is used to control column visibility. ... To add a simple header menu, set headermenu:true in the DataTable config....
Read more >
Vuetify Datatable with show/hide columns - CodePen
Dessert (100g serving). Frozen Yogurt Calories. 159 Fat (g). 6 Carbs (g). 24 Protein... Dessert (100g serving). Ice cream sandwich Calories. 237 Fat (g)....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found