Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vuetify v-data-table not responsive when using v-slot:body

Vuetify v-data-table not stacking on mobile devices when using v-slot:body

How can I get the data table to stack if it implements the body v-slot? As can be seen in the Vuetify documentation this is how a normal v-data-table behaves:

Stacked table

And this is how it behaves when using body slot:

Not stacked table

Thank you.

like image 738
lukas Avatar asked Sep 25 '19 11:09

lukas


1 Answers

The default body implementation of v-data-table has two components for the table rows - Row and MobileRow. When the page's width is under 600px, MobileRow is used.

By using the body slot, the default implementation is discarded, along with the mobile logic, so you must supply your own. Fortunately, there's a handy helper class we can use to easily switch styling depending on the current page size.

Here's a codepen with a sketch up of the solution. The relevant part:

  <template v-slot:body="props">
    <tbody>
      <tr v-for="item in props.items">
        <td class="d-block d-sm-table-cell" v-for="field in Object.keys(item)">
          {{item[field]}}
        </td>
      </tr>
    </tbody>
  </template>

For more precise styling, you might want to use the visibility helper classes and have two separate implementations for the table rows - just like the default implementation.

like image 89
srdecny Avatar answered Oct 13 '22 12:10

srdecny