draggable sort@end="dragEnd"

@end="dragEnd" usage in draggable plugin updates data

When using the draggable plug-in for drag sorting, @end="dragEnd" can be used to update data. The implementation is to obtain the sorting information after dragging in the dragEnd method, and then update the data source.

<template>
  <div>
    <h3>拖拽排序列表</h3>
    <draggable v-model="items" @end="dragEnd">
      <div v-for="(item, index) in items" :key="item.id">
        {
   
   { item.name }}
      </div>
    </draggable>
  </div>
</template>

<script>
import draggable from "vuedraggable";

export default {
  components: {
    draggable,
  },
  data() {
    return {
      items: [
        { id: 1, name: "apple" },
        { id: 2, name: "banana" },
        { id: 3, name: "cherry" },
      ],
    };
  },
  methods: {
    dragEnd(event) {
      //获取拖拽后数据的排序,更新数据源
      const newItems = event.newContainer.children.map((item, index) => {
        return { ...item.item, order: index + 1 };
      });
      this.items = newItems;
    },
  },
};
</script>

In the above example, we obtain the sorted data after dragging in the dragEnd method, and then reassign the data to items to update the data.

Guess you like

Origin blog.csdn.net/weixin_44727080/article/details/132582518