找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 67|回复: 3

VueDraggablePlus 专为 Vue 打造的拖拽排序模块,基于 Sortablejs 封装

[复制链接]

86

主题

84

回帖

728

积分

管理员

积分
728
发表于 2024-6-3 10:38:45 | 显示全部楼层 |阅读模式
关于 VueDraggablePlus

VueDraggablePlus 是一个专为 Vue 打造的拖拽排序模块,基于 Sortablejs 封装,支持 Vue3 或 Vue 2.7+。之前,Vue 作者尤雨溪还在社交媒体上推荐了这款组件。

解决痛点

在 Sortablejs 官方以往的 Vue 组件中,都是通过使用组件作为列表的直接子元素来实现拖拽列表,当我们使用一些组件库时,如果组件库中没有提供列表根元素的插槽,我们很难实现拖拽列表,vue-draggable-plus 完美解决了这个问题,它可以让你在任何元素上使用拖拽列表,我们可以使用指定元素的选择器,来获取到列表根元素,然后将列表根元素作为 Sortablejs 的 container,

技术特性
  • 功能强大:全面继承 Sortable.js 拖拽排序库的所有功能;
  • Vue 生态支持好:兼容 Vue3 和 Vue2;
  • 实用灵活:支持组件、指令、函数式调用,我们喜欢那种编程方式都没问题;
  • TS 支持:这个库本身就是用 TypeScript编写,有完整的 TS 文档;
  • 数据绑定:支持 v-model 双向绑定,不需要单独维护排序数据;
  • 支持自定义容器:可以自定某个容器作为拖拽容器,比 Sortable.js 更灵活。

上面提到了,vue-draggable-plus提供三种方式:组件使用方式、hooks使用方式和指令使用方式。下面都给大家介绍一下具体如何使用。


安装

首先安装依赖

  1. npm install vue-draggable-plus
  2. // 或者
  3. yarn add vue-draggable-plus
复制代码

首先导入vue-draggable-plus组件:

  1. import { VueDraggable } from 'vue-draggable-plus'
复制代码


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×

86

主题

84

回帖

728

积分

管理员

积分
728
 楼主| 发表于 2024-6-3 10:56:34 | 显示全部楼层
函数使用

  1. <template>
  2.   <div class="flex gap-2 p-4 w-300px h-300px m-auto bg-gray-500/5 rounded">
  3.     <section ref="el1" class="w-[50%]">
  4.       <div
  5.         v-for="item in list1"
  6.         :key="item.id"
  7.         class="flex justify-between h-30 bg-gray-500/5 rounded p-3 cursor-move"
  8.       >
  9.         <span>{{ item.name }}</span>
  10.       </div>
  11.     </section>
  12.     <section ref="el2" class="w-[50%]">
  13.       <div
  14.         v-for="item in list2"
  15.         :key="item.id"
  16.         class="flex justify-between h-30 bg-gray-500/5 rounded p-3 cursor-move"
  17.       >
  18.         <span>{{ item.name }}</span>
  19.       </div>
  20.     </section>
  21.   </div>
  22. </template>
  23.   
  24. <script setup lang="ts">
  25. import { ref } from "vue";
  26. import { type UseDraggableReturn, VueDraggable } from "vue-draggable-plus";
  27. import { useDraggable } from "vue-draggable-plus";

  28. const list1 = ref([
  29.   {
  30.     name: "风险编号",
  31.     id: "riskNumber",
  32.   },
  33.   {
  34.     name: "风险点等级",
  35.     id: "rishLevel",
  36.   },
  37.   {
  38.     name: "控制目标",
  39.     id: "control target",
  40.   },
  41.   {
  42.     name: "影响程度",
  43.     id: "degree of influnce",
  44.   },
  45. ]);
  46. const list2 = ref([
  47.   {
  48.     name: "曝光量",
  49.     id: "open",
  50.   },
  51.   {
  52.     name: "展示数",
  53.     id: "show",
  54.   },
  55.   {
  56.     name: "点击数",
  57.     id: "click",
  58.   },
  59.   {
  60.     name: "转化数",
  61.     id: "buy",
  62.   },
  63. ]);
  64. const el1 = ref();
  65. const el2 = ref();

  66. useDraggable(el1, list1, {
  67.   animation: 150,
  68.   ghostClass: "ghost",
  69.   group: "number",
  70. });
  71. useDraggable(el2, list2, {
  72.   animation: 150,
  73.   ghostClass: "ghost",
  74.   group: "number",
  75. });
  76. </script>
  77.   
  78. <style scoped>
  79. .ghost {
  80.   opacity: 0.5;
  81.   background: #c8ebfb;
  82. }
  83. </style>
