vue使用vuedraggable對(duì)列表進(jìn)行拖拽排序
vuedraggable 是一個(gè)基于 Vue 的拖拽排序組件,它可以讓你輕松地在 Vue 應(yīng)用中實(shí)現(xiàn)拖拽排序功能。vuedraggable 底層是基于 Sortable.js 實(shí)現(xiàn)的,它支持排序、拖拽、交換等操作。
安裝 vuedraggable
通過 npm 安裝
如果你使用的是 npm,可以通過以下命令安裝 vuedraggable:
npm install vuedraggable
通過 CDN 引入
如果你不想使用 npm,可以直接通過 CDN 引入:
<script src="https://cdn.jsdelivr.net/npm/vuedraggable@next"></script>
基本用法
以下是一個(gè)簡(jiǎn)單的 vuedraggable 使用示例,展示了如何在 Vue 組件中實(shí)現(xiàn)一個(gè)可拖拽排序的列表。
示例 1:基本的拖拽排序
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>vuedraggable 示例</title>
</head>
<body>
<div id="app">
<draggable v-model="items" :group="{ name: 'items' }" :animation="200">
<div v-for="item in items" :key="item.id" class="item">
{{ item.name }}
</div>
</draggable>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@next"></script>
<script src="https://cdn.jsdelivr.net/npm/vuedraggable@next"></script>
<script>
const app = Vue.createApp({
data() {
return {
items: [
{ id: 1, name: 'Item 1' },
{ id: 2, name: 'Item 2' },
{ id: 3, name: 'Item 3' },
{ id: 4, name: 'Item 4' },
],
};
},
});
app.mount("#app");
</script>
</body>
</html>
代碼解釋:
v-model="items":將組件的數(shù)據(jù)與父組件綁定。在此案例中,items 是一個(gè)數(shù)組,存儲(chǔ)了所有的列表項(xiàng)。
:group="{ name: 'items' }":指定拖拽排序的組。如果你有多個(gè) draggable 列表,并希望它們之間能夠拖拽排序,可以使用 group 來指定組。
:animation="200":設(shè)置拖拽時(shí)的動(dòng)畫效果,單位為毫秒。
進(jìn)階用法
示例 2:實(shí)現(xiàn)多個(gè)列表的拖拽排序(跨列表拖拽)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>vuedraggable 示例</title>
</head>
<body>
<div id="app">
<div class="list-container">
<div class="list">
<h3>List 1</h3>
<draggable v-model="list1" :group="{ name: 'items', pull: 'clone', put: true }">
<div v-for="item in list1" :key="item.id" class="item">
{{ item.name }}
</div>
</draggable>
</div>
<div class="list">
<h3>List 2</h3>
<draggable v-model="list2" :group="{ name: 'items', pull: false, put: true }">
<div v-for="item in list2" :key="item.id" class="item">
{{ item.name }}
</div>
</draggable>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@next"></script>
<script src="https://cdn.jsdelivr.net/npm/vuedraggable@next"></script>
<script>
const app = Vue.createApp({
data() {
return {
list1: [
{ id: 1, name: 'Item 1' },
{ id: 2, name: 'Item 2' },
],
list2: [
{ id: 3, name: 'Item 3' },
{ id: 4, name: 'Item 4' },
],
};
},
});
app.mount("#app");
</script>
<style>
.list-container {
display: flex;
gap: 20px;
}
.list {
width: 200px;
border: 1px solid #ccc;
padding: 10px;
}
.item {
padding: 5px;
margin: 5px;
background-color: #f0f0f0;
cursor: pointer;
}
</style>
</body>
</html>
代碼解釋:
在這個(gè)例子中,我們有兩個(gè)列表 list1 和 list2,并使用了 vuedraggable 來使得它們之間能夠?qū)崿F(xiàn)拖拽排序。
pull: 'clone' 表示你可以從 list1 拖動(dòng)項(xiàng)目并將其克隆到目標(biāo)列表(list2)。 put: true 允許你將項(xiàng)目放入目標(biāo)列表。
pull: false 表示不允許從 list2 拖動(dòng)項(xiàng)目到 list1。
常用屬性和事件
v-model:用于雙向綁定拖拽的數(shù)據(jù)。group:指定拖拽的組,支持name,pull,put屬性。name: 同一組的列表可以相互拖拽。pull: 定義該列表是否可以拖動(dòng)元素到其它列表(clone代表克隆元素)。put: 定義該列表是否允許接收元素。
animation:設(shè)置拖拽時(shí)的動(dòng)畫效果,單位為毫秒。disabled:禁用拖拽功能。ghostClass:設(shè)置拖拽過程中元素的 CSS 類。
事件:
@start:拖動(dòng)開始時(shí)觸發(fā)。@end:拖動(dòng)結(jié)束時(shí)觸發(fā)。@change:拖動(dòng)排序完成時(shí)觸發(fā)。
到此這篇關(guān)于vue使用vuedraggable對(duì)列表進(jìn)行拖拽排序的文章就介紹到這了,更多相關(guān)vue vuedraggable列表拖拽排序內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vuedraggable+element ui實(shí)現(xiàn)頁(yè)面控件拖拽排序效果
- vue拖拽排序插件vuedraggable使用方法詳解
- Vue?基于?vuedraggable?實(shí)現(xiàn)選中、拖拽、排序效果
- vue使用vuedraggable實(shí)現(xiàn)嵌套多層拖拽排序功能
- vue拖拽組件 vuedraggable API options實(shí)現(xiàn)盒子之間相互拖拽排序
- 使用element+vuedraggable實(shí)現(xiàn)圖片上傳拖拽排序
- Vue3使用vuedraggable實(shí)現(xiàn)拖拽排序功能實(shí)例代碼
相關(guān)文章
vuex中數(shù)據(jù)持久化插件vuex-persistedstate使用詳解
這篇文章主要介紹了vuex中數(shù)據(jù)持久化插件vuex-persistedstate使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
推薦一款簡(jiǎn)易的solid?js消息UI庫(kù)使用詳解
這篇文章主要為大家介紹了推薦一款簡(jiǎn)易的solid-js消息UI庫(kù)使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
vue3中安裝并使用CSS預(yù)處理器Sass的方法詳解
Sass是一種CSS預(yù)處理器,它擴(kuò)展了CSS的功能,提供了更高級(jí)的語(yǔ)法和特性,例如變量、嵌套、混合、繼承和顏色功能等,這些特性可以幫助開發(fā)者更高效地管理和維護(hù)樣式表,本文介紹vue3中安裝并使用CSS預(yù)處理器Sass的方法,感興趣的朋友一起看看吧2024-01-01
學(xué)習(xí)vue.js中class與style綁定
這篇文章主要和大家一起學(xué)習(xí)vue.js中class與style綁定操作,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
Vue2.x中的父組件傳遞數(shù)據(jù)至子組件的方法
這篇文章主要介紹了Vue2.x中的父組件數(shù)據(jù)傳遞至子組件的方法,需要的朋友可以參考下2017-05-05
vue之a(chǎn)-table中實(shí)現(xiàn)清空選中的數(shù)據(jù)
今天小編就為大家分享一篇vue之a(chǎn)-table中實(shí)現(xiàn)清空選中的數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11
Vue3封裝自動(dòng)滾動(dòng)列表指令(含網(wǎng)頁(yè)縮放滾動(dòng)問題)
本文主要介紹了Vue3封裝自動(dòng)滾動(dòng)列表指令(含網(wǎng)頁(yè)縮放滾動(dòng)問題),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
vue3如何實(shí)現(xiàn)表格內(nèi)容無縫滾動(dòng)(又寫了一堆冗余代碼)
這篇文章主要給大家介紹了關(guān)于vue3如何實(shí)現(xiàn)表格內(nèi)容無縫滾動(dòng)的相關(guān)資料,在Vue3項(xiàng)目中難免會(huì)遇到讓列表滾動(dòng)的需求,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-04-04
el-table-column疊加el-popover使用示例小結(jié)
el-table-column有一列展示多個(gè)tag信息,實(shí)現(xiàn)點(diǎn)擊tag展示tag信息以及tag對(duì)應(yīng)的詳細(xì)信息,本文通過示例代碼介紹el-table-column疊加el-popover使用示例小結(jié),感興趣的朋友跟隨小編一起看看吧2024-04-04

