使用https://github.com/SortableJS/Vue.Draggable
我有一个关于如何实现将文件拖动到文件夹的经典示例的问题.
我看到@change事件获取有关被拖动项目的数据,但没有关于被拖动项目的数据.
关于如何实现这一点的任何想法?
这是一个看起来或多或少的例子,但显然我希望能够在onFoldersChange中重新分配file.folder_name – 不知何故.
https://jsfiddle.net/u5nb48cs/2/
谢谢!
解决方法:
如果您希望文件夹包含文件,则每个文件夹都需要能够保存文件并成为可拖动的目标.
var vm = new Vue({
el: "#main",
data: {
folders: [{
name: "Folder1",
contents: []
}, {
name: "Folder2",
contents: []
}, {
name: "Folder3",
contents: []
}],
files: [{
name: "foo",
folder_name: "Folder1"
}, {
name: "bar",
folder_name: 'Folder2'
}]
},
methods: {
onFoldersChange() {
console.log(arguments);
}
}
});
.dragArea {
min-height: 10px;
}
<script src="//cdn.jsdelivr.net/sortable/1.4.2/Sortable.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/vue/2.4.2/vue.min.js"></script>
<script src="//cdn.rawgit.com/David-Desmaisons/Vue.Draggable/master/dist/vuedraggable.min.js"></script>
<div id="main">
<h1>Vue Draggable</h1>
<div class="drag">
<h2>Folders</h2>
<div v-for="(element, index) in folders" :key="index">{{element.name}}
<draggable v-model="element.contents" class="dragArea" :options="{group:{ put:'files'}}" @change="onFoldersChange">
<div v-for="item in element.contents">
{{item.name}}
</div>
</draggable>
</div>
<h2>Files</h2>
<draggable v-model="files" class="dragArea" :options="{group:{ name:'files'}}">
<div v-for="(element, index) in files" :key="index">{{element.name}}</div>
</draggable>
</div>
</div>