Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

styling individual rows in a Vuetify data table

Can I apply different styling for a specific row in a data table?

This is my code:

<v-data-table
          :headers="headers"
          :items="items"
          v-model="selected"
          :search="search"
          :no-data-text="mensagem"
          select-all
          :rows-per-page-text="linhasPorPagina">
          <template slot="items" slot-scope="props">
            <td>
              <v-checkbox
                primary
                hide-details
                v-model="props.selected"
              ></v-checkbox>
            </td>
            <td class="text-xs-left">{{ props.item.id }}</td>
            <td class="text-xs-left">{{ props.item.apresentante }}</td>    
        </v-data-table>

For example, I want to apply a red line when the Id > 4

like image 452
Fabio Ebner Avatar asked May 02 '18 13:05

Fabio Ebner


People also ask

How to use tutorialdataservice with vuetify?

– These Components call TutorialDataService methods which use axios to make HTTP requests and receive responses. You will see some options, choose default (babel, eslint). After the Vue project is created successfully, we import Vuetify with command: vue add vuetify.

How to get the class name of an item in vuetify?

If you're using newer versions of vuetify, you have access to item-class as a property of the v-data-table. This will provide the item as the first argument to the callback function. <v-data-table.... :item-class="itemRowBackground" ></v-data-table> And then define the function which will return the class name:

How to import vuetify into Vue project?

You will see some options, choose default (babel, eslint). After the Vue project is created successfully, we import Vuetify with command: vue add vuetify.

How do I Group rows in a V-data-table?

The v-data-table renders a default footer using the v-data-footer component. You can pass props to this component using footer-props. Using the group-by and group-desc props you can group rows on an item property. The show-group-by prop will show a group button in the default header.


3 Answers

You can now use vuetifys data table item-class property:

new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  methods: {
    row_classes(item) {
        if (item.calories < 200) {
          return "orange"; //can also return multiple classes e.g ["orange","disabled"]
        } 
    }
  },
  data () {
    return {
      singleSelect: false,
      selected: [],
      headers: [{text: 'Dessert (100g serving)', value: 'name'},
                { text: 'Calories', value: 'calories' },
      ],
      desserts: [{name: 'Frozen Yogurt',calories: 159,},
                 {name: 'Ice cream sandwich',calories: 237,},
                 {name: 'Eclair',calories: 262,},
                 {name: 'Cupcake',calories: 305,},
      ],
    }
  },
})
.orange {
  background-color: orange;
}
<link rel='stylesheet' href='https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.css'>

<div id="app">
  <v-app>
    <v-data-table
      v-model="selected"
      :headers="headers"
      :items="desserts"
      :item-class= "row_classes"                  
      class="elevation-1"
    >
    </v-data-table>
  </v-app>
</div>

<script src='https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js'></script>
<script src='https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.js'></script>
like image 123
Soth Avatar answered Sep 30 '22 17:09

Soth


You can actually wrap your <td> elements within a <tr> element. Then you can use Vue style binding to determine whether you want classes applied or not.

<template>
  <tr v-bind:class="{'active-class-name': isActive(item)}">
    <td>Woo</td>
  </tr>
</template>

It renders out the tbody block with a row (tr) with applied class names and the child columns contained within.

like image 26
mellisdesigns Avatar answered Sep 30 '22 18:09

mellisdesigns


I was also trying to style rows in a vuetify data-table but the above answers didn't have everything I needed to make it work. Using Vuetify v2

I wanted to add a class to a row when a certain condition was met.

<v-data-table  
   :headers="myHeaders"
   :items="myItems"
>
  <template v-slot:item="props">
    <tr :class="{'my-class': props.item.current > props.item.total}">
      <td>{{props.item.current}}</td>
      <td>{{props.item.total}}</td>
    </tr>
  </template>
</v-data-table>

...
// js portion of the component (computed prop)

myItems() {
    return [
      {
        current: 101,
        total: 100
      },
      {
        current: 45,
        total: 100
      }
    ]
  }
like image 37
Ju66ernaut Avatar answered Sep 30 '22 18:09

Ju66ernaut