安装
npm install vuedraggable --save # vue2
npm install vuedraggable@next --save # vue3
注册
基本用法只需要在 Vue 项目中导入vuedraggable
组件并注册即可:
import draggable from 'vuedraggable'
export default {
components: {
draggable
}
}
然后在模板中使用它:
<draggable v-model="list">
<div v-for="element in list" class="list-element">{{element}}</div>
</draggable>
.list-element {
padding: 10px;
background: #eee;
}
这将使这些列表项可拖拽排序。
示例代码:
示例一:
<template> <div class="row"> <div class="col-2"> <div class="form-group"> <div class="btn-group-vertical buttons" role="group" aria-label="Basic example" > <button class="btn btn-secondary" @click="add">Add</button> <button class="btn btn-secondary" @click="replace">Replace</button> </div> <div class="form-check"> <input id="disabled" type="checkbox" v-model="enabled" class="form-check-input" /> <label class="form-check-label" for="disabled">DnD enabled</label> </div> </div> </div> <div class="col-6"> <h3>Draggable {{ draggingInfo }}</h3> <draggable :list="list" :disabled="!enabled" class="list-group" ghost-class="ghost" :move="checkMove" @start="dragging = true" @end="dragging = false" > <div class="list-group-item" v-for="element in list" :key="element.name" > {{ element.name }} </div> </draggable> </div> <rawDisplayer class="col-3" :value="list" title="List" /> </div> </template> <script> import draggable from "@/vuedraggable"; let id = 1; export default { name: "simple", display: "Simple", order: 0, components: { draggable }, data() { return { enabled: true, list: [ { name: "John", id: 0 }, { name: "Joao", id: 1 }, { name: "Jean", id: 2 } ], dragging: false }; }, computed: { draggingInfo() { return this.dragging ? "under drag" : ""; } }, methods: { add: function() { this.list.push({ name: "Juan " + id, id: id++ }); }, replace: function() { this.list = [{ name: "Edgard", id: id++ }]; }, checkMove: function(e) { window.console.log("Future index: " + e.draggedContext.futureIndex); } } }; </script> <style scoped> .buttons { margin-top: 35px; } .ghost { opacity: 0.5; background: #c8ebfb; } </style>
示例二:
<template> <div class="row"> <div class="col-3"> <h3>Draggable 1</h3> <draggable class="list-group" :list="list1" group="people" @change="log" itemKey="name" > <template #item="{ element, index }"> <div class="list-group-item">{{ element.name }} {{ index }}</div> </template> </draggable> </div> <div class="col-3"> <h3>Draggable 2</h3> <draggable class="list-group" :list="list2" group="people" @change="log" itemKey="name" > <template #item="{ element, index }"> <div class="list-group-item">{{ element.name }} {{ index }}</div> </template> </draggable> </div> <rawDisplayer class="col-3" :value="list1" title="List 1" /> <rawDisplayer class="col-3" :value="list2" title="List 2" /> </div> </template> <script> import draggable from "@/vuedraggable"; export default { name: "two-lists", display: "Two Lists", order: 1, components: { draggable }, data() { return { list1: [ { name: "John", id: 1 }, { name: "Joao", id: 2 }, { name: "Jean", id: 3 }, { name: "Gerard", id: 4 } ], list2: [ { name: "Juan", id: 5 }, { name: "Edgard", id: 6 }, { name: "Johnson", id: 7 } ] }; }, methods: { add: function() { this.list.push({ name: "Juan" }); }, replace: function() { this.list = [{ name: "Edgard" }]; }, clone: function(el) { return { name: el.name + " cloned" }; }, log: function(evt) { window.console.log(evt); } } }; </script>
示例三:可拖拽表格
<template> <div class="row"> <div class="col-8"> <h3>Draggable table</h3> <table class="table table-striped"> <thead class="thead-dark"> <tr> <th scope="col">Id</th> <th scope="col">Name</th> <th scope="col">Sport</th> </tr> </thead> <draggable v-model="list" tag="tbody" item-key="name"> <template #item="{ element }"> <tr> <td scope="row">{{ element.id }}</td> <td>{{ element.name }}</td> <td>{{ element.sport }}</td> </tr> </template> </draggable> </table> </div> <rawDisplayer class="col-3" :value="list" title="List" /> </div> </template> <script> import draggable from "@/vuedraggable"; export default { name: "table-example", display: "Table", order: 8, components: { draggable }, data() { return { list: [ { id: 1, name: "Abby", sport: "basket" }, { id: 2, name: "Brooke", sport: "foot" }, { id: 3, name: "Courtenay", sport: "volley" }, { id: 4, name: "David", sport: "rugby" } ], dragging: false }; } }; </script> <style scoped> .buttons { margin-top: 35px; } </style>
查看更多,参考原文:手把手带你过一遍vue拖拽插件vuedraggable所有功能 - 掘金