Project Icon

vue-draggable-plus

Vue3拖拽排序组件 灵活高效的列表管理工具

vue-draggable-plus是一款专为Vue3设计的拖拽排序组件,基于Sortablejs开发。该组件提供组件、函数和指令三种使用方式,支持自定义容器和动画效果,并提供丰富的事件回调。它解决了复杂组件库中实现拖拽列表的难题,适用性广泛。组件继承了Sortablejs的全部配置项,并针对Vue3进行了优化,为开发者提供了灵活高效的列表管理工具。

NPM version NPM Downloads Docs & Demos
GitHub stars

vue-draggable-plus

中文文档

Drag and drop sorting module, support Vue>=v3 or Vue>=2.7

Example of use

Describe

Since the vue3 component of Sortablejs has not been updated, it has been seriously out of touch with vue3, so this project was born. This component is based on Sortablejs, so if you want to know more about Sortablejs, you can check it out Sortablejs official website

We have encapsulated a variety of usages for this, you can use components, function, or instructions, there is always one that suits you

Solve pain points

In Sortablejs official Vue components in the past, the drag-and-drop list is implemented by using the component as a direct child element of the list. When we use some component libraries, if there is no slot for the root element of the list in the component library , it is difficult for us to implement a drag list, vue-draggable-plus perfectly solves this problem, it allows you to use a drag list on any element, we can use the selector of the specified element to get the root element of the list, and then Use the root element of the list as container of Sortablejs, for details, refer to specify target container.

Install


npm install vue-draggable-plus

Usage

Component usage

<template>
    <VueDraggable ref="el" v-model="list">
      <div v-for="item in list" :key="item.id">
        {{ item.name }}
      </div>
    </VueDraggable>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { VueDraggable } from 'vue-draggable-plus'

const list = ref([
  {
    name: 'Joao',
    id: 1
  },
  {
    name: 'Jean',
    id: 2
  },
  {
    name: 'Johanna',
    id: 3
  },
  {
    name: 'Juan',
    id: 4
  }
])
</script>

Function Usage

<template>
  <div ref="el">
    <div v-for="item in list" :key="item.id">
      {{ item.name }}
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useDraggable } from 'vue-draggable-plus'

const el = ref<HTMLElement | null>(null)
const list = ref([
  {
    name: 'Joao',
    id: 1
  },
  {
    name: 'Jean',
    id: 2
  },
  {
    name: 'Johanna',
    id: 3
  },
  {
    name: 'Juan',
    id: 4
  }
])
// The return value is an object, which contains some methods, such as start, destroy, pause, etc.
const draggable = useDraggable(el, list, {
  animation: 150,
  onStart() {
    console.log('start')
  },
  onUpdate() {
    console.log('update')
  }
})
</script>

Directive Usage

<template>
  <div
    v-draggable="[
        list,
        {
          animation: 150,
        }
      ]"
  >
    <div v-for="item in list" :key="item.id">
      {{ item.name }}
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { vDraggable } from 'vue-draggable-plus'
const list = ref([
  {
    name: 'Joao',
    id: 1
  },
  {
    name: 'Jean',
    id: 2
  },
  {
    name: 'Johanna',
    id: 3
  },
  {
    name: 'Juan',
    id: 4
  }
])

function onStart() {
  console.log('start')
}

function onUpdate() {
  console.log('update')
}
</script>

Explanation

All event functions starting with on can be passed to components using v-on. For example:


<template>
  <VueDraggable v-model="list" @start="onStart" @end="onEnd"></VueDraggable>
</template>
<script lang="ts" setup>
import { ref } from "vue";
import { VueDraggable } from 'vue-draggable-plus'
import { SortableEvent } from "sortablejs";

const list = ref([
  {
    name: 'Joao',
    id: '1'
  },
  {
    name: 'Jean',
    id: '2'
  },
  {
    name: 'Johanna',
    id: '3'
  },
  {
    name: 'Juan',
    id: '4'
  }
])

