Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vuetify v-data-table , how to render header text in HTML?

Vuetify documentation suggests to pass an object array for headers that has a text attribute like so:

[{
    text: string;
    value: string;
    align: 'left' | 'center' | 'right';
    sortable: boolean;
    class: string[] | string;
    width: string;
}]

But if you pass:

[{
    text: string = "<div>Foo</div><div>Bar</div>;
    value: string;
    align: 'left' | 'center' | 'right';
    sortable: boolean;
    class: string[] | string;
    width: string;
}]

It won't render the HTML (it'll escape the text).

So, how do I render HTML?

like image 789
Artur Grigio Avatar asked Jun 04 '18 21:06

Artur Grigio


4 Answers

i found a solution for your problem:

 <template v-slot:item.description="{item}">
   <div v-html="item.description"></div>
 </template>

Just replace description with your attribute in your object :

Object:

And here the image of the object data-table

like image 166
gilou.31 Avatar answered Nov 12 '22 00:11

gilou.31


Something to note if you're on an updated version is that

<template slot="headers" slot-scope="props">
<th><div>Foo</div><div>Bar</div></th>

is now

<template slot="header" slot-scope="props">
<th><div>Foo</div><div>Bar</div></th>

The version i'm testing in is v2.2.8 but it was probably changed in v2

like image 30
Frosty Avatar answered Oct 11 '22 14:10

Frosty


Have a look at the Vuetify example of the header slot. It has the means to complete your task.

Below here there is a copy from the exact part, just replace the {{ header.text }} usage with Vue's solution using raw HTML to force HTML string rendering. It will look like something like this <span v-html="header.text"></span>.

<template slot="headers" slot-scope="props">
  <tr>
    <th>
      <v-checkbox
        :input-value="props.all"
        :indeterminate="props.indeterminate"
        primary
        hide-details
        @click.native="toggleAll"
      ></v-checkbox>
    </th>
    <th
      v-for="header in props.headers"
      :key="header.text"
      :class="['column sortable', pagination.descending ? 'desc' : 'asc', header.value === pagination.sortBy ? 'active' : '']"
      @click="changeSort(header.value)"
    >
      <v-icon small>arrow_upward</v-icon>
      {{ header.text }}
    </th>
  </tr>
</template>
like image 12
Bernardo Duarte Avatar answered Nov 12 '22 00:11

Bernardo Duarte


You would need to use the headers template slot instead of passing them as a prop:

  <template slot="headers" slot-scope="props">
    <th><div>Foo</div><div>Bar</div></th>
  </template>
like image 2
Jpod Avatar answered Nov 11 '22 22:11

Jpod