Vue.Draggable實現(xiàn)交換位置
Vue.Draggable實現(xiàn)交換位置,供大家參考,具體內(nèi)容如下
前言
最近的一個項目接觸了到了Vue.Draggable這個組件。不過我們的需求和Vue.Draggable這個組件所支持的有些出入。這個拖拽組件屬于插入的模式。一但拖拽的是相間的元素(中間隔著幾個元素),那么拖拽元素就會占據(jù)被拖拽元素的位置,而后續(xù)元素位置逐級向下減一。
如下圖:
c拖拽到a的位置,表現(xiàn)為c插入到a的前面。所以變?yōu)榱薱ab。
需求
實現(xiàn)交換而非插入及上圖要變成(cba)
實現(xiàn)方式
想要阻止它插入是不可能,我們只能等它插入結(jié)束后對我們想要的元素進行操作。比如拿到頭和尾部兩個索引。交換他們在數(shù)組中的位置。需要注意的是,因為拖拽時已經(jīng)改變數(shù)組里面元素的位置了,因此我們需要在拖拽前copy一個和原數(shù)組一樣的數(shù)組。
實現(xiàn)步驟
1、 選擇一個適合自己的方法,需要能夠獲得開始索引和結(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>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue3+vite中開發(fā)環(huán)境與生產(chǎn)環(huán)境全局變量配置指南
最近在使用vite生成項目,這篇文章主要給大家介紹了關(guān)于vue3+vite中開發(fā)環(huán)境與生產(chǎn)環(huán)境全局變量配置的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下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é)合示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-01-01深入淺析Vue.js中 computed和methods不同機制
這篇文章給大家介紹了Vue.js中 computed和methods不同機制,在vue.js中,methods和computed兩種方式來動態(tài)當作方法使用,文中還給大家提到了computed和methods的區(qū)別,感興趣的朋友一起看看吧2018-03-03vue + socket.io實現(xiàn)一個簡易聊天室示例代碼
本篇文章主要介紹了vue + socket.io實現(xiàn)一個簡易聊天室示例代碼,具有一定的參考價值,有興趣的可以了解一下。2017-03-03解決vue3項目打包發(fā)布到服務(wù)器后訪問頁面顯示空白問題
這篇文章主要介紹了解決vue3項目打包發(fā)布到服務(wù)器后訪問頁面顯示空白問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03Vue3中使用Element-Plus的el-upload組件限制只上傳一個文件的功能實現(xiàn)
在 Vue 3 中使用 Element-Plus 的 el-upload 組件進行文件上傳時,有時候需要限制只能上傳一個文件,本文將介紹如何通過配置 el-upload 組件實現(xiàn)這個功能,讓你的文件上傳變得更加簡潔和易用,需要的朋友可以參考下2023-10-10