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é)束后對我們想要的元素進(jìn)行操作。比如拿到頭和尾部兩個(gè)索引。交換他們在數(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>以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Vue實(shí)現(xiàn)移動(dòng)端拖拽交換位置
- vue懸浮可拖拽懸浮按鈕的實(shí)例代碼
- vuedraggable+element ui實(shí)現(xiàn)頁面控件拖拽排序效果
- Vue 實(shí)現(xiàn)可視化拖拽頁面編輯器
- 基于Vue實(shí)現(xiàn)拖拽功能
- 利用Vue-draggable組件實(shí)現(xiàn)Vue項(xiàng)目中表格內(nèi)容的拖拽排序
- 基于Vue實(shí)現(xiàn)拖拽效果
- vue實(shí)現(xiàn)element-ui對話框可拖拽功能
- vue實(shí)現(xiàn)div拖拽互換位置
- vue實(shí)現(xiàn)拖拽交換位置
相關(guān)文章
詳解從零搭建 vue2 vue-router2 webpack3 工程
本篇文章主要介紹了詳解從零搭建 vue2 vue-router2 webpack3 工程,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11
vue通信方式EventBus的實(shí)現(xiàn)代碼詳解
這篇文章主要介紹了vue通信方法EventBus的實(shí)現(xiàn)代碼,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06
vue如何將后臺(tái)返回的數(shù)字轉(zhuǎn)換成對應(yīng)的文字
這篇文章主要介紹了vue如何將后臺(tái)返回的數(shù)字轉(zhuǎn)換成對應(yīng)的文字,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10

