Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I reorder the items in a Vuetify data table by dragging the rows?

I'm working on a Vuetify web app for a client and she wants to be able to adjust the order of elements being displayed in a data table by dragging and dropping the rows, but the Vuetify documentation doesn't explain how to do that; how can I do it?

like image 237
Nathan Wailes Avatar asked Oct 09 '20 01:10

Nathan Wailes


People also ask

Can vuetify handle expanded slots in vdatatable?

Because vDataTable is a tabular structure, I'm not sure the expanded slot could easily be handled the way you'd like in Vuetify. In addition to the other changes needed to detect dropping an item between another item and its expand slot, note the :key added to the expand slot content. It's also needed.

Does vuetify support drag-n-drop?

Today we are going to talk about integrating drag-n-drop functionality for our Vuetify project. There are some use cases we are going to talk a little bit in deep because in our Vuetify Discord Community many user asked for using drag and drop for data tables. This will be a two part article for now and maybe I can add more if needed.

What is sortable in Vue?

It is a Vue component allowing drag-and-drop sorting in sync with View-Model and is a wrapper around popular Sortable.js library. This post will be about different use cases with Vuetify. For integration and getting started see the docs of the library. It will explain better than me.

What is draggable in Vue JS?

There is nothing technical to tell but we will go through examples one by one and at last see how to integrate it with data tables (v-data-table for vuetify users). We are going to use a wonderful library called Vue. Draggable. It is a Vue component allowing drag-and-drop sorting in sync with View-Model and is a wrapper around popular Sortable.


Video Answer


1 Answers

Here's a CodePen I got working: https://codepen.io/NathanWailes/pen/rNLajYO

It uses:

  • Vue 2.x
  • Vuetify 2.3.13
  • Sortable 1.10.2 (so you'll need to install/import this if you don't already have it)

It's based on the answers in this GitHub issue.

Here's the code:

<div id="app">
  <v-app>
    <v-data-table
      :headers="headers"
      :items="desserts"
      v-sortable-data-table
      @sorted="saveOrder"
      item-key="name"
    ></v-data-table>
  </v-app>
</div>

new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  data () {
    return {
      headers: [
        {
          text: 'Dessert',
          align: 'start',
          sortable: false,
          value: 'name',
        },
      ],
      desserts: [
        {
          name: 'Frozen Yogurt',
        },
        {
          name: 'Ice cream sandwich',
        },
        {
          name: 'Eclair',
        },
        {
          name: 'Cupcake',
        },
        {
          name: 'Gingerbread',
        },
      ],
    }
  },
  methods: {
    saveOrder (event) {
      const movedItem = this.desserts.splice(event.oldIndex, 1)[0];
      this.desserts.splice(event.newIndex, 0, movedItem);
    }
  },
  directives: {
    sortableDataTable: {
      bind (el, binding, vnode) {
        const options = {
          animation: 150,
          onUpdate: function (event) {
            vnode.child.$emit('sorted', event)
          }
        }
        Sortable.create(el.getElementsByTagName('tbody')[0], options)
      }
    }
  },
})
like image 168
Nathan Wailes Avatar answered Oct 27 '22 17:10

Nathan Wailes