【问题标题】:Why element of a box not moving from one box to another while dragging and dropping?为什么一个盒子的元素在拖放时不会从一个盒子移动到另一个盒子?
【发布时间】:2020-10-19 13:46:59
【问题描述】:

我正在使用 Vue 可拖动元素,当将一个元素从一个框拖到另一个框时,它会移动到同一个框,但不能在其他框中拖放/移动。假设我想将 '1' 从框 1 移动到框 2。它不可拖动到另一个框。为了显示数据在数组中的方式,我使用了显示数据按钮来记录数据。检查元素的顺序。

这里有什么问题?

VUE 代码

<template>
  <div id="app">
    <!-- LOG ALL DATA -->
    <button type="button" name="button" @click="showData()">show data</button>

    <div class="container">
      <draggable :list="numbers[index]" class="box" v-for="(array, index) in numbers">
        <div class="heading">
          box {{ index  + 1}}
        </div>
          <div class="box-data" v-for="x in array">
            {{ x }}
          </div>
      </draggable>
    </div>

  </div>
</template>

<script>
 import draggable from 'vuedraggable'

export default {
  data(){
    return {
      numbers: [
        [1, 2, 3, 4, 5],
        [6, 7, 8],
        [9, 10, 11, 12]
      ],
    }
  },
  components: {
    draggable,
  },
  methods: {
    showData() {
        console.table(this.numbers);
    },
  }
}
</script>

<style>

  #app{
    height: 100%;
  }

  html, body{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    height: 100%;
  }

  button{
    padding: 1rem;
    margin: 1rem;
    background-color: #984636;
    color: white;
  }

  .container{
      background-color: bisque;
      padding: 16px;
      margin: 16px;
      height: 100%;
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      align-items: center;
  }
  .box{
    width: 300px;
    height: 100%;
    background-color: pink;
  }

  .box-data{
    padding: .5rem;
    margin: 1rem;
    background-color: #f6f7f8;
  }
  .heading{
    text-align: center;
    margin-top: 1rem;
  }
</style>

【问题讨论】:

    标签: vue.js vuejs2 draggable vuedraggable vue.draggable


    【解决方案1】:

    为可拖动组件添加一个prop group="boxes"

    【讨论】:

    • 在底层,Vue.Draggable 使用了 Sortable.js。如果要添加支持多个列表,则需要在可排序实例中提供 group 选项:有关更多信息,请参阅docs
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-24
    • 1970-01-01
    • 1970-01-01
    • 2017-02-04
    • 1970-01-01
    • 2016-04-12
    • 1970-01-01
    相关资源
    最近更新 更多