当前位置:   article > 正文

vue2 中如何使用 vuedraggable 库实现拖拽功能_vue2使用vuedraggable

vue2使用vuedraggable

1.通过 npm 或 yarn 安装 vuedraggable 库

  1. npm install vuedraggable
  2. yarn add vuedraggable

2. 引入组件内部使用,以下代码是一个Demo,可直接复制粘贴演示

注意:因项目使用了 vant,需要安装 vant 才能正常运行

  1. <template>
  2. <div class="wrapper">
  3. <van-nav-bar title="生产订单" left-arrow @click-left="onLeftClick" />
  4. <div class="page_bg">
  5. <div style="display: flex; justify-content: center">
  6. <van-list class="list_wrap myStyle" :loading="loading" :finished="finished" @load="onLoad">
  7. <van-cell class="cell_item" v-for="(item, index) in dataList" :key="index">
  8. <span class="sign"></span>
  9. <div class="mystyle">
  10. <div>销售订单:{{ item.BillNo }}</div>
  11. </div>
  12. <div class="table_wrap" style="z-index: 999">
  13. <draggable class="list-group" :list="item.Entries" item-key="id" group="people" @change="onChange1">
  14. <div v-for="(items, indexs) in item.Entries" :key="indexs" style="padding: 10px">
  15. <div>{{ items }}</div>
  16. </div>
  17. </draggable>
  18. </div>
  19. </van-cell>
  20. </van-list>
  21. <van-list style="width: 50vw; margin-right: 10px" :loading="loading" :finished="finished" @load="onLoad">
  22. <van-cell class="cell_item myCell">
  23. <van-dropdown-menu>
  24. <van-dropdown-item v-model="currentValue" :options="newList" />
  25. <van-button style="position: absolute; top: 12px; right: 15px; width: 100px; height: 55px; z-index: 999999999" type="info" size="small">提交</van-button>
  26. </van-dropdown-menu>
  27. <div class="table_wrap" style="height: 275px">
  28. <draggable class="list-group" :list="listObj.Entries" item-key="id" group="people" @change="onChange2">
  29. <div v-for="(items, index) in listObj.Entries" :key="index" style="padding: 10px">
  30. <div>{{ items }}</div>
  31. </div>
  32. </draggable>
  33. </div>
  34. </van-cell>
  35. </van-list>
  36. </div>
  37. </div>
  38. </div>
  39. </template>
  40. <script>
  41. import Draggable from 'vuedraggable';
  42. export default {
  43. name: 'productlist',
  44. components: { Draggable },
  45. data() {
  46. return {
  47. loading: false,
  48. finished: true,
  49. isLoad: false,
  50. dataList: [],
  51. newList: [
  52. { text: '1号线', value: 0 },
  53. { text: '2号线', value: 1 },
  54. { text: '3号线', value: 2 },
  55. { text: '4号线', value: 3 }
  56. ],
  57. currentValue: 0,
  58. listObj: {
  59. BillNo: '1号线',
  60. Entries: []
  61. }
  62. };
  63. },
  64. mounted() {
  65. this.userInfo = JSON.parse(localStorage.getItem('userInfo'));
  66. this.onSearchClick();
  67. },
  68. methods: {
  69. onLeftClick() {
  70. this.$router.push('/');
  71. },
  72. onLoad() {
  73. if (this.isLoad) {
  74. this.page++;
  75. this.onSearchClick();
  76. }
  77. },
  78. async onSearchClick() {
  79. this.dataList = [
  80. {
  81. BillNo: 'XSDD01',
  82. Entries: ['物料:屏幕;客户:山东', '物料:手机;客户:四川', '物料:电脑;客户:河南', '物料:机箱;客户:北京']
  83. }
  84. ];
  85. },
  86. onChange1(evt) {
  87. console.log('数据列表:', evt);
  88. },
  89. onChange2(evt) {
  90. console.log('响应列表:', evt);
  91. }
  92. }
  93. };
  94. </script>
  95. <style scoped>
  96. ::v-deep .van-popup--top {
  97. --dynamic-width: 50vw;
  98. left: calc(var(--dynamic-width) + 5px);
  99. width: calc(var(--dynamic-width) - 10px);
  100. }
  101. .list_wrap.scroll_div {
  102. max-height: calc(100vh - 123px);
  103. }
  104. .custom-width {
  105. min-width: 200px;
  106. }
  107. .customWidth {
  108. min-width: 100px;
  109. }
  110. .myStyle {
  111. width: 50vw;
  112. padding: 0 5px;
  113. font-size: 12px !important;
  114. }
  115. .myCell {
  116. margin: 0 5px 0 0;
  117. }
  118. ::v-deep .van-dropdown-menu__bar {
  119. position: relative;
  120. height: 40px;
  121. border-bottom: 1px solid #e5e5e5;
  122. }
  123. ::v-deep .van-dropdown-menu__title--active,
  124. ::v-deep .van-dropdown-item__option--active .van-dropdown-item__icon,
  125. ::v-deep .van-dropdown-item__option--active {
  126. color: #0860b9;
  127. }
  128. ::v-deep .van-overlay {
  129. background-color: rgba(0, 0, 0, 0);
  130. }
  131. ::v-deep .van-dropdown-menu__item {
  132. justify-content: left;
  133. }
  134. </style>

3.友情提示,这个库使用方法其实很简单,因为看别人写的比较复杂,自己才写一个Demo 供大家理解,其实这个库中,最重要的就是这个属性 group="people" ,它决定了哪些组件可以相互拖拽,就相当于一个 key,相同 key 的可以相互拖拽。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/797192
推荐阅读
相关标签
  

闽ICP备14008679号