0

我有一个使用 Vue Draggable 的 vue 组件。

<template>
  <div class="row my-5">
    <div v-for="column in columns" :key="column.title" class="col">
      <p class="font-weight-bold text-uppercase">{{column.title}}</p>
      <!-- Draggable component comes from vuedraggable. It provides drag & drop functionality -->
      <draggable :list="column.tasks" :animation="200" ghost-class="ghost-card" group="tasks" :move="checkMove">
        <transition-group>        
          <task-card
            v-for="(task) in column.tasks"
            :key="task.id"
            :task="task"
            class="mt-3 cursor-move"
          ></task-card>
          <!-- </transition-group> -->
        </transition-group>
      </draggable>
    </div>
  </div>
</template>


<script>
import draggable from "vuedraggable";
import TaskCard from "../board/TaskCard";
export default {
  name: "App",
  components: {
    TaskCard,
    draggable,
  },
 data() {
    return {
      columns: [
                 .....
             ],
    };
  },
  methods: {
   checkMove: function(evt){   
    console.log('moved');
}
  },
};
</script>

在任务卡组件中 -

<template>
  <div class="bg-white shadow rounded p-3 border border-white">
    <div class="d-flex justify-content-between align-items-center mb-3">
      <h2>{{task.id}}</h2>
      <span>{{task.date}}</span>
    </div>
    <p class="font-weight-bold">{{task.title}}</p>
  </div>
</template>
<script>
export default {
  props: {
    task: {
      type: Object,
      default: () => ({}),
    },
  },
};
</script>


当我移动一个项目时,我想要一个确认更改的模式,然后才移动该项目。(即,如果我单击模式内的取消按钮,则不应移动该项目。)

使用提供的 checkMove() 函数如何实现这一点?

4

1 回答 1

1

我不认为你可以通过使用onMoveevent. 该onEnd事件似乎更合适,但不幸的是它没有任何取消删除功能。

所以我认为这里唯一的解决方案是如果用户决定取消,则将其还原。

您可以收听change事件(请参阅文档中的更多内容)

<draggable
  group="tasks"
  v-model="column.tasks"
  @change="handleChange($event, column.tasks)">
  ...
</draggable>
...
<button @click="revertChanges">Cancel</button>
<button @click="clearChanges">Yes</button>

...
  handleChange(event, list) {
    this.changes.push({ event, list })
    this.modal = true
  },

  clearChanges() {
    this.changes = []
    this.modal = false
  },

  revertChanges() {
    this.changes.forEach(({ event, list }) => {
      if (event.added) {
        let { newIndex } = event.added
        list.splice(newIndex, 1)
      }

      if (event.removed) {
        let { oldIndex, element } = event.removed
        list.splice(oldIndex, 0, element)
      }

      if (event.moved) {
        let { newIndex, oldIndex, element } = event.moved
        list[newIndex] = list[oldIndex]
        list[oldIndex] = element
      }
    })

    this.changes = []
    this.modal = false
  }
...

JSFiddle示例

于 2020-09-03T09:34:11.403 回答