1

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

目前,我正在从序列化程序呈现状态,但我想在拖放后动态更改它。

<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>
4

1 回答 1

3

draggable组件发出一个更改事件:

change当 list prop 不为 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 }"
于 2019-05-21T19:32:26.407 回答