温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何在Vue中利用vue-draggable 插件实现在不同列表间的拖拽功能

发布时间:2021-02-19 17:00:27 阅读:490 作者:Leah 栏目:web开发
Vue开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

如何在Vue中利用vue-draggable 插件实现在不同列表间的拖拽功能?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

安装vue-draggable:

npm install vuedraggable

在使用插件的组件内引入vue-draggable并注册组件:

import draggable from "vuedraggable"

components: {
 draggable
}

然后在我们需要拖拽的列表中使用:

<draggable class="selected-list" tag="ul" 
v-model="selectedTheme" 
v-bind="dragOptions"
 :move="onMove"
 @end="onEnd" 
 >
 <li class="selected-theme"
 v-for="item in selectedTheme"
 :key="item.type"
 >{{item.name}}</li>
</draggable>

下面是拖拽功能组件的完整代码:

<template>
 <div class="theme-setting">
 <el-dialog
 title="设置选项"
 :visible.sync="dialogVisible"
 width="648px"
 :close-on-click-modal="false"
 >
 <div class="theme-left">
  <dl class="theme-title">
  <dt class="title">当前选项</dt>
  <dd class="des">从右侧拖拽添加</dd>
  </dl>
  <draggable class="selected-list" tag="ul" 
  v-model="selectedTheme" 
  v-bind="dragOptions"
  :move="onMove"
  @end="onEnd" 
  >
  <li class="selected-theme"
  v-for="item in selectedTheme"
  :key="item.type"
  >{{item.name}}</li>
  </draggable>
 </div>
 <div class="theme-right">
  <h4 class="theme-right-title">全部选项</h4>
  <draggable class="theme-right-list" tag="ul"
  v-model="unSelectTheme"
  v-bind="dragOptions"
  :move="onMove"
  @end="onEnd">
  <li class="theme-right-item"
  v-for="item in unSelectTheme"
  :key="item.type"
  >{{item.name}}</li>
  </draggable>
 </div>
 <div class="drag-drop-del" v-show="isShowDel">
  <img src="../assets/imgs/drapDrop/drag_drop_del.png" alt="">
 </div>
 <span slot="footer" class="dialog-footer">
  <el-button @click="restoreDefault">恢复默认设置</el-button>
  <el-button type="primary" @click="saveThemeSet">保存</el-button>
 </span>
 </el-dialog>
 </div>
</template>

