嵌套的vue-draggable元素

问题描述 投票:1回答:1

我试图使用嵌套的vue-draggable元素来直观地表示歌曲结构(具有潜在的重复性)。

这就是我想出的原型:

var vm = new Vue({
  el: "#main",
  data: {
    "structure": ["Prelude", "Verse", ["Chorus", "Verse"], "Last Chorus"]
  },
});
#main {
  text-align: center;
  width: 300px;
  background-color: #EEE;
  padding:10px;
}

.element {
  text-align: center;
  padding: 10px;
  margin: 5px auto;
  border-radius: 10px;
  color: #FFF;
  font-family: sans-serif;
  font-weight: bold;
}

.tag {
  width: 150px;
  background-color: #007BFF;
}

.group {
  width: 175px;
  border: 3px solid #CED4DA;
  background-color: #FFF;
}
<script src="//cdnjs.cloudflare.com/ajax/libs/vue/2.5.2/vue.min.js"></script>
<script src="//cdn.jsdelivr.net/npm/[email protected]/Sortable.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/Vue.Draggable/2.15.0/vuedraggable.min.js"></script>

<div id="main">
  <draggable v-for="tag in structure" :options="{group:'tags'}">
    <div v-if="!Array.isArray(tag)" class="tag element">
      {{tag}}
    </div>
    <draggable v-else :options="{group:'tags'}" class="group element">
      <div v-for="tag2 in tag" class="tag element">
        {{tag2}}
      </div>
    </draggable>
  </draggable>
  {{structure}}
</div>

即使是Vue.js的新手,这似乎也不是“走路”。我解决这个问题的具体问题是:

  • 当分组元素位于顶部时,我无法拖动其上方的任何其他内容(同样适用于最底层)
  • 拖动的结构不会在data.structure属性中表示
  • 我怎么去筑巢呢?小组在小组中......
vue.js vuejs2 draggable
1个回答
0
投票

你应该:

1)将数据结构更改为递归数据结构:

 data: {
    "structure": [
       { name: "Prelude"}, 
       { name: "Verse"}, 
       { name: "Middle", children: [{ name: "Chorus"}, { name: "Verse"}]}, 
       { name: "Last Chorus"}
    ]
  },

2)使用draggable使用递归组件,例如:

<template>
  <draggable class="dragArea" tag="ul" :list="data" :group="{ name: 'g1' }">
    <li v-for="el in children" :key="el.name">
      <p>{{ el.name }}</p>
      <nested-draggable v-if="el.children" :tasks="el.children" />
    </li>
  </draggable>
</template>

看这个工作示例:

https://sortablejs.github.io/Vue.Draggable/#/nested-example

© www.soinside.com 2019 - 2024. All rights reserved.