Mateus Fernando
Mateus Fernando

Reputation: 99

How to dynamically change the header of a v-data-table?

I'm trying to make a v-data-table that is populated with json data (Vue + Vuetify + Vue-Resource). I can show the data without problems, but I need to change the first colum of the header to let visible what data the user is viewing actually. At this moment I'm using a static header without the label that I want:

headers: [
    {
      text: "",
      align: "left",
      sortable: false,
      value: "name",
      id: "primeiraColunaColuna"
    },

    { text: "total QTD", value: "total QTD" },
    { text: "total", value: "Total" },
    { text: "Date", value: "Date" },
    { text: "Time", value: "Time" }
  ],

I want to make the text field change to A, B, C, D, etc. There's anyway to make this happen?

Upvotes: 3

Views: 13315

Answers (1)

Steve
Steve

Reputation: 20469

You can return headers from a method, that takes the text as a parameter, for example you could use the current index in a loop:

<v-layout>
  <v-flex v-for="i in 3" xs4>
    <v-data-table
    :headers="getHeaders(i)"
    :items="desserts"
    class="elevation-1"
    >
      <template v-slot:items="props">
        <td>{{ props.item.name }}</td>
        <td class="text-xs-right">{{ props.item.calories }}</td>
        <td class="text-xs-right">{{ props.item.fat }}</td>

      </template>
    </v-data-table>
  </v-flex>
</v-layout>

methods:{
  getHeaders(headingText){
    return [
    {
      text: 'Dynamic heading no. ' +headingText,
      align: 'left',
      sortable: false,
      value: 'name'
    },
    { text: 'Calories', value: 'calories' },
    { text: 'Fat (g)', value: 'fat' }
  ];
}

}

live example: https://codepen.io/sf-steve/pen/pYgOze

Upvotes: 6

Related Questions