4

使用https://github.com/SortableJS/Vue.Draggable

我有一个关于如何实现将文件拖入文件夹的经典示例的问题。我看到该@change事件获取有关被拖动项目的数据,但不是关于它被拖动到的项目的数据。关于如何实现这一目标的任何想法?

这是一个或多或少的示例,但显然我希望能够以某种方式 重新分配file.folder_name内部。https://jsfiddle.net/u5nb48cs/2/onFoldersChange

谢谢!

4

1 回答 1

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>

于 2018-01-02T13:52:25.710 回答