<script>
import {Messagefrom 'element-ui'
import draggable from "vuedraggable" 
 export default {
 name'DragDrop',
 components: {
 draggable
 },
 data() {
 return {
  dialogVisiblefalse,
  selectedTheme: [{
  type1,
  name'选项1'
  }, {
  type2,
  name'选项2'
  }, {
  type3,
  name'选项3'
  }, {
  type4,
  name'选项4'
  }], // 已选主题列表
  unSelectTheme: [{
  type5,
  name'选项5'
  }, {
  type6,
  name'选项6'
  }], // 未选主题列表
  backSelectedTheme: [], // 选主题列表备份
  backUnSelectTheme: [], // 未选主题列表备份用于恢复默认设置
  relatedListLast: {}, // 已选主题列表最后一项
  isShowDelfalse
 }
 },
 methods: {
 showDrag() {
  this.dialogVisible = true
 },
 onMove({ relatedContext, draggedContext, to }) {
  const relatedElement = relatedContext.element
  const draggedElement = draggedContext.element
  let dragInEl = to['className']
  if (dragInEl == 'selected-list') {
  this.isShowDel = false
  if (this.selectedTheme.length === 4) { 
   // 判断往已选列表拖时,如果已经满足4项,则记录已选列表的最后一项
   // 拖拽结束时将此项清除到未选列表中
   this.relatedListLast = this.selectedTheme[this.selectedTheme.length-1]
  }
  } else {
  this.isShowDel = true // 判断如果是往未选列表里拖的话显示垃圾桶
  }
  return (
  (!relatedElement || !relatedElement.fixed) && !draggedElement.fixed
  )
 },
 onEnd(dragObj) {
  let dragInEl = dragObj.to['className']
  if (dragInEl == 'selected-list') {
  if (this.selectedTheme.length > 4) {
   // 判断已选列表大于4项,将记录的最后一项过滤出来,并push到未选列表数组
   this.selectedTheme = this.selectedTheme.filter(item => {
   return item.type != this.relatedListLast.type
   })
   this.unSelectTheme.push(this.relatedListLast)
  }
  }
  if (dragInEl === 'theme-right-list') {
  // 判断是往未选列表拖时,拖拽结束时将垃圾桶隐藏
  this.isShowDel = false
  }
 },
 // 保存设置
 saveThemeSet() {
  const params = {
  taskTopicListthis.selectedTheme
  }
  if (this.selectedTheme.length !== 4) {
  Message({
   type'error',
   message'需设置4个选项 !'
  })
  return false
  }
  $ajax.save(params).then(data => {
  this.dialogVisible = false
  Message({
   type'success',
   message'保存成功!'
  })
  this.$parent.refresh()
  }).catch(err => {
  console.log(err)
  })
 },
 // 恢复默认设置
 restoreDefault() {
  this.selectedTheme = this.backSelectedTheme
  this.unSelectTheme = this.backUnSelectTheme
 }
 },
 computed: {
 dragOptions() {
  return {
  animation0,
  group"description",
  disabledfalse,
  ghostClass"ghost"
  }
 }
 }
 };
</script>
<style lang="less" scoped>
bodyuldldtddlih2h4{
 margin0;
 padding0;
}
ulolli {
 list-style: none;
}
.theme-setting {
 /deep/.el-dialog {
 height476px;
 border-radius6px;
 .el-dialog__header {
  height55px;
  line-height56px;
  padding0;
  border-bottom1px solid rgba(13,20,300.1);
  .el-dialog__title {
  height:21px;
  font-size:16px;
  font-family:MicrosoftYaHei-Bold,MicrosoftYaHei;
  font-weight:bold;
  color:rgba(13,20,30,1);
  line-height:21px;
  }
  .el-dialog__headerbtn {
  margin-top: -4px;
  }
 }
 .el-dialog__body {
  position: relative;
  display: flex;
  height331px;
  padding0;
  border-bottom1px solid rgba(13,20,300.1);
  .theme-left {
  width218px;
  margin-left24px;
  border-right1px solid rgba(13,20,300.1);
  .theme-title {
   display: flex;
   margin-top24px;
   .title {
   height:19px;
   margin-right4px;
   font-size:14px;
   font-family:MicrosoftYaHei-Bold,MicrosoftYaHei;
   font-weight:bold;
   color:rgba(13,20,30,1);
   line-height:19px;
   }
   .des {
   height:16px;
   font-size:12px;
   font-family:MicrosoftYaHei;
   color:rgba(13,20,30,0.6);
   line-height:19px;
   }
  }
  .selected-list {
   height240px;
   margin-top24px;
   overflow: hidden;
   .selected-theme {
   width:160px;
   height:48px;
   line-height:48px;
   text-align: center;
   margin-bottom16px;
   cursor: pointer;
   background:linear-gradient(180deg,rgba(43,46,83,10%,rgba(108,116,150,1100%);
   border-radius:6px;
   font-size:14px;
   font-family:MicrosoftYaHei;
   color:rgba(255,255,255,1);
   }
  }
  }
  .theme-right {
  padding0 24px;
  .theme-right-title {
   padding-top24px;
   height:19px;
   font-size:14px;
   font-family:MicrosoftYaHei-Bold,MicrosoftYaHei;
   font-weight:bold;
   color:rgba(13,20,30,0.4);
   line-height:19px;
  }
  .theme-right-list {
   width357px;
   height240px;
   overflow: scroll;
   margin-top24px;
   .theme-right-item {
   width160px;
   height:48px;
   line-height:48px;
   float: left;
   margin-right16px;
   margin-bottom16px;
   background:rgba(247,248,252,1);
   border-radius:6px;
   font-size:14px;
   font-family:MicrosoftYaHei;
   color:rgba(13,20,30,0.4);
   text-align: center;
   cursor: pointer;
   }
  }
  .theme-right-list::before.theme-right-list::after {
   content"";
   display: table;
  }
  .theme-right-list::after {
   clear: both;
  }
  }
  .drag-drop-del {
  position: absolute;
  right1px;
  top0;
  width404px;
  height331px;
  display: flex;
  justify-content: center;
  align-items: center;
  background-imageurl('../../src/assets/imgs/drapDrop/drag_drop.png');
  img {
   width96px;
   height96px;
  }
  }
 }
 .el-dialog__footer {
  height88px;
  padding24px 24px 0;
  .dialog-footer {
  .el-button+.el-button {
   margin-left16px;
  }
  }
 }
 }
}
</style>

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×