htmlvue.jsvuejs2vuetify.jsv-data-table

Vuetify <v-data-table> custom <th> header


I am using Vuetify <v-data-table>. How to add button on table header <th>?

This code is what I have tried so far.

<v-data-table v-model="selected" :headers="headers" :items="desserts">
  <template slot="items" slot-scope="row">
    <th><button>button</button></th>
  </template>
</v-data-table>  

<script>
export default {
    data:() =>({
    headers: [
          {
            text: 'Dessert (100g serving)',
            align: 'start',
            sortable: true,
            value: 'name',
          },
          { text: 'Calories', value: 'calories' },
          { text: 'Fat (g)', value: 'fat' },
          { text: 'Carbs (g)', value: 'carbs' },
          { text: 'Protein (g)', value: 'protein' },
          { text: 'Iron (%)', value: 'iron' },
        ],
});
</script>

Please help.


Solution

  • You can use the header.<fieldname> slot template.

    For example, to target the second column, calories:

    <v-data-table v-model="selected" :headers="headers" :items="desserts">
    
      <template v-slot:header.calories="{ header }">
        {{ header.text }}
        <v-btn>Button</v-btn>
      </template>
    
    </v-data-table>