17
votes

My goal is to create a Vuetify 2 data table from a list of car models. Data needs to be grouped by vendor with a customized group header row and also the item rows for each car model needs to be customized. Below is a very reduced example to show my main problem which is that Vuetify fully ignores my template for the item-slot and uses the default behaviour instead.

How can I make Vuetify use that template as well with avoiding to use a single template for each item column? ... because in my real world example there are a lot of columns which needs to be customized.

Vue code:

<div id="app">
  <v-app>
    <v-data-table
      dense
      disable-sort
      :headers="headers"
      hide-default-footer
      :items="cars"
      item-key="id"
      group-by="vendor"
    >
      <template v-slot:group.header="{items, isOpen, toggle}">
        <th colspan="2">
          <v-icon @click="toggle"
            >{{ isOpen ? 'mdi-minus' : 'mdi-plus' }}
          </v-icon>
          {{ items[0].vendor }}
        </th>
      </template>
      <template v-slot:item="{ item }">
        <tr>
          <td><strong>{{ item.name }}</strong></td>
          <td>{{ item.power }} HP</td>
        </tr>
      </template>
    </v-data-table>
  </v-app>
</div>

Javascript code:

new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  data () {
    return {
      headers: [
        { text: 'Model name', value: 'name' },
        { text: 'Power', value: 'power' }
      ],
      cars: [
        {
          id: 1,
          name: '320i',
          vendor: 'BMW',
          power: 170
        },
        {
          id: 2,
          name: 'M5',
          vendor: 'BMW',
          power: 350
        },
        {
          id: 3,
          name: 'Golf GTD',
          vendor: 'Volkswagen',
          power: 184
        },
        {
          id: 4,
          name: 'Polo GTI',
          vendor: 'Volkswagen',
          power: 190
        }
      ]
    }
  }
})

Codepen demo can be found here.

1
It would be nice to get a comment what's wrong with that question when downvoting it... - GreenTurtle
I upvoted your question just because there's any reason to downvote it. However, I think using the group.header slot makes the item slot ignored, so the only way seems to be to use the item.name slot for each colum. It's not so much more code... - Fab
Thanks! You're probably right even if this is not mentioned at all in the documentation (or I'm just blind). And to be honest I see no reason to ignore it because they should be unrelated to each other. However, this seems to be the only way to go right now even if my real world code has way more columns (14) and they all needs to be customized in some way or the other, so it will add a lot of boilerplate code... - GreenTurtle
Sorry @GregK - no news regarding this topic on my side. Solved the problem with the definition of each column separately. Not very smart but at least it works. - GreenTurtle
Just so you know @GreenTurtle, that example was a crazy huge help in getting where I wanted. I am extremely grateful! - GregK

1 Answers

2
votes

The following behavior is a bug in previous Vuetify versions. Using Vuetify 2.4.9+ (or even earlier) will fix your problem.

Check this codepen: https://codepen.io/seaskyways/pen/VwPbyER

It's the same as yours just with updated Vuetify.

// and this is some code cuz stackoverflow doesn't allow codepens without code