意见箱
恒创运营部门将仔细参阅您的意见和建议,必要时将通过预留邮箱与您保持联络。感谢您的支持!
意见/建议
提交建议

sortable事件监听怎么设置

来源:佚名 编辑:佚名
2024-06-27 14:09:45

在Vue中使用sortable插件来实现拖拽排序时,可以通过在组件中添加sortable的事件监听方法来设置事件监听。以下是一个简单的示例:

<template>
  <div>
    <ul ref="sortableList">
      <li v-for="(item, index) in items" :key="item.id">
        {{ item.name }}
      </li>
    </ul>
  </div>
</template>

<script>
import Sortable from 'sortablejs';

export default {
  data() {
    return {
      items: [
        { id: 1, name: 'Item 1' },
        { id: 2, name: 'Item 2' },
        { id: 3, name: 'Item 3' },
      ]
    };
  },
  mounted() {
    const sortableList = new Sortable(this.$refs.sortableList, {
      onEnd: (evt) => {
        // evt.oldIndex is the old index of the dragged element
        // evt.newIndex is the new index of the dragged element
        // You can update the items array based on these indexes
      }
    });
  }
};
</script>

在上面的示例中,我们在组件的mounted生命周期钩子中实例化了Sortable,并设置了onEnd事件监听方法。在onEnd事件中,我们可以获取到被拖拽元素的旧索引和新索引,并根据这些索引更新items数组。


sortable事件监听怎么设置

本网站发布或转载的文章均来自网络,其原创性以及文中表达的观点和判断不代表本网站。
上一篇: sortable在React项目中的应用 下一篇: sortable与React Hooks配合使用