最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue組件設計之多列表拖拽交換排序功能實現

 更新時間:2023年05月05日 09:57:57   作者:aiguangyuan  
這篇文章主要介紹了Vue組件設計之多列表拖拽交換排序,常見的場景有單列表拖拽排序,多列表拖拽交換排序,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

在前端開發(fā)中,拖拽排序是一種提升用戶體驗非常好的方式,常見的場景有單列表拖拽排序,多列表拖拽交換排序,比如以下這種效果:

 下面將以這種效果為例,設計一個組件。

1. 安裝所需依賴

npm install vuedraggable --save

本例中目前所用的版本為:2.20.0

2. 組件設計實現

<template>
    <div class="dnd-list">
        <div class="dnd-list-aside" :style="{width:width1 }" >
            <h3>{{ list1Title }}</h3>
            <draggable :list="list1" group="article" class="drag-area" :set-data="setData">
                <div :key="element.id" v-for="element in list1" class="list-complete-item">
                    <div class="list-complete-item-handle1">
                        {{ element.id }} {{ element.title }} [{{ element.author }}]
                    </div>
                    <div style="position:absolute;right:0px">
                        <span style="float:right;margin-top:-20px;margin-right:5px;" @click="deleteItem(element)">
                            <i style="color: #ff4949" class="el-icon-delete" />
                        </span>
                    </div>
                </div>
            </draggable>
        </div>
        <div class="dnd-list-aside" :style="{width:width2}">
            <h3>{{ list2Title }}</h3>
            <draggable :list="list2" group="article" class="drag-area">
                <div :key="element.id" v-for="element in list2" class="list-complete-item">
                    <div class="list-complete-item-handle2" @click="pushItem(element)">
                        {{ element.id }}  {{ element.title }} [{{ element.author }}]
                    </div>
                </div>
            </draggable>
        </div>
    </div>
</template>
<script>
import draggable from "vuedraggable";
export default {
    name: "DndList",
    components: { draggable },
    props: {
        list1: {
            type: Array,
            default() {
                return [];
            },
        },
        list2: {
            type: Array,
            default() {
                return [];
            },
        },
        list1Title: {
            type: String,
            default: "list1",
        },
        list2Title: {
            type: String,
            default: "list2",
        },
        width1: {
            type: String,
            default: "48%",
        },
        width2: {
            type: String,
            default: "48%",
        },
    },
    methods: {
        // 是否在列表一
        isNotInList1(v) {
            return this.list1.every((k) => v.id !== k.id);
        },
        // 是否在列表二
        isNotInList2(v) {
            return this.list2.every((k) => v.id !== k.id);
        },
        // 刪除列表項
        deleteItem(element) {
            for (const item of this.list1) {
                if (item.id === element.id) {
                    const index = this.list1.indexOf(item);
                    this.list1.splice(index, 1);
                    break;
                }
            }
            if (this.isNotInList2(element)) {
                this.list2.unshift(element);
            }
        },
        // 點擊切換列表項
        pushItem(element) {
            for (const item of this.list2) {
                if (item.id === element.id) {
                    const index = this.list2.indexOf(item);
                    this.list2.splice(index, 1);
                    break;
                }
            }
            if (this.isNotInList1(element)) {
                this.list1.push(element);
            }
        },
        // 拖拽交換時
        setData(dataTransfer) {
            // 解決火狐問題
            // 詳見 : https://github.com/RubaXa/Sortable/issues/1012
            dataTransfer.setData("Text", "");
        },
    },
};
</script>
<style lang="scss" scoped>
.dnd-list {
    background: #fff;
    padding-bottom: 40px;
    &:after {
        content: "";
        display: table;
        clear: both;
    }
    .dnd-list-aside {
        float:left;
        padding-bottom: 30px;
        &:first-of-type {
            margin-right: 2%;
        }
        .drag-area{
            margin-top: 15px;
            min-height: 50px;
            padding-bottom: 30px;
        }
    }
}
.list-complete-item {
    cursor: pointer;
    position: relative;
    font-size: 14px;
    padding: 5px 12px;
    margin-top: 4px;
    border: 1px solid #bfcbd9;
    transition: all 1s;
}
.list-complete-item-handle1 {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    margin-right: 50px;
}
.list-complete-item-handle2 {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    margin-right: 20px;
}
.list-complete-item.sortable-chosen {
    background: #4ab7bd;
}
.list-complete-item.sortable-ghost {
    background: #30b08f;
}
.list-complete-enter,.list-complete-leave-active {
    opacity: 0;
}
</style>

3. 組件使用示例

