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>
相关文章
|
6天前
|
JavaScript
Vue3中路由跳转的语法
Vue3中路由跳转的语法
109 58
|
4天前
|
JavaScript 索引
Vue 2和Vue 3的区别以及实现原理
Vue 2 的响应式系统通过Object.defineProperty来实现,它为对象的每个属性添加 getter 和 setter,以便追踪依赖并响应数据变化。
20 9
|
6天前
|
JavaScript 开发工具
vite如何打包vue3插件为JSSDK
【9月更文挑战第10天】以下是使用 Vite 打包 Vue 3 插件为 JS SDK 的步骤:首先通过 `npm init vite-plugin-sdk --template vue` 创建 Vue 3 项目并进入项目目录 `cd vite-plugin-sdk`。接着,在 `src` 目录下创建插件文件(如 `myPlugin.js`),并在 `main.js` 中引入和使用该插件。然后,修改 `vite.config.js` 文件以配置打包选项。最后,运行 `npm run build` 进行打包,生成的 `my-plugin-sdk.js` 即为 JS SDK,可在其他项目中引入使用。
|
7天前
|
JavaScript 开发者
彻底搞懂 Vue3 中 watch 和 watchEffect是用法
彻底搞懂 Vue3 中 watch 和 watchEffect是用法
|
4天前
|
JavaScript 调度
Vue3 使用 Event Bus
Vue3 使用 Event Bus
10 1
|
4天前
|
JavaScript
Vue3 : ref 与 reactive
Vue3 : ref 与 reactive
9 1
vue3 reactive数据更新,视图不更新问题
vue3 reactive数据更新,视图不更新问题
|
5天前
|
JavaScript
|
5天前
vue3定义暴露一些常量
vue3定义暴露一些常量
|
4天前
Vue3 使用mapState
Vue3 使用mapState
9 0