Vue3拖拽插件(vuedraggable@next)

简介: vuedraggable 是一款 Vue2 拖拽插件,可轻松实现列表项的拖拽排序与交互。通过简单配置,即可在不同区域间拖动元素并实现数据同步。支持多种事件监听和自定义样式。

参考文档
官方网站

效果如下图:vuedraggable@4.1.0 在线预览

在这里插入图片描述

安装插件

pnpm add vuedraggable@next

引入并使用

<script lang="ts" setup>
import { ref, watchEffect } from 'vue'
import Draggable from 'vuedraggable'

const players = ref([
  { name: 'curry', id: 1 },
  { name: 'klay', id: 2 },
  { name: 'green', id: 3 },
  { name: 'kobe', id: 4 },
  { name: 'james', id: 5 },
  { name: 'jordan', id: 6 },
  { name: 'dear', id: 7 },
  { name: 'muse', id: 8 }
])
const roles = ref([
  { name: '李白', id: 1 },
  { name: '韩信', id: 2 },
  { name: '公孙离', id: 3 },
  { name: '李元芳', id: 4 },
  { name: '关羽', id: 5 },
  { name: '诸葛亮', id: 6 },
  { name: '澜', id: 7 },
  { name: '吕布', id: 8 }
])

watchEffect(() => {
  console.log('players:', players.value)
})
watchEffect(() => {
  console.log('roles:', roles.value)
})
function onStart (e: any) { // 开始拖动时触发的事件
  console.log('开始拖拽 start:', e)
  console.log('拖拽操作前的索引oldIndex:', e.oldIndex)
  console.log('拖拽完成后的索引newIndex:', e.newIndex)
}
function onEnd (e: any) { // 拖动完成时触发的事件
  console.log('结束拖拽 end:', e)
  console.log('拖拽操作前的索引oldIndex:', e.oldIndex)
  console.log('拖拽完成后的索引newIndex:', e.newIndex)
}
function onMove (evt: any, originalEvent: DragEvent) { // 拖拽move事件回调
  console.log('move:', evt)
  console.log('originalEvent:', originalEvent)
  // 不允许拖拽
  return evt.draggedContext.element.id !== 7 // false表示阻止,true表示不阻止
}
</script>
<template>
  <div>
    <h2 class="mb10">设置相同的 group,即可实现两个拖拽区域按钮拖动交换</h2>
    <a-row :gutter="32">
      <a-col :span="12">
        <a-card title="players (sort: false,不允许内部拖动排序,但可以拖动元素到外部 roles)">
          <!-- 参考文档:https://github.com/SortableJS/vue.draggable.next
          https://www.itxst.com/vue-draggable-next/tutorial.html -->
          <Draggable
            animation="300"
            v-model="players"
            group="human"
            :sort="false"
            item-key="id"
            @start="onStart"
            @end="onEnd"
            :move="onMove">
            <template #item="{ element }">
              <Button class="mr12 mb12">{
  { element.name }} {
  { element.id }}</Button>
            </template>
            <template #header>
              <Button class="mr12" type="primary">header</Button>
            </template>
            <template #footer>
              <Button type="primary">footer</Button>
            </template>
          </Draggable>
        </a-card>
      </a-col>
      <a-col :span="12">
        <a-card title="roles (id 值为偶数不可拖动,奇数可拖动)">
          <Draggable
            animation="300"
            v-model="roles"
            group="human"
            filter=".unmover"
            draggable=".mover"
            item-key="id"
            @start="onStart"
            @end="onEnd"
            :move="onMove">
            <template #item="{ element }">
              <Button class="mr12 mb12" :class="[element.id % 2 === 0 ? 'unmover' : 'mover']">{
  { element.name }} {
  { element.id }}</Button>
            </template>
            <template #header>
              <Button class="mr12" type="primary">header</Button>
            </template>
            <template #footer>
              <Button type="primary">footer</Button>
            </template>
          </Draggable>
        </a-card>
      </a-col>
    </a-row>
  </div>