<template>
    <div class="box">
        <DndList :list1="list1" :list2="list2" :list1Title="list1Title" :list2Title="list2Title"></DndList>
    </div>
</template>
<script>
import DndList from "@/components/DndList";
export default {
    components:{
        DndList:DndList
    },
    data() {
        return {
            list1:[
                {id:1,title:"《西游記》",author:"吳承恩"},
                {id:2,title:"《紅樓夢》",author:"曹雪芹"},
                {id:3,title:"《水滸傳》",author:"施耐庵"},
                {id:4,title:"《三國演義》",author:"羅貫中"},
                {id:5,title:"《名人傳》",author:"羅曼羅蘭"},
                {id:6,title:"《鋼鐵是怎樣煉成的》",author:"奧斯特洛夫斯基"},
            ],
            list2:[
                {id:7,title:"《魯賓遜漂流記》",author:"笛福"},
                {id:8,title:"《格列佛游記》",author:"約翰斯威夫特"},
                {id:9,title:"《繁星春水》",author:"冰心"},
            ],
            list1Title:"我的圖書收藏",
            list2Title:"已讀完的圖書"
        };
    }
}
</script>
<style scoped>
.box{
    width:600px;
    margin:20px;
    padding:10px;
    background:#fff;
}
</style>

到此這篇關于Vue組件設計之多列表拖拽交換排序的文章就介紹到這了,更多相關vue拖拽交換排序內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • vue與django(drf)實現文件上傳下載功能全過程

    vue與django(drf)實現文件上傳下載功能全過程

    最近簡單的學習了django和drf上傳文件(主要是圖片),做一個記錄,下面這篇文章主要給大家介紹了關于vue與django(drf)實現文件上傳下載功能的相關資料,需要的朋友可以參考下
    2023-02-02
  • vue2滾動條加載更多數據實現代碼

    vue2滾動條加載更多數據實現代碼

    本篇文章主要介紹了vue2滾動條加載更多數據實現代碼,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01
  • vue下拉刷新組件的開發(fā)及slot的使用詳解

    vue下拉刷新組件的開發(fā)及slot的使用詳解

    這篇文章主要介紹了vue下拉刷新組件的開發(fā)及slot的使用詳解,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-12-12
  • Vue.js 中 ref 和 reactive 的區(qū)別及用法解析

    Vue.js 中 ref 和 reactive 的區(qū)別及用法解析

    在Vue.js中,ref主要用于創(chuàng)建響應式的引用,通過.value屬性來訪問和修改值,特別適用于需要頻繁更改整個值的場景,而reactive則用于創(chuàng)建深度響應的對象或數組,本文給大家介紹Vue.js 中 ref 和 reactive 的區(qū)別及用法,感興趣的朋友跟隨小編一起看看吧
    2024-09-09
  • 詳解Vue自定義過濾器的實現

    詳解Vue自定義過濾器的實現

    這篇文章主要介紹了詳解Vue自定義過濾器的實現,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧。
    2017-01-01
  • vue引入子組件命名不規(guī)范錯誤的解決方案

    vue引入子組件命名不規(guī)范錯誤的解決方案

    這篇文章主要介紹了vue引入子組件命名不規(guī)范錯誤的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • Vue express鑒權零基礎入門

    Vue express鑒權零基礎入門

    這篇文章主要介紹了詳解express鑒權,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-02-02
  • 解析vue中的$mount

    解析vue中的$mount

    本文主要是帶領大家分析$mount的相關知識,需要的朋友一起學習吧
    2017-12-12
  • vue封裝自定義分頁器組件與使用方法分享

    vue封裝自定義分頁器組件與使用方法分享

    這篇文章主要給大家介紹了關于vue封裝自定義分頁器組件與使用方法的相關資料,非常的好用,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2022-01-01
  • 詳解Vue取消eslint語法限制

    詳解Vue取消eslint語法限制

    本篇文章給大家分享了Vue取消eslint語法限制的相關知識點內容,有興趣的朋友們可以參考學習下。
    2018-08-08

最新評論

凤阳县| 三亚市| 五原县| 织金县| 禄劝| 徐州市| 塔河县| 法库县| 阳泉市| 库伦旗| 八宿县| 通城县| 新巴尔虎左旗| 资阳市| 忻城县| 平武县| 东明县| 富平县| 高安市| 竹北市| 建湖县| 丘北县| 阿鲁科尔沁旗| 外汇| 文登市| 连南| 长兴县| 东源县| 马龙县| 灵武市| 大余县| 南宁市| 苗栗市| 特克斯县| 宁陕县| 柳林县| 宁武县| 长治市| 古交市| 保山市| 金门县|