Vue.Draggable實(shí)現(xiàn)交換位置
Vue.Draggable實(shí)現(xiàn)交換位置,供大家參考,具體內(nèi)容如下
前言
最近的一個(gè)項(xiàng)目接觸了到了Vue.Draggable這個(gè)組件。不過我們的需求和Vue.Draggable這個(gè)組件所支持的有些出入。這個(gè)拖拽組件屬于插入的模式。一但拖拽的是相間的元素(中間隔著幾個(gè)元素),那么拖拽元素就會(huì)占據(jù)被拖拽元素的位置,而后續(xù)元素位置逐級(jí)向下減一。
如下圖:
c拖拽到a的位置,表現(xiàn)為c插入到a的前面。所以變?yōu)榱薱ab。
需求
實(shí)現(xiàn)交換而非插入及上圖要變成(cba)
實(shí)現(xiàn)方式
想要阻止它插入是不可能,我們只能等它插入結(jié)束后對(duì)我們想要的元素進(jìn)行操作。比如拿到頭和尾部?jī)蓚€(gè)索引。交換他們?cè)跀?shù)組中的位置。需要注意的是,因?yàn)橥献r(shí)已經(jīng)改變數(shù)組里面元素的位置了,因此我們需要在拖拽前copy一個(gè)和原數(shù)組一樣的數(shù)組。
實(shí)現(xiàn)步驟
1、 選擇一個(gè)適合自己的方法,需要能夠獲得開始索引和結(jié)束索引
end,sort,update都可以
2、深拷貝
copyList(){ ? ?this.copyList=this.list.slice(0) }
3、通過索引來操作copyList數(shù)組位置
const item=this.copyList[oldIndex] this.copyList.splice(oldIndex, 1, this.copyList[newIndex]); this.copyList.splice(newIndex, 1, item);
4、將copyList賦值給list,并在結(jié)尾處獲得新的拷貝的copyList
this.list=this.copyList this.copyList = this.list.slice(0);
全部代碼
import draggable from "@/vuedraggable"; let id = 1; export default { ? name: "simple", ? display: "Simple", ? order: 0, ? components: { ? ? draggable, ? }, ? data() { ? ? return { ? ? ? enabled: true, ? ? ? list: [{ name: "a" }, { name: "b" }, { name: "c" }], ? ? ? dragging: false, ? ? ? index: 0, ? ? ? copyList: [], ? ? }; ? }, ? computed: { ? ? draggingInfo() { ? ? ? return this.dragging ? "under drag" : ""; ? ? }, ? }, ? created() { ? ? this.copyList = this.list.slice(0); ? }, ? methods: { ? ? add: function () { ? ? ? this.list.push({ name: "Juan " + id, id: id++ }); ? ? }, ? ? replace: function () { ? ? ? this.list = [{ name: "Edgard", id: id++ }]; ? ? }, ? ? end({ oldIndex, newIndex }) { ? ? ? const item = this.copyList[oldIndex]; ? ? ? this.copyList.splice(oldIndex, 1, this.copyList[newIndex]); ? ? ? this.copyList.splice(newIndex, 1, item); ? ? ? this.list = this.copyList; ? ? ? this.copyList = this.list.slice(0); ? ? }, ? } };
<draggable ? ? ? ? :list="list" ? ? ? ? :disabled="!enabled" ? ? ? ? class="list-group" ? ? ? ? ghost-class="ghost" ? ? ? ? :move="checkMove" ? ? ? ? @end="end" ? ? ? ? @sort="sort" ? ? ? ? @update="update" ? ? ? ? @start="start" ? ? ? > ? <div class="list-group-item" v-for="element in list" :key="element.name">{{ element.name }}</div> </draggable>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Vue實(shí)現(xiàn)移動(dòng)端拖拽交換位置
- vue懸浮可拖拽懸浮按鈕的實(shí)例代碼
- vuedraggable+element ui實(shí)現(xiàn)頁(yè)面控件拖拽排序效果
- Vue 實(shí)現(xiàn)可視化拖拽頁(yè)面編輯器
- 基于Vue實(shí)現(xiàn)拖拽功能
- 利用Vue-draggable組件實(shí)現(xiàn)Vue項(xiàng)目中表格內(nèi)容的拖拽排序
- 基于Vue實(shí)現(xiàn)拖拽效果
- vue實(shí)現(xiàn)element-ui對(duì)話框可拖拽功能
- vue實(shí)現(xiàn)div拖拽互換位置
- vue實(shí)現(xiàn)拖拽交換位置
相關(guān)文章
vue3+vite中開發(fā)環(huán)境與生產(chǎn)環(huán)境全局變量配置指南
最近在使用vite生成項(xiàng)目,這篇文章主要給大家介紹了關(guān)于vue3+vite中開發(fā)環(huán)境與生產(chǎn)環(huán)境全局變量配置的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08Vue3關(guān)于響應(yīng)式數(shù)據(jù)類型詳解(ref、reactive、toRef、及toRefs)
這篇文章主要介紹了Vue3關(guān)于響應(yīng)式數(shù)據(jù)類型(ref、reactive、toRef、以及toRefs),本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-01-01深入淺析Vue.js中 computed和methods不同機(jī)制
這篇文章給大家介紹了Vue.js中 computed和methods不同機(jī)制,在vue.js中,methods和computed兩種方式來動(dòng)態(tài)當(dāng)作方法使用,文中還給大家提到了computed和methods的區(qū)別,感興趣的朋友一起看看吧2018-03-03vue + socket.io實(shí)現(xiàn)一個(gè)簡(jiǎn)易聊天室示例代碼
本篇文章主要介紹了vue + socket.io實(shí)現(xiàn)一個(gè)簡(jiǎn)易聊天室示例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-03-03解決vue3項(xiàng)目打包發(fā)布到服務(wù)器后訪問頁(yè)面顯示空白問題
這篇文章主要介紹了解決vue3項(xiàng)目打包發(fā)布到服務(wù)器后訪問頁(yè)面顯示空白問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03Vue3中使用Element-Plus的el-upload組件限制只上傳一個(gè)文件的功能實(shí)現(xiàn)
在 Vue 3 中使用 Element-Plus 的 el-upload 組件進(jìn)行文件上傳時(shí),有時(shí)候需要限制只能上傳一個(gè)文件,本文將介紹如何通過配置 el-upload 組件實(shí)現(xiàn)這個(gè)功能,讓你的文件上傳變得更加簡(jiǎn)潔和易用,需要的朋友可以參考下2023-10-10vue下axios攔截器token刷新機(jī)制的實(shí)例代碼
這篇文章主要介紹了vue下axios攔截器token刷新機(jī)制的實(shí)例代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01