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?
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.
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.
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.
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.
Here's a CodePen I got working: https://codepen.io/NathanWailes/pen/rNLajYO
It uses:
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)
}
}
},
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With