</template>
<style lang="less" scoped>
.mr12 {
  margin-right: 12px;
}
.mb12 {
  margin-bottom: 12px;
}
</style>
相关文章
|
4月前
|
JavaScript 前端开发 安全
Vue 3
Vue 3以组合式API、Proxy响应式系统和全面TypeScript支持,重构前端开发范式。性能优化与生态协同并进,兼顾易用性与工程化,引领Web开发迈向高效、可维护的新纪元。(238字)
724 139
|
9月前
|
缓存 JavaScript PHP
斩获开发者口碑!SnowAdmin:基于 Vue3 的高颜值后台管理系统,3 步极速上手!
SnowAdmin 是一款基于 Vue3/TypeScript/Arco Design 的开源后台管理框架,以“清新优雅、开箱即用”为核心设计理念。提供角色权限精细化管理、多主题与暗黑模式切换、动态路由与页面缓存等功能,支持代码规范自动化校验及丰富组件库。通过模块化设计与前沿技术栈(Vite5/Pinia),显著提升开发效率,适合团队协作与长期维护。项目地址:[GitHub](https://github.com/WANG-Fan0912/SnowAdmin)。
1160 5
|
4月前
|
缓存 JavaScript 算法
Vue 3性能优化
Vue 3 通过 Proxy 和编译优化提升性能,但仍需遵循最佳实践。合理使用 v-if、key、computed,避免深度监听,利用懒加载与虚拟列表,结合打包优化,方可充分发挥其性能优势。(239字)
371 1
|
5月前
|
开发工具 iOS开发 MacOS
基于Vite7.1+Vue3+Pinia3+ArcoDesign网页版webos后台模板
最新版研发vite7+vue3.5+pinia3+arco-design仿macos/windows风格网页版OS系统Vite-Vue3-WebOS。
563 11
|
4月前
|
JavaScript 安全
vue3使用ts传参教程
Vue 3结合TypeScript实现组件传参,提升类型安全与开发效率。涵盖Props、Emits、v-model双向绑定及useAttrs透传属性,建议明确声明类型,保障代码质量。
441 0
|
9月前
|
JavaScript 数据可视化 前端开发
基于 Vue 与 D3 的可拖拽拓扑图技术方案及应用案例解析
本文介绍了基于Vue和D3实现可拖拽拓扑图的技术方案与应用实例。通过Vue构建用户界面和交互逻辑,结合D3强大的数据可视化能力,实现了力导向布局、节点拖拽、交互事件等功能。文章详细讲解了数据模型设计、拖拽功能实现、组件封装及高级扩展(如节点类型定制、连接样式优化等),并提供了性能优化方案以应对大数据量场景。最终,展示了基础网络拓扑、实时更新拓扑等应用实例,为开发者提供了一套完整的实现思路和实践经验。
1194 78
|
6月前
|
缓存 前端开发 大数据
虚拟列表在Vue3中的具体应用场景有哪些?
虚拟列表在 Vue3 中通过仅渲染可视区域内容,显著提升大数据列表性能,适用于 ERP 表格、聊天界面、社交媒体、阅读器、日历及树形结构等场景,结合 `vue-virtual-scroller` 等工具可实现高效滚动与交互体验。
623 1
|
6月前
|
缓存 JavaScript UED
除了循环引用,Vue3还有哪些常见的性能优化技巧?
除了循环引用,Vue3还有哪些常见的性能优化技巧?
356 0
|
7月前
|
JavaScript
vue3循环引用自已实现
当渲染大量数据列表时,使用虚拟列表只渲染可视区域的内容,显著减少 DOM 节点数量。
169 0
|
9月前
|
JavaScript API 容器
Vue 3 中的 nextTick 使用详解与实战案例
Vue 3 中的 nextTick 使用详解与实战案例 在 Vue 3 的日常开发中,我们经常需要在数据变化后等待 DOM 更新完成再执行某些操作。此时,nextTick 就成了一个不可或缺的工具。本文将介绍 nextTick 的基本用法,并通过三个实战案例,展示它在表单验证、弹窗动画、自动聚焦等场景中的实际应用。
835 17

热门文章

最新文章