Vue draggable多选拖拽
宜简 人气:0前言
最近产品提出一个排序需求,希望能进行拖拽排序,我一想,vue2插件这么多,sortable.js、draggable分分钟实现了,但是还有一个需求希望能够实现多选拖拽,这个就有点麻烦,查询了许多资料,暂时没有发现能够多选的拖拽组件,如果自己写一个,既耗费时间,实现效果上必然没有draggable这种成熟轮子效果好,于是决定基于draggable做改进。
实现思路
我们知道draggable插件只要设置相同的group名字就能实现分组拖拽,如果我们渲染两个同名分组,控制第二个分组的显示与隐藏,鱼目混珠,就能实现简单的多选拖拽效果
1.通过group="task"设置两个同名分组,同时利用draggable=".card"属性,控制class名称,禁止单选
<draggable group="task" v-model="dataList" @end="draggerEnd" class="" :class="show ? 'order-box-show' : 'order-box'" animation="300" draggable=".card"> <transition-group> <div v-for="(item, index) in dataList" :key="index" class="box" @click="handleSelect(index)" :class="item.select || show ? 'card' : ''">{{ item.name }} </div> </transition-group> </draggable> <div class="position" v-show="show"> <draggable group="task" v-model="dataList2" @end="draggerEnd" class="order-box" animation="300"> <transition-group> <div v-for="(item, index) in dataList2" :key="index" class="boxs"> <div> {{ item.name }} </div> </div> </transition-group> </draggable>
2.通过v-show="show"控制第二个分组的隐藏与展示,同时对数据做处理,并且在拖拽结束后,要在分组一中找到分组二拖拽过来的初始元素删掉
const findIndex = this.dataList.findIndex((item => item.name === '选中元素展示区')) this.dataList.splice(findIndex, 1)
Demo全部代码
<template> <div> <div> <draggable group="task" v-model="dataList" @end="draggerEnd" class="" :class="show ? 'order-box-show' : 'order-box'" animation="300" draggable=".card"> <transition-group> <div v-for="(item, index) in dataList" :key="index" class="box" @click="handleSelect(index)" :class="item.select || show ? 'card' : ''">{{ item.name }} </div> </transition-group> </draggable> <div class="position" v-show="show"> <draggable group="task" v-model="dataList2" @end="draggerEnd" class="order-box" animation="300"> <transition-group> <div v-for="(item, index) in dataList2" :key="index" class="boxs"> <div> {{ item.name }} </div> </div> </transition-group> </draggable> </div> </div> <el-button style="marginLeft:50px" @click="handleStart">确认</el-button> </div> </template> <script> import draggable from 'vuedraggable' export default { components: { draggable }, data() { return { dataList: [ ], dataList2: [{ name: '选中元素展示区' }], show: false, selectArr: [], } }, mounted() { for (var i = 0; i < 60; i++) { this.dataList.push({ name: i, select: false }) } }, methods: { handleSelect(index) { const data = JSON.parse(JSON.stringify(this.dataList)) data[index].select = true this.selectArr.push({ ...data[index] }) this.dataList = JSON.parse(JSON.stringify(data)) }, handleStart(e) { if (!this.selectArr.length) { return } this.show = true this.selectArr.forEach((item, index) => { const Index = this.dataList.findIndex(it => item.name === it.name) this.dataList.splice(Index, 1) }) }, draggerEnd(e) { const newIndex = e.newIndex this.dataList.splice(newIndex, 0, ...this.selectArr) const findIndex = this.dataList.findIndex((item => item.name === '选中元素展示区')) this.dataList.splice(findIndex, 1) this.show = false }, } } </script> <style lang="scss" scoped> .order-box { padding: 20px 50px; display: flex; flex-wrap: wrap; >span { flex: 1; display: flex; flex-wrap: wrap; } .box { width: 100px; height: 50px; background: rgb(7, 174, 215); margin-right: 10px; margin-bottom: 10px; } .boxs { width: 100px; height: 50px; background: rgb(215, 177, 7); margin-right: 10px; margin-bottom: 10px; } .card { background: rgb(128, 68, 0) !important; } } .order-box-show { padding: 20px 50px; display: flex; flex-wrap: wrap; >span { flex: 1; display: flex; flex-wrap: wrap; } .box { width: 100px; height: 50px; background: rgb(7, 174, 215); margin-right: 10px; margin-bottom: 10px; } .boxs { width: 100px; height: 50px; background: rgb(215, 177, 7); margin-right: 10px; margin-bottom: 10px; } } .position { border: 1px solid red; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); cursor: pointer; } </style>
Demo实现效果
加载全部内容