复制代码

86

主题

84

回帖

728

积分

管理员

积分
728
 楼主| 发表于 2024-6-3 10:57:19 | 显示全部楼层
组件使用方式
  1. <template>
  2.     <div class="flex w-full">
  3.       <VueDraggable
  4.         ref="el"
  5.         v-model="list"
  6.         :animation="150"
  7.         ghostClass="ghost"
  8.         class="flex w-full flex-col gap-2 p-4 h-300px"
  9.       >
  10.         <div
  11.           v-for="item in list"
  12.           :key="item.id"
  13.           class="cursor-move flex justify-between h-30"
  14.         >
  15.         <span>{{ item.id }}</span>
  16.         <span>{{ item.name }}</span>
  17.         </div>
  18.       </VueDraggable>
  19.     </div>
  20.   </template>
  21.   
  22.   <script setup lang="ts">
  23.   import { ref } from 'vue'
  24.   import { type UseDraggableReturn, VueDraggable } from 'vue-draggable-plus'
  25.   const list = ref([
  26.     {
  27.       name: '风险编号',
  28.       id: "riskNumber"
  29.     },
  30.     {
  31.       name: '风险点等级',
  32.       id:  "rishLevel"
  33.     },
  34.     {
  35.       name: '控制目标',
  36.       id: "control target"
  37.     },
  38.     {
  39.       name: '影响程度',
  40.       id:  "degree of influnce"
  41.     }
  42.   ])
  43.   const el = ref<UseDraggableReturn>()
  44.   </script>
  45.   
  46.   <style scoped>
  47.   .ghost {
  48.     opacity: 0.5;
  49.     background: #c8ebfb;
  50.   }
  51.   </style>
复制代码

86

主题

84

回帖

728

积分

管理员

积分
728
 楼主| 发表于 2024-6-3 10:58:15 | 显示全部楼层
//指令使用方式
//我们可以将表格的 thead 指定为目标容器,实现表格列拖拽

  1. <template>
  2.   <table class="table table-striped">
  3.     <thead class="thead-dark">
  4.       <tr v-draggable="headers">
  5.         <th class="cursor-move" v-for="header in headers" :key="header.value">
  6.           {{ header.text }}
  7.         </th>
  8.       </tr>
  9.     </thead>
  10.     <tbody>
  11.       <tr v-for="item in list" :key="item.name">
  12.         <td v-for="header in headers" :key="header">
  13.           {{ item[header.value] }}
  14.         </td>
  15.       </tr>
  16.     </tbody>
  17.   </table>
  18. </template>

  19. <script setup lang="ts">
  20. import { ref } from "vue";
  21. import { vDraggable } from "vue-draggable-plus";
  22. const headers = ref([
  23.   {
  24.     text: "序号",
  25.     value: "id",
  26.   },
  27.   {
  28.     text: "日期",
  29.     value: "date",
  30.   },
  31.   {
  32.     text: "展示量",
  33.     value: "show",
  34.   },
  35. ]);
  36. const list = ref([
  37.   {
  38.     date: "2023-10-10",
  39.     show: 17800,
  40.     id: 1,
  41.   },
  42.   {
  43.     date: "2023-10-11",
  44.     show: 56231,
  45.     id: 2,
  46.   },
  47.   {
  48.     date: "2023-10-12",
  49.     show: 763230,
  50.     id: 3,
  51.   },
  52.   {
  53.     date: "2023-10-13",
  54.     show: 21232,
  55.     id: 4,
  56.   },
  57. ]);
  58. </script>
  59. <style scoped>
  60. tr {
  61.   height: 48px;
  62.   background: #fafafa;
  63.   text-align: center;
  64. }
  65. tr td,
  66. tr th {
  67.   min-width: 60px;
  68. }
  69. tr:nth-child(2n) {
  70.   background-color: #f1f6ff;
  71. }
  72. .table {
  73.   width: 100%;
  74. }
  75. </style>
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Discuz! X

GMT+8, 2025-12-6 13:23 , Processed in 0.083721 second(s), 20 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表