当前位置:   article > 正文

vue2 draggable拖拽组件的使用及注意事项_draggable组件

draggable组件

记录draggable拖拽组件的使用方法以及遇到的问题。

参考文章draggable拖拽组件使用

参考文章Vue项目拖拽插件 ---- vuedraggable的安装与使用

  • 效果图

 vue.draggable中文api地址https://www.itxst.com/vue-draggable/tutorial.html

  •  安装
npm install vuedraggable
  • 引入与使用
  1. <template>
  2. <div class="cardDrag-container common-container">
  3. <div class="cardDrag-content">
  4. <div class="left-drag-container">
  5. <draggable
  6. tag="ul"
  7. class="cardDrag-list-wrap"
  8. v-model="leftComponentsList"
  9. :group="groupLeft"
  10. animation="300"
  11. >
  12. <li
  13. class="cardDrag-list"
  14. v-for="item in leftComponentsList"
  15. :key="item.id"
  16. >
  17. <div class="cardDrag">
  18. <svg-icon class="svg-width" :iconClass="item.icon"></svg-icon>
  19. <label>{{item.name}}</label>
  20. </div>
  21. </li>
  22. </draggable>
  23. </div>
  24. <div class="right-drag-container">
  25. <draggable
  26. tag="ul"
  27. class="cardDrag-list-wrap"
  28. v-model="rightComponentsList"
  29. :group="groupRight"
  30. animation="300"
  31. @add="handleAdd"
  32. >
  33. <li
  34. class="cardDrag-list"
  35. v-for="item in rightComponentsList"
  36. :key="item.id"
  37. >
  38. <div class="cardDrag cardBackground" :style="computedStyle">
  39. <div class="card-box">
  40. <svg-icon class="svg-width" :iconClass="item.icon"></svg-icon>
  41. <label>{{item.name}}({{item.orderId}})</label>
  42. </div>
  43. </div>
  44. </li>
  45. </draggable>
  46. </div>
  47. </div>
  48. </div>
  49. </template>
  1. <script>
  2. import draggable from 'vuedraggable'
  3. import _ from 'lodash'
  4. export default {
  5. name: 'index',
  6. components: {
  7. draggable,
  8. },
  9. data(){
  10. return{
  11. leftComponentsList:[
  12. { id: 1, orderId: 1, name: '电商购物评价', icon: 'dianshanggouwupingjia' },
  13. { id: 2, orderId: 2, name: '波特五利', icon: 'bote5li' },
  14. { id: 3, orderId: 3, name: '提炼产品卖点', icon: 'tilianchanpinmaidian' },
  15. { id: 4, orderId: 4, name: '写周报', icon: 'xiezhoubao' },
  16. ],
  17. rightComponentsList:[
  18. { id: 11, orderId: 109, name: '产品卖货笔记', icon: 'chanpinmaihuobiji' },
  19. { id: 12, orderId: 110, name: '优化简历', icon: 'youhuajianli' },
  20. { id: 13, orderId: 111, name: '数据分析', icon: 'shujufenxi' },
  21. { id: 14, orderId: 112, name: '用户画像', icon: 'xieyonghuhuaxiang' },
  22. ],
  23. groupLeft:{
  24. name: 'itxst',
  25. pull: 'clone', // 拖出
  26. put: false, // 拖入
  27. },
  28. groupRight:{
  29. name: 'itxst',
  30. pull: false, // 拖出
  31. put: true, // 拖入
  32. },
  33. }
  34. },
  35. }
  36. </script>

v-model绑定拖拽的数组。

下面说明一下用到的属性及事件。

  • group

拖拽分组,多组之间相互拖拽,可以实现不同数组之间相互拖拽。例如group都为itxst的分组可以相互拖拽。

  1. //设置方式一,直接设置组名
  2. group:'itxst'
  3. //设置方式,object,也可以通过自定义函数function实现复杂的逻辑
  4. group:{
  5. name:'itxst',//组名为itxst
  6. pull: true|false| 'clone'|array|function,//是否允许拖出当前组
  7. put:true|false|array|function,//是否允许拖入当前组
  8. }
  • tag

draggable 标签在渲染后展现出来的标签类型,默认渲染成div。

  • animation

拖拽时的动画时间,单位:ms。

  • add

添加单元时的回调函数。

下面列出在使用draggable过程中发现的两个问题。

  • 注意事项一

需要设置拖拽容器的高度,否则,当容器内没有小块时,高度为0,再想拖拽小块进入该容器就拖不进去了。

  1. <style lang="scss">
  2. .left-drag-container div span{
  3. display: inline-block;
  4. width: 100% !important;
  5. height: calc(100vh - 180px) !important;
  6. }
  7. .right-drag-container div span{
  8. display: inline-block;
  9. width: 100% !important;
  10. height: calc(100vh - 180px) !important;
  11. }
  12. </style>
  • 注意事项二

在这里,左边的容器拖出时是clone模式,连续拖拽同一个到右边容器时,控制台提示报错,并且组件展示的内容都一样,组件的属性值也被修改成相同的值。

页面的展示内容

此时的代码是这么写的

  1. handleAdd(e){
  2. const newIndex = e.newIndex
  3. const newCard = this.rightComponentsList[newIndex]
  4. newCard.id = this.randomNumber()
  5. newCard.orderId = this.cardId
  6. this.cardId = this.cardId + 1
  7. this.$set(this.rightComponentsList,newIndex,newCard)
  8. },

可以看到页面上,新拖拽的三个组件显示的orderId是一样的,后来发现造成这个错误是因为,在右边容器添加组件,取该组件的属性时使用的是简单赋值,在拖拽新的组件时,修改了同组件的属性值。

代码做如下修改

  1. handleAdd(e){
  2. const newIndex = e.newIndex
  3. // 深拷贝
  4. const newCard = _.cloneDeep(this.rightComponentsList[newIndex])
  5. newCard.id = this.randomNumber()
  6. newCard.orderId = this.cardId
  7. this.cardId = this.cardId + 1
  8. this.$set(this.rightComponentsList,newIndex,newCard)
  9. },

 以上就是在使用过程中发现的问题啦~

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

闽ICP备14008679号