vue.draggable 拖拽

打印 上一主题 下一主题

主题 997|帖子 997|积分 2991

文档:
vue.draggable中文文档 - itxst.com


 

参考:
Vue.jsvuedraggable插件安装与使用教程-CSDN博客 
vue使用vuedraggable实现嵌套多层拖拽排序功能_vue.js_脚本之家 
https://zhuanlan.zhihu.com/p/611811323 

安装vuedraggable插件

  1. npm i vuedraggable
复制代码
 
  1. <template>
  2.   <div class="left-allocate">
  3.     <!-- 拖拽部分开始 -->
  4.     <div class="drag-distribute-cls">
  5.       <div class="waitloadcar-list">
  6.         <draggable
  7.           v-show="waitLoadList.length > 0"
  8.           v-model="waitLoadList"
  9.           forceFallback="true"
  10.           group="vehicle-station"
  11.           animation="0"
  12.           :sort="false"
  13.           touchStartThreshold="0px"
  14.           dragClass="dragClass"
  15.           ghostClass="ghostClass"
  16.           chosenClass="chosenClass"   
  17.           @start="onStart"
  18.           @end="onEnd">
  19.           <transition-group>
  20.             <div v-for="(vehicle, index) in waitLoadList" :key="index" :class="['vehicle-cls', vehicle.type === 'pallet' ? 'pallet-cls' : 'shelves-cls']">
  21.               <van-image
  22.                 v-if="vehicle.type === 'shelves'"
  23.                 width="48"
  24.                 height="48"
  25.                  :src="require('../src/assets/logo.png')"
  26.               />
  27.               <van-image
  28.                 v-if="vehicle.type === 'pallet'"
  29.                 width="68"
  30.                 height="48"
  31.                 :src="require('../src/assets/logo.png')"
  32.               />
  33.               <van-image
  34.                 v-if="vehicle.type === 'cage'"
  35.                 width="64"
  36.                 height="64"
  37.                   :src="require('../src/assets/logo.png')"
  38.               />
  39.               <span class="vehicle-name">{
  40.   
  41.   { vehicle.name }}</span>
  42.             </div>
  43.           </transition-group>
  44.         </draggable>
  45.         <div v-show="waitLoadList.length === 0" class="vehicle-cls shelves-cls">无未分配载具</div>
  46.       </div>
  47.       <h4 class="header">放置位置列表</h4>
  48.       <div class="workposition-list">
  49.         <draggable
  50.           v-show="workpositionList.length"
  51.           v-model="workpositionList"
  52.           group="vehicle-station"
  53.           :disabled="false"
  54.           :sort="false"
  55.           handle=".mover-butuodong">
  56.           <transition-group>
  57.             <div v-for="(workpos, index) in workpositionList"
  58.               :key="index"
  59.               class="workpos-cls"
  60.               @mousemove="workposMove"
  61.               :data-actindex="index"
  62.               :style="{ marginRight: index % 2 ? '16px': '0px'}">
  63.                 <h5 :style="{ marginTop: index % 2 !== 0 ? '48px' : '16px' }">{
  64.   
  65.   { index % 2 === 0 ? '放置位置' + workpos.station : ''}}</h5>
  66.                 <!-- 托盘 -->
  67.                 <div class="pallet-icon" v-if="workpos.type === 'pallet'">
  68.                   <van-image
  69.                     width="68"
  70.                     height="48"
  71.                     :src="require('../src/assets/logo.png')"
  72.                   />
  73.                   <span class="sel-text">{
  74.   
  75.   { workpos.name }}</span>
  76.                 </div>
  77.                 <!-- 移动货架 -->
  78.                 <div class="shelves-icon" v-if="workpos.type === 'shelves'">
  79.                   <div class="left-shelves">
  80.                     <van-image
  81.                       width="48"
  82.                       height="48"
  83.   :src="require('../src/assets/logo.png')"
  84.                     />
  85.                     <span class="sel-text">{
  86.   
  87.   { workpos.name }}</span>
  88.                   </div>
  89.                 </div>
  90.                 <!-- 笼车 -->
  91.                 <div class="shelves-icon" v-if="workpos.type === 'cage'">
  92.                   <div class="left-shelves">
  93.                     <van-image
  94.                       width="48"
  95.                       height="48"
  96.                       :src="require('../src/assets/logo.png')"
  97.                     />
  98.                     <span class="sel-text">{
  99.   
  100.   { workpos.name }}</span>
  101.                   </div>
  102.                 </div>
  103.                 <!-- 放置位置空 -->
  104.                 <div class="shelves-icon" v-if="workpos.type === 'kong'">
  105.                   <div class="left-kong">空</div>
  106.                 </div>
  107.             </div>
  108.           </transition-group>
  109.         </draggable>
  110.         <div v-show="workpositionList.length === 0" class="workpos-kong">
  111.           无可分配放置位置
  112.         </div>
  113.       </div>
  114.     </div>
  115.   </div>
  116. </template>
  117. <script>
  118. import draggable from 'vuedraggable'
  119. export default {
  120.   name: 'DraggerName',
  121.   components: {
  122.     draggable
  123.   },
  124.   data () {
  125.     return {
  126.       waitLoadList: [
  127.         // { type: 'shelves', name: '测试11', station: '', vehicleId: '1', amount: 4000.00 },
  128.         { type: 'pallet', name: '测试两个15', station: '', vehicleId: '5', amount: 4000.00 },
  129.         { type: 'shelves', name: '测试16', station: '', vehicleId: '6', amount: 4000.00 },
  130.         { type: 'shelves', name: '测试17', station: '', vehicleId: '7', amount: 4000.00 },
  131.         { type: 'pallet', name: '测试两个18', station: '', vehicleId: '8', amount: 4000.00 }
  132.       ],
  133.       // 数据的一些约定:托盘(type: 'pallet')放在放置位置的第一个;type: 'fu' 不显示
  134.       workpositionList: [
  135.         { type: 'kong', name: '', station: '1', storageId: '1' },
  136.         { type: 'kong', name: '', station: '1', storageId: '2' },
  137.         { type: 'kong', name: '', station: '2', storageId: '3' },
  138.         { type: 'kong', name: '', station: '2', storageId: '4' },
  139.         { type: 'kong', name: '', station: '3', storageId: '5' },
  140.         { type: 'kong', name: '', station: '3', storageId: '6' }
  141.       ],
  142.       waitDragStart: [],
  143.       waitVehicleing: {}, // 正在拖动的
  144.       workDragStart: []
  145.     }
  146.   },
  147.   mounted () {
  148.     // 重置hover
  149.     document.body.style.setProperty('--bg-color', 'transparent')
  150.     document.body.style.setProperty('--border-value', '1px dotted #CACDD6')
  151.   },
  152.   methods: {
  153.     // 动态获取放置编号
  154.     workposMove (e) {
  155.       this.workposNo = e.currentTarget.dataset.actindex
  156.     },
  157.     // 开始拖拽事件
  158.     onStart (e) {
  159.       const { oldIndex } = e
  160.       this.waitVehicleing = this.waitLoadList[oldIndex]
  161.       // this.waitDragStart = deepClone(this.waitLoadList)
  162.       // this.workDragStart = deepClone(this.workpositionList)
  163.       // 添加hover
  164.       document.body.style.setProperty('--bg-color', 'rgba(241, 151, 44, 0.10)')
  165.       document.body.style.setProperty('--border-value', '1px solid #F1972C')
  166.     },
  167.     // 拖拽结束事件
  168.     onEnd (e) {
  169.       // 重置hover
  170.       document.body.style.setProperty('--bg-color', 'transparent')
  171.       document.body.style.setProperty('--border-value', '1px dotted #CACDD6')
  172.       // hover样式的序号
  173.       const hoverIndex = Number(this.workposNo)
  174.       // 开始分配
  175.       const { newIndex, oldIndex } = e
  176.       // 列表没有变化
  177.       if ((newIndex === oldIndex) && (this.waitDragStart.length === this.waitLoadList.length)) {
  178.         return false
  179.       }
  180.       this.workpositionList.splice(newIndex, 1)
  181.       const { type: waitType } = this.waitVehicleing
  182.       const { type: workType, name: workName, station: workStation, storageId: workStorageId } = this.workpositionList[hoverIndex]
  183.       if (workName) {
  184.         this.waitLoadList.push({ type: workType, name: workName, station: '' })
  185.       }
  186.       if (['shelves', 'cage'].includes(waitType)) {
  187.         this.workpositionList[hoverIndex] = {
  188.           ...this.waitVehicleing,
  189.           station: workStation,
  190.           storageId: workStorageId
  191.         }
  192.       } else if (waitType === 'pallet') {
  193.         this.workpositionList[hoverIndex] = {
  194.           ...this.waitVehicleing,
  195.           station: workStation,
  196.           storageId: workStorageId
  197.         }
  198.         // 托盘占两个格子
  199.         if (hoverIndex % 2) {
  200.           // 第奇数个
  201.           const { type: sworkType, name: sworkName, station: sworkStation, storageId: sworkStorageId } = this.workpositionList[hoverIndex - 1]
  202.           if (sworkName) {
  203.             this.waitLoadList.push({ type: sworkType, name: sworkName, station: '' })
  204.           }
  205.           this.workpositionList.splice(hoverIndex + 1, 0, { type: 'fu', name: '', station: sworkStation, storageId: sworkStorageId })
  206.           this.workpositionList.splice(hoverIndex - 1, 1)
  207.         } else {
  208.           // 第偶数个
  209.           const { type: sworkType, name: sworkName, station: sworkStation, storageId: sworkStorageId } = this.workpositionList[hoverIndex + 1]
  210.           if (sworkName) {
  211.             this.waitLoadList.push({ type: sworkType, name: sworkName, station: '' })
  212.           }
  213.           this.workpositionList[hoverIndex + 1] = { type: 'fu', name: '', station: sworkStation, storageId: sworkStorageId }
  214.         }
  215.       }
  216.     }
  217.   }
  218. }
  219. </script>
  220. <style lang="scss" scoped>
  221. .left-allocate {
  222.   .drag-distribute-cls {
  223.     .waitloadcar-list {
  224.       width: 100%;
  225.       min-height: 134px;
  226.       div span {
  227.         display: flex;
  228.         flex-direction: row;
  229.         flex-wrap: wrap;
  230.       }
  231.       .vehicle-cls {
  232.         height: 102px;
  233.         background: #FFFFFF;
  234.         box-shadow: 0 1px 6px 0 rgba(0,0,0,0.08);
  235.         border-radius: 8px;
  236.         margin: 16px;
  237.         display: flex;
  238.         flex-direction: column;
  239.         align-items: center;
  240.         justify-content: space-around;
  241.       }
  242.       .shelves-cls {
  243.         width: 126px;
  244.       }
  245.       .pallet-cls {
  246.         width: 262px;
  247.       }
  248.       .vehicle-name {
  249.         font-family: PingFangSC-Regular;
  250.         font-size: 14px;
  251.         color: rgba(0,0,0,0.70);
  252.         letter-spacing: 0;
  253.         text-align: center;
  254.         font-weight: 400;
  255.       }
  256.     }
  257.     .workposition-list {
  258.       .workpos-kong {
  259.         width: 126px;
  260.         height: 102px;
  261.         background: #ffffff;
  262.         box-shadow: 0 1px 6px 0 rgba(0, 0, 0, 0.08);
  263.         border-radius: 8px;
  264.         margin: 16px;
  265.         display: flex;
  266.         flex-direction: column;
  267.         align-items: center;
  268.         justify-content: space-around;
  269.       }
  270.       div span {
  271.         display: flex;
  272.         flex-wrap: wrap;
  273.         margin-left: 16px;
  274.       }
  275.       .workpos-cls {
  276.         .sel-text {
  277.           display: inline;
  278.           margin-left: 0px;
  279.         }
  280.         h5 {
  281.           font-family: PingFangSC-Regular;
  282.           font-size: 16px;
  283.           color: rgba(0,0,0,0.70);
  284.           letter-spacing: 0;
  285.           font-weight: 400;
  286.           margin: 16px 0 12px;
  287.         }
  288.         .pallet-icon {
  289.           width: 262px;
  290.           height: 102px;
  291.           background: #DBF3F0;
  292.           box-shadow: 0 1px 6px 0 rgba(0,0,0,0.08);
  293.           border-radius: 8px;
  294.           display: flex;
  295.           flex-direction: column;
  296.           justify-content: space-around;
  297.           align-items: center;
  298.         }
  299.         .shelves-icon {
  300.           display: flex;
  301.           flex-direction: row;
  302.           align-items: center;
  303.           justify-content: space-between;
  304.           .left-kong {
  305.             width: 126px;
  306.             height: 102px;
  307.             line-height: 102px;
  308.             background: #F5F6F9;
  309.             border: 1px dotted #CACDD6;
  310.             border-radius: 8px;
  311.             text-align: center;
  312.             font-family: PingFangSC-Medium;
  313.             font-size: 18px;
  314.             color: rgba(0,0,0,0.40);
  315.             letter-spacing: 0;
  316.             font-weight: 500;
  317.             margin: 0 4px;
  318.             &:hover {
  319.               background-color: var(--bg-color);
  320.               border: var(--border-value);
  321.             }
  322.           }
  323.           .left-shelves {
  324.             width: 126px;
  325.             height: 102px;
  326.             background: #DBF3F0;
  327.             box-shadow: 0 1px 6px 0 rgba(0,0,0,0.08);
  328.             border-radius: 8px;
  329.             display: flex;
  330.             flex-direction: column;
  331.             justify-content: space-around;
  332.             align-items: center;
  333.             margin: 0 4px;
  334.           }
  335.         }
  336.       }
  337.     }
  338.     .dragClass {
  339.       border: 1px solid #01AE97;
  340.       border-radius: 8px;
  341.     }
  342.     .ghostClass {
  343.       display: none;
  344.     }
  345.   }
  346. }
  347. </style>
复制代码
 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

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

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

美丽的神话

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表