function onStart(event: SortableEvent) {
  console.log('start drag')
}

function onEnd(event: SortableEvent) {
  console.log('end drag')
}
</script>

For information on using Hooks and directives, please refer to the documentation.

Options

Options inherits all configuration items from Sortablejs. For details, please see the Sortablejs official documentation.

Types

type Easing =
  | 'steps(int, start | end)'
  | 'cubic-bezier(n, n, n, n)'
  | 'linear'
  | 'ease'
  | 'ease-in'
  | 'ease-out'
  | 'ease-in-out'
  | 'step-start'
  | 'step-end'
  | 'initial'
  | 'inherit'

type PullResult = ReadonlyArray<string> | boolean | 'clone';
type PutResult = ReadonlyArray<string> | boolean;

interface GroupOptions {
  /**
   * Group name.
   */
  name: string;
  /**
   * The ability to move from the list. Clone - copy the item instead of moving it.
   */
  pull?: PullResult | ((to: Sortable, from: Sortable, dragEl: HTMLElement, event: SortableEvent) => PullResult) | undefined;
  /**
   * Whether elements can be added from other lists, or an array of group names from which elements can be obtained.
   */
  put?: PutResult | ((to: Sortable, from: Sortable, dragEl: HTMLElement, event: SortableEvent) => PutResult) | undefined;
  /**
   * After moving to another list, the cloned element is restored to its initial position.
   */
  revertClone?: boolean | undefined;
}

type Group = string | GroupOptions | undefined;

type ScrollFn = ((
        this: Sortable,
        offsetX: number,
        offsetY: number,
        originalEvent: Event,
        touchEvt: TouchEvent,
        hoverTargetEl: HTMLElement,
    ) => 'continue' | void) | undefined;

API

ParameterDescriptionTypeDefault
animationShow animation while draggingNumber0
chosenClassCSS class name for chosen itemString'sortable-chosen'
delayDelay in milliseconds before drag startsNumber0
delayOnTouchOnlyDelay on touch eventNumber0
directionDragging direction, 'vertical' or 'horizontal' (default auto detect)String-
disabledDisable draggingBooleanfalse
dragClassCSS class name for dragged itemString'sortable-drag'
draggableSelector for draggable items within elementString-
emptyInsertThresholdDistance (in pixels) from empty sortable items where dragging element should be inserted. Set to 0 to disable this feature.Number5
easingAnimation easingEasing-
fallbackClassCSS class name for cloned DOM elements when using forceFallbackStringsortable-fallback
fallbackOnBodyAppend cloned DOM element to body elementBooleanfalse
fallbackTolerancePixels mouse must move before drag start when using forceFallbackNumber0
filterSelector for items that should not be draggableString-
forceFallbackIgnore HTML5 drag and drop behavior and force fallbackBooleanfalse
ghostClassCSS class name for drop placeholderString'sortable-ghost'
groupGroup items to drag between sortable lists. Both lists must have the same group value. Also define whether lists can be dragged out of, cloned, or receive elements from other lists. See TypeScript type definition above for details.Group-
handleSelector for handle to initiate drag. If not set, the target element's children are usedString-
invertSwapAlways use inverted swap zone if set to trueBooleanfalse
invertedSwapThresholdInverted swap zone threshold, defaults to swapThreshold valueNumber-
preventOnFilterCall event.preventDefault() on filter eventBooleantrue
removeCloneOnHideRemove instead of hiding cloned element when not displayedBooleantrue
sortAllow list items to be sorted within containerBooleantrue
swapThresholdSwap zone thresholdNumber1
touchStartThresholdPixels before cancelling delay touch eventNumber1
setDataPass a function where the first argument is of type DataTransfer and the second argument is of type HTMLElementFunction-
scrollEnable scrollingBooleanHTMLElement
scrollFnCustom scroll functionScrollFn-
scrollSensitivityThe distance in pixels the mouse must be to the edge to start scrollingNumber-
scrollSpeedThe scrolling speed in ms/pxnumber-
bubbleScrollEnables automatic scrolling for all parent elements to make it easier to move itemsBooleantrue
onChooseTriggered when an item is selected((event: SortableEvent) => void)-
onUnchooseTriggered when an item is deselected((event: SortableEvent) => void)-
onStartTriggered when an item is picked up for drag and drop((event: SortableEvent) => void)-
onEndTriggered when an item is no longer being dragged((event: SortableEvent) => void)-
onAddTriggered when an item is moved from one list to another((event: SortableEvent) => void)-
onUpdateTriggered when the order of the items is updated((event: SortableEvent) => void)-
onSortTriggered whenever any changes are made to the list((event: SortableEvent) => void)-
onRemoveTriggered when an item is removed from the list and moved to another((event: SortableEvent) => void)-
onFilterTriggered when trying to drag a filtered item((event: SortableEvent) => void)-
onMoveTriggered while an item is being dragged((event: MoveEvent,originalEvent: Event) => void)-
onCloneTriggered when an item is cloned((event: SortableEvent) => void)-
onChangeTriggered when an item is dragged and changes position((event: SortableEvent) => void)-
项目侧边栏1项目侧边栏2
推荐项目
Project Cover

