Vue可拖动-拖放后动态更改组件

时间:2019-05-21 18:38:18

标签: vue.js vuedraggable

我有两个单独的dragAreas,当我将组件从一个区域拖到另一个区域时,如何根据被拖动到的区域动态更改组件的状态?

当前,我正在从序列化器渲染状态,但我想在拖放后动态更改它。

<draggable
  class="dragArea published"
  :options="{ group: { name: 'g1', put: 'g1'}, animation: 120 }"
  @end="onEnd">
    <div
      v-for="lesson in classroomLessonsPublished"
      :class="`lesson-card ${lesson.id}`"
      data-status="published">   
        <div> 
          {{ lesson.status }}
        </div>
    </div>
</draggable>

<draggable
  class="dragArea unpublished"
  :options="{ group: { name: 'g1', put: 'g1'}, animation: 120 }"
  @end="onEnd">
    <div
      v-for="lesson in classroomLessonsUnpublished"
      :class="`lesson-card ${lesson.id}`"
      data-status="unpublished">   
        <div> 
          {{ lesson.status }}
        </div>
   </div>
</draggable>

1 个答案:

答案 0 :(得分:0)

draggable组件发出一个change事件:

  

change事件在列表属性不为null并且   拖放操作会更改相应的数组。

传递给事件的参数具有名为added的属性,该属性包含添加到数组中的元素的信息。

{
  newIndex: number, // the index of the added element
  element: T // the added element,
}

示例:将元素从已发布组拖放到未发布组。

模板:

<draggable
  class="dragArea unpublished"
  :options="{ group: { name: 'g1', put: 'g1'}, animation: 120 }"
  @change="onUnpublishedChange"
  @end="onEnd"
>
  <!-- ... -->
</draggable>

脚本:

 methods: {
    onUnpublishedChange({ added }) {
      if (added) {
        added.element.status= false // Not published
      }
    },
  }

您的群组名称相同,您应该将其更改为:

已发布:

:options="{ group: { name: 'published', put: 'unpublished'}, animation: 120 }"

未发布:

:options="{ group: { name: 'unpublished', put: 'published'}, animation: 120 }"