在vue中使用vuedraggable组件与html5原生拖拽事件
文章目录
最近,需要在项目中实现附件分类、调整附件之间的顺序及附件类别之间的顺序。先上结论,vuedraggable仅实现了前两个功能点,HTML5原生拖拽事件实现了全部的三个功能点。也可能是我使用vuedraggable的打开方式不对,若各位看官知道的话,还请不吝赐教。
一、宫格式
这种交互使用vuedraggable,可实现附件分类和调整某个类别下的附件顺序,但无法调整附件类别之间的顺序。
实现思路
1、附件列表截取,因为每行只显示3个类别,当然您也可以通过代码指定任意的个数。
2、用v-for来渲染。
3、剩下的交给vuedraggable。
一、布局
<div v-for="(item,index) in dataList" :key="index" class="flex">
<div v-for="nestItem in item" :key="nestItem.id" class="attachmentBox flex-item">
<h4>{{nestItem.attachmentTypeName}}({{nestItem.filelist.length}})</h4>
<draggable tag="ul" v-model="nestItem.filelist" animation="100" item-key="id" :group="{name:'type', pull: true, put: true}">
<template #item="{ element }">
<li @click="preview(element)">
<img :src="element.fileUrl" style="width:60px;height: 60px;" />
<p>{{element.fileName}}</p>
</li>
</template>
</draggable>
</div>
</div>二、截取
this.dataList = this.$TOOL.array.chunkItem(JSON.parse(sourceList),3)二、标签式
这种交互使用HTML5原生拖拽事件,可实现上述的三个功能点,且支持多选后的批量拖拽操作。
实现思路
一、布局
1、左右布局,通过左侧标签筛选右侧内容。
2、右侧上方为内容区域,下方为选中项区域。
HTML
<el-container>
<el-aside>
<ul class="tabBox">
<li v-for="(item,index) in tabList" :key="item.id" :class="activeTabIndex===index ? 'active' : ''" draggable="true"
@dragstart="dragStart('tab', item, index)" @dragover="allowDrop" @drop="drop($event, index)" @click="toogleActive(index)">
<h4 class="text-center">{{`${item.name}(${item.length})`}}</h4>
</li>
</ul>
</el-aside>
<div class="flex flex-col">
<div class="flex-item attachmentBox">
<ul>
<el-checkbox-group v-model="checkList" class="margin-20">
<el-checkbox v-for="(item,index) in dataList[activeTabIndex].filelist" :key="item.id" :label="item.id" style="height: auto;">
<li draggable="true" @dragstart="dragStart('file', item, index)" @dragover="allowDrop" @drop="drop($event,index)"
@click="preview(item)">
<img :src="item.fileUrl" style="width:60px;height: 60px;" />
<p>{{ item.fileName }}</p>
</li>
</el-checkbox>
</el-checkbox-group>
</ul>
</div>
<ul class="attachmentBox" style="height:300px;" draggable="true" @dragstart="dragStart('files')">
<li v-for="item in fileCollectList" :key="item.id">
<img :src="item.fileUrl" style="width:60px;height: 60px;" />
<p>{{ item.fileName }}</p>
</li>
</ul>
</div>
</el-container>CSS
<style lang="scss" scoped>
.tabBox li {
height: 60px;
margin: 10px;
border: 1px solid var(--el-color-primary-light-3);
}
.tabBox li.active {
background-color: var(--el-color-primary-light-3);
color: white;
}
.tabBox li h4 {
line-height: 60px;
}
.attachmentBox li {
display: inline-block;
width: 100px;
height: 100px;
text-align: center;
margin: 20px;
color: #fff;
padding: 4px;
cursor: move;
border-radius: 4px;
border: 1px solid var(--el-color-primary-light-3);
}
.attachmentBox li:not(:first-child){
margin-left: 0;
}
.attachmentBox li p {
font-size: 12px;
color: black;
margin-top: 6px;
min-height: 20px;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
</style>二、拖拽
file: 单个附件,files: 多个附件,tab: 标签
dragStart(type, item, index){
this.dragType = type
this.sourceFile = item
this.sourceFileIndex = index
},
allowDrop(e){
e.preventDefault()
},
drop(event, index) {
if(this.dragType === 'file'){
if(event.target.tagName === 'H4'){
this.dataList[this.activeTabIndex].filelist.splice(this.sourceFileIndex, 1)
this.dataList[index].filelist.push(this.sourceFile)
//this.activeTabIndex = index
}else{
let temp = this.dataList[this.activeTabIndex].filelist[index]
this.dataList[this.activeTabIndex].filelist[index] = this.dataList[this.activeTabIndex].filelist[this.sourceFileIndex]
this.dataList[this.activeTabIndex].filelist[this.sourceFileIndex] = temp
}
if(this.checkList.length){
this.checkList = []
}
}else if(this.dragType === 'files'){
let deleteItemIds = []
for(let collectItem of this.fileCollectList){
deleteItemIds.push(collectItem.id)
this.dataList[index].filelist.push(collectItem)
this.checkList = []
}
this.dataList[this.activeTabIndex].filelist = this.dataList[this.activeTabIndex].filelist.filter(item => !deleteItemIds.includes(item.id))
//this.activeTabIndex = index
}else if(this.dragType === 'tab'){
let temp = this.dataList[index]
this.dataList[index] = this.dataList[this.sourceFileIndex]
this.dataList[this.sourceFileIndex] = temp
}
}总结:第一种方式,结合HTML5原生拖拽事件可以实现第三个功能点,但我都使用了HTML5原生拖拽事件了,为啥还要用你vuedraggable。反正个人的原则是,要么用插件,要么自己写,改插件总感觉怪怪的,有点四不像。
在项目中,我是同时开放了这两种交互模式,可以自行选择其中一种进行操作。

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论