Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to highlight rows in v-data-table on click in Vuetify (version >= 2.0)?

I use a v-data-table to manage emails. User can click on a row and a popup appears with the email details.

What I want to have:
I want to have rows marked as "readed" (so css bold/not-bold) after these rows were clicked.

Problem:
I already found some examples here (but only for older Vuetify versions): Vuetify - How to highlight row on click in v-data-table

This example (and all other examples I've found) use extended code for v-data-table - like:

<v-data-table :headers="headers" :items="desserts" class="elevation-1">
  <template v-slot:items="props">
    <tr @click="activerow(props.item)" :class="{'primary': props.item.id===selectedId}">
      <td>{{ props.item.name }}</td>
      <td class="text-xs-right">{{ props.item.calories }}</td>
      <td class="text-xs-right">{{ props.item.fat }}</td>
      <td class="text-xs-right">{{ props.item.carbs }}</td>
      <td class="text-xs-right">{{ props.item.protein }}</td>
      <td class="text-xs-right">{{ props.item.iron }}</td>
    </tr>
  </template>
</v-data-table>

So extended code is:

<template v-slot:items="props">
  <tr @click="activerow(props.item)" :class="{'primary': props.item.id===selectedId}">
    <td>{{ props.item.name }}</td>
    <td class="text-xs-right">{{ props.item.calories }}</td>
    <td class="text-xs-right">{{ props.item.fat }}</td>
    <td class="text-xs-right">{{ props.item.carbs }}</td>
    <td class="text-xs-right">{{ props.item.protein }}</td>
    <td class="text-xs-right">{{ props.item.iron }}</td>
  </tr>
</template>

However since I use Vutify version 2 the

<template slot="headers" slot-scope="props"> and <template slot="items" slot-scope="props"> inside <v-data-table> seem to be ignored.

Vuetify 2 provides some new slots, but I have not yet find out how to use them for this example.

Can anyone provide an example for Vuetify >= 2.0? Believe me, there is no one for higher versions available yet - not on any development environment like CodePen or JSFiddle etc.

like image 275
Varathron Avatar asked Aug 14 '19 07:08

Varathron


People also ask

How do I highlight a row in a V data table?

In the above example click on a row will be highlighted by adding class called primary. If it is a static data it is working fine, but if it is dynamic data like getting data from API, the data in the data table will always be refreshed, if i change the pagination and sort and all.

What is V data table?

# Data tables The v-data-table component is used for displaying tabular data. Features include sorting, searching, pagination, content-editing, and row selection.

What is Vuetify used for?

What is Vuetify? Vuetify is a complete UI framework built on top of Vue.js. The goal of the project is to provide developers with the tools they need to build rich and engaging user experiences.


1 Answers

For me it workes with Vuetify 2.X by replacing the table body (v-slot:body) and define the tr and td manually. It helps to study the Slot Example.

This way it is possible to add the click event and set the css class like this:

<template v-slot:body="{ items }">
    <tbody>
      <tr v-for="item in items" :key="item.name" @click="selectItem(item)" :class="{'selectedRow': item === selectedItem}">
        <td>{{ item.name }}</td>
        <td>{{ item.age }}</td>
      </tr>
    </tbody>
</template>

(Side note: This way you can not use the event click:row on the v-data-table anymore. Because it does not get fired by overwriting the row... But defining @click directly on tr works nicely too.)

In method selectItem we save the item in a data field selectedItem.

data () {
  return {
    selectedItem: null,
    ...
  }
},
methods: {
    selectItem (item) {
      console.log('Item selected: ' + item.name)
      this.selectedItem = item
    }
}

CSS class we set when the row is clicked. So background changes and text gets bold:

<style scoped>
.selectedRow {
    background-color: red;
    font-weight: bold;
}
</style>
like image 85
Nick Avatar answered Oct 15 '22 23:10

Nick