vue-draggable-plus

repository·main·Indexed 26 days ago

https://github.com/alfred-skyblue/vue-draggable-plus

A 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.

Tokens
21.3K
Snippets
29
Records
77
Agent score
85%

What's inside vue-draggable-plus

  1. Use the `target` option with the functional API

    main

    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>
  2. Implement custom node cloning

    main

    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.

    Requirements for Cloning

    To ensure cloning works correctly, you must satisfy these two conditions:

    1. The pull property within the group configuration of the item being cloned must be set to 'clone'.
    2. The name property within the group configuration must be identical for both the source item and the target container.

    Important Note on Keys

    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.

  3. Enable Drag Clone functionality

    main

    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:

    1. Group Pull Setting: The pull property within the group attribute of the component must be set to 'clone'.
    2. Group Name Consistency: The name property within the group attribute must be identical across both the source and target lists.
    3. Unique Keys: When using the clone attribute, you must regenerate a unique key for the cloned item to prevent abnormal component rendering.
  4. Implement transition animations with vue-draggable-plus

    main

    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.

    Component Usage

    Wrap your list items within a <TransitionGroup> component to enable smooth entry/exit and movement animations when the list order changes.

    Function Usage

    When using the programmatic API (functions), ensure the underlying list being manipulated is wrapped in a transition component to reflect changes visually.

    Directive Usage

    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.