豆包MarsCode

豆包 MarsCode 是一款革命性的编程助手,通过AI技术提供代码补全、单测生成、代码解释和智能问答等功能,支持100+编程语言,与主流编辑器无缝集成,显著提升开发效率和代码质量。

Project Cover

AI写歌

Suno AI是一个革命性的AI音乐创作平台,能在短短30秒内帮助用户创作出一首完整的歌曲。无论是寻找创作灵感还是需要快速制作音乐,Suno AI都是音乐爱好者和专业人士的理想选择。

Project Cover

有言AI

有言平台提供一站式AIGC视频创作解决方案,通过智能技术简化视频制作流程。无论是企业宣传还是个人分享,有言都能帮助用户快速、轻松地制作出专业级别的视频内容。

Project Cover

Kimi

Kimi AI助手提供多语言对话支持,能够阅读和理解用户上传的文件内容,解析网页信息,并结合搜索结果为用户提供详尽的答案。无论是日常咨询还是专业问题,Kimi都能以友好、专业的方式提供帮助。

Project Cover

阿里绘蛙

绘蛙是阿里巴巴集团推出的革命性AI电商营销平台。利用尖端人工智能技术,为商家提供一键生成商品图和营销文案的服务,显著提升内容创作效率和营销效果。适用于淘宝、天猫等电商平台,让商品第一时间被种草。

Project Cover

吐司

探索Tensor.Art平台的独特AI模型,免费访问各种图像生成与AI训练工具,从Stable Diffusion等基础模型开始,轻松实现创新图像生成。体验前沿的AI技术,推动个人和企业的创新发展。

Project Cover

SubCat字幕猫

SubCat字幕猫APP是一款创新的视频播放器,它将改变您观看视频的方式!SubCat结合了先进的人工智能技术,为您提供即时视频字幕翻译,无论是本地视频还是网络流媒体,让您轻松享受各种语言的内容。

Project Cover

美间AI

美间AI创意设计平台,利用前沿AI技术,为设计师和营销人员提供一站式设计解决方案。从智能海报到3D效果图,再到文案生成,美间让创意设计更简单、更高效。

Project Cover

稿定AI

稿定设计 是一个多功能的在线设计和创意平台,提供广泛的设计工具和资源,以满足不同用户的需求。从专业的图形设计师到普通用户,无论是进行图片处理、智能抠图、H5页面制作还是视频剪辑,稿定设计都能提供简单、高效的解决方案。该平台以其用户友好的界面和强大的功能集合,帮助用户轻松实现创意设计。

投诉举报邮箱: service@vectorlightyear.com
@2024 懂AI·鲁ICP备2024100362号-6·鲁公网安备37021002001498号