Joao
Jean
Johanna
Juan
[ { "name": "Joao", "id": 1 }, { "name": "Jean", "id": 2 }, { "name": "Johanna", "id": 3 }, { "name": "Juan", "id": 4 } ]
Component usageUse components to complete drag and drop sorting
Single list drag and drop sorting, two way data binding.
[ { "name": "Joao", "id": 1 }, { "name": "Jean", "id": 2 }, { "name": "Johanna", "id": 3 }, { "name": "Juan", "id": 4 } ]
<template>
<button @click="start">start</button>
<button @click="pause">pause</button>
<button @click="disabled = true">disabled</button>
<div class="flex">
<VueDraggable
ref="el"
v-model="list"
:disabled="disabled"
:animation="150"
ghostClass="ghost"
class="flex flex-col gap-2 p-4 w-300px h-300px m-auto bg-gray-500/5 rounded"
@start="onStart"
@update="onUpdate"
@end="onEnd"
>
<div
v-for="item in list"
:key="item.id"
class="cursor-move h-30 bg-gray-500/5 rounded p-3"
>
{{ item.name }}
</div>
</VueDraggable>
<preview-list :list="list" />
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import {
type DraggableEvent,
type UseDraggableReturn,
VueDraggable
} from 'vue-draggable-plus'
const list = ref([
{
name: 'Joao',
id: 1
},
{
name: 'Jean',
id: 2
},
{
name: 'Johanna',
id: 3
},
{
name: 'Juan',
id: 4
}
])
const el = ref<UseDraggableReturn>()
const disabled = ref(false)
function pause() {
el.value?.pause()
}
function start() {
el.value?.start()
}
const onStart = (e: DraggableEvent) => {
console.log('start', e)
}
const onEnd = (e: DraggableEvent) => {
console.log('onEnd', e)
}
const onUpdate = () => {
console.log('update')
}
</script>
<style scoped>
.ghost {
opacity: 0.5;
background: #c8ebfb;
}
</style>
[ { "name": "Joao", "id": 1 }, { "name": "Jean", "id": 2 }, { "name": "Johanna", "id": 3 }, { "name": "Juan", "id": 4 } ]
<template>
<button @click="start()">start</button>
<div class="flex">
<div
class="flex flex-col gap-2 p-4 w-300px h-300px m-auto bg-gray-500/5 rounded"
ref="el"
>
<div
v-for="item in list"
:key="item.id"
class="h-30 bg-gray-500/5 rounded p-3 cursor-move"
>
{{ item.name }}
</div>
</div>
<preview-list :list="list" />
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import { useDraggable } from 'vue-draggable-plus'
const list = ref([
{
name: 'Joao',
id: 1
},
{
name: 'Jean',
id: 2
},
{
name: 'Johanna',
id: 3
},
{
name: 'Juan',
id: 4
}
])
const el = ref()
const { start } = useDraggable(el, list, {
animation: 150,
ghostClass: 'ghost',
onStart() {
console.log('start')
},
onUpdate() {
console.log('update')
}
})
</script>
<style scoped>
.ghost {
opacity: 0.5;
background: #c8ebfb;
}
</style>
[ { "name": "Joao", "id": 1 }, { "name": "Jean", "id": 2 }, { "name": "Johanna", "id": 3 }, { "name": "Juan", "id": 4 } ]
<template>
<div class="flex">
<ul
v-draggable="[
list,
{
animation: 150,
ghostClass: 'ghost',
onUpdate,
onStart
}
]"
class="target-directive flex flex-col gap-2 p-4 w-300px h-300px m-auto bg-gray-500/5 rounded"
>
<li
v-for="item in list"
:key="item.id"
class="h-30 bg-gray-500/5 rounded p-3 cursor-move"
>
{{ item.name }}
</li>
</ul>
<preview-list :list="list" />
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import { vDraggable } from 'vue-draggable-plus'
const list = ref([
{
name: 'Joao',
id: 1
},
{
name: 'Jean',
id: 2
},
{
name: 'Johanna',
id: 3
},
{
name: 'Juan',
id: 4
}
])
function onStart() {
console.log('start')
}
function onUpdate() {
console.log('update')
}
</script>
<style scoped>
.ghost {
opacity: 0.5;
background: #c8ebfb;
}
</style>