Install vue-draggable-plus via npm
mainInstall the package using npm to add drag-and-drop sorting capabilities to your Vue project (supports Vue >= v3 or Vue >= 2.7).
npm install vue-draggable-plusrepository·main·Indexed 26 days ago
https://github.com/alfred-skyblue/vue-draggable-plusA universal drag-and-drop sorting module for Vue (v2.7+ and v3+) based on Sortablejs. It provides multiple implementation methods including a VueDraggable component, a composable useDraggable function, and a directive. The library supports two-way binding via v-model, customizable dragging behavior, group configurations for inter-list movement, and a comprehensive set of event hooks such as onStart, onEnd, and onMove.
Install the package using npm to add drag-and-drop sorting capabilities to your Vue project (supports Vue >= v3 or Vue >= 2.7).
npm install vue-draggable-plus<tbody> element as the target-container. This allows users to reorder rows via drag-and-drop. There are three ways to achieve this: using the component wrapper, using the functional API, or using the directive.<thead> element as the target container. This allows users to reorder columns by dragging the header cells.vue-draggable-plus via three different methods: Component usage, Function usage, or Directive usage. All methods support two-way data binding to synchronize the state of the lists during drag-and-drop operations.target attribute. Ensure the selector correctly identifies the intended container element.When using the functional API (e.g., useDraggable), you can pass a target selector to specify the container. To ensure accuracy, you can use an ID selector (e.g., #my-container) for the target element.
Important Requirement: When using the functional API, your component must have exactly one root element. If the component has multiple root elements, the functional logic may fail to correctly identify or manage the target container.
<!-- Correct usage with a single root element -->
<template>
<div>
<div id="my-container">
<ElTable :list="list" />
</div>
<div class="flex justify-between">
<pre class="code-block">{{ text }}</pre>
</div>
</div>
</template>
<!-- Incorrect usage with multiple root elements -->
<template>
<div id="my-container">
<ElTable :list="list" />
</div>
<div class="flex justify-between">
<pre class="code-block">{{ text }}</pre>
</div>
</template>Custom cloning can be implemented across the three primary usage patterns of vue-draggable-plus:
clone function as a prop to the draggable component.clone option when using the programmatic API.clone option within the directive configuration.animation property to handle the transition effect when items are being reordered, and the transition property to handle the effect when items are being restored to their original positions.You can customize how nodes are cloned by passing a function to the clone property. This is particularly useful in low-code scenarios, such as dragging form elements onto a canvas where a new component instance must be created.
By default, the library uses JSON.parse(JSON.stringify()) for cloning, but you can provide your own logic using libraries like lodash or other deep-cloning utilities.
To ensure cloning works correctly, you must satisfy these two conditions:
pull property within the group configuration of the item being cloned must be set to 'clone'.name property within the group configuration must be identical for both the source item and the target container.When using a custom clone function, you must generate a new unique key for the cloned item within the function. Failing to do so will result in component rendering errors.
handle option. This function allows for dynamic logic to determine if an element should act as a drag handle.To enable dragging an item from one list to another while keeping the original item in its source list, use the clone attribute.
By default, the library uses JSON.parse(JSON.stringify()) to perform the clone. If you require a custom cloning logic, pass a custom function to the clone attribute.
Critical Requirements for Cloning:
pull property within the group attribute of the component must be set to 'clone'.name property within the group attribute must be identical across both the source and target lists.clone attribute, you must regenerate a unique key for the cloned item to prevent abnormal component rendering.You can implement transition animations during drag-and-drop operations by wrapping your draggable elements or containers with Vue's <TransitionGroup> component. This works across all three usage modes: Component, Function, and Directive.
Wrap your list items within a <TransitionGroup> component to enable smooth entry/exit and movement animations when the list order changes.
When using the programmatic API (functions), ensure the underlying list being manipulated is wrapped in a transition component to reflect changes visually.
When using the v-draggable directive, the elements being moved should be children of a <TransitionGroup> to support transition effects during the drag-and-drop process.