Vue Draggable

2.0.6 · active · verified Sun Apr 19

Vue Draggable is a lightweight, dependency-free drag and drop library specifically designed for Vue.js applications. It leverages native HTML5 drag and drop APIs, making it a performant choice for reordering lists or moving items between zones. The current stable version is 2.0.6, and it primarily targets Vue 2.x environments. Its key differentiators include its complete lack of external dependencies, offering a lean footprint, and providing flexible integration options as a global plugin, a local directive, or an optional renderless component (`VueDraggableGroup`) for advanced reactivity management. While it does not specify a strict release cadence, updates are generally aligned with maintaining compatibility and addressing issues within the Vue 2 ecosystem.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates global plugin setup, basic usage of the `v-drag-and-drop` directive with options, and handling custom dropzone events in a Vue 2 application.

import Vue from 'vue';
import VueDraggable from 'vue-draggable';

Vue.use(VueDraggable);

new Vue({
  el: '#app',
  template: `
    <div id="app">
      <h3>My Drag & Drop Lists</h3>
      <div v-drag-and-drop:options="options">
        <ul @added="onAdded" @removed="onRemoved" @reordered="onReordered">
          <li v-for="item in items1" :key="item.id" :data-id="item.id">{{ item.name }}</li>
        </ul>
        <ul @added="onAdded" @removed="onRemoved" @reordered="onReordered">
          <li v-for="item in items2" :key="item.id" :data-id="item.id">{{ item.name }}</li>
        </ul>
      </div>
    </div>
  `,
  data() {
    return {
      items1: [
        { id: 1, name: 'Task A' },
        { id: 2, name: 'Task B' }
      ],
      items2: [
        { id: 3, name: 'Task X' },
        { id: 4, name: 'Task Y' }
      ],
      options: {
        dropzoneSelector: 'ul',
        draggableSelector: 'li',
        multipleDropzonesItemsDraggingEnabled: true,
        onDrop: (event) => {
          console.log('Item dropped:', event.items.map(i => i.dataset.id));
          // For simple scenarios, manual data manipulation would go here.
          // For reactive data, VueDraggableGroup is recommended.
        }
      }
    };
  },
  methods: {
    onAdded({ ids, index, dropzone }) {
      console.log('Item added:', ids, 'at index:', index, 'in dropzone:', dropzone);
      // Update your data model based on these events
    },
    onRemoved({ ids, index, dropzone }) {
      console.log('Item removed:', ids, 'from index:', index, 'in dropzone:', dropzone);
    },
    onReordered({ ids, index, dropzone }) {
      console.log('Item reordered:', ids, 'at index:', index, 'in dropzone:', dropzone);
    }
  }
});

view raw JSON →