Vue.draggable学习

draggable: 基于并提供Sortable.js的所有特性

demo案例: 可以拖拽和删除

<template>
    <div class="row">
        <div class="col-3" style="display: flex;width: 80%" >
            <draggable
                    class="dragArea list-group"
                    :list="list1"
                    :group="{ name: 'people', pull: 'clone', put: false }"
                    @change="log"
            >
                <div class="list-group-item" v-for="element in list1" :key="element.id">
                    {{ element.name }}
                </div>
            </draggable>
            <draggable
                    class="dragArea list-group"
                    :list="list2"
                    :group="{ name: 'people', pull: 'clone', put: false }"
                    @change="log"
            >
                <div class="list-group-item" v-for="element in list2" :key="element.id">
                    {{ element.name }}
                </div>
            </draggable>
        </div>

        <div class="p_down">
            <h3>Draggable 3</h3>
            <template v-for="(item,index) in tableList">
                <div class="col-3" id="`##${index}`">
                    <draggable
                            class="dragArea list-group"
                            :id="index"
                            :list="item"
                            group="people"
                            @change="log"
                            @end="handleDragEnd"
                            :move = "handleRemoved"
                    >
                        <div class="list-group-item" v-for="element in item" :key="element.id">
                            {{ element.name }}
                        </div>
                    </draggable>
                </div>
            </template>
        </div>
        <el-button @click="handleClick">保存</el-button>

    </div>
</template>

<script>
    import draggable from "vuedraggable";
    // let idGlobal = 8;
    export default {
        name: "custom-clone",
        display: "Custom Clone",
        order: 3,
        components: {
            draggable
        },
        data() {
            return {
                list1: [
                    { name: "dog 1", id: 1 },
                    { name: "dog 2", id: 2 },
                    { name: "dog 3", id: 3 },
                    { name: "dog 4", id: 4 }
                ],
                list2: [
                    { name: "cat 5", id: 5 },
                    { name: "cat 6", id: 6 },
                    { name: "cat 7", id: 7 }
                ],
                list3: [
                    { name: "cat 5", id: 8 }
                ],
                commonList: [
                  {
                      list1: [[],[]]
                  },
                    {
                        list2: [
                            [
                                { name: "cat 5", id: 5 },
                                { name: "cat 6", id: 6 },
                            ],
                            [
                                { name: "cat 5", id: 8 }
                            ]
                        ]
                    }
                ],
                tableList: []
            };
        },
        mounted() {
            this.tableList = this.commonList[0].list1
        },
        methods: {
            log: function(evt) {
                window.console.log('cc',evt);

            },
            // cloneDog({ id }) {
            //     return {
            //         id: id,
            //         name: `cat ${id}`
            //     };
            // },
            cloneDog(evt) {
                var origEl = evt.item;
                var cloneEl = evt.clone;
                console.log("origEl",origEl)
                console.log("cloneEl",cloneEl)
                return origEl
            },
            handleDragEnd: function (evt,item) {
                console.log("evt-------")
                console.log(evt)
                console.log(evt.to.id)
                console.log(evt.from)
                console.log(evt.newIndex)
                console.log(evt.clone)
                console.log(evt.pullMode)
                // //
                // this.tableList.forEach(child => {
                //     for(let i = 0;i < child.length;i++) {
                //         if(i == evt.newDraggableIndex) {
                //             child.splice(evt.newDraggableIndex, 1)
                //         }
                //     }
                // })
                let currentId = evt.to.id
                let arr = this.tableList
                for(let i = 0;i < arr.length; i++) {
                    if(i == currentId) {
                        for(let j = 0;j < arr[i].length;j++) {
                            if(j == evt.newDraggableIndex) {
                                arr[i].splice(evt.newDraggableIndex, 1)
                            }
                        }
                    }
                }
            },
            handleRemoved(evt) {
                console.log(evt.draggedContext.index)
                console.log(evt.draggedContext.element)
                console.log(evt.draggedContext.futureIndex)
                console.log(evt.relatedContext.index)
                console.log(evt.relatedContext.element)
                console.log(evt.relatedContext.list)
                console.log(evt.relatedContext.component)
                return (evt.draggedContext.element.name!== 'b')
            },

            handleClick () {
                console.log(this.tableList);
            }
            // handleAdd: function (evt) {
            //     this.tableList.forEach(child => {
            //         for(let i = 0;i < child.length;i++) {
            //             if(i == evt.oldDraggableIndex) {
            //                 this.$message({
            //                     type: 'warning',
            //                     message: '不能重复添加'
            //                 })
            //                 return
            //             }
            //         }
            //     })
            // },

        }
    };
</script>
<style scoped lang="scss">
    .row {
        width: 100%;
        height: 800px;
        background: #FFFFFF;
        padding: 20px;

    }
    .col-3 {
        font-size: 18px;
        width: 50%;
        .dragArea {
            width: 100%;
            height: 300px;
            padding: 10px;
            display: flex;
        }
        .list-group-item {
            width: 10%;
            margin-top: 2%;
            margin: 2% 10px;
            display: inline-block;
        }
    }
    .p_down {
        width: 100%;
        height: 200px;
        display: flex;

        .col-3 {
            font-size: 18px;
            width: 50%;
            margin-left: 2%;
            border-left: 1px solid #cccccc;
            .dragArea {
                width: 100%;
                height: 300px;
                padding: 10px;
                display: flex;
            }
            .list-group-item {
                width: 10%;
                margin-top: 2%;
                margin: 2% 10px;
                display: inline-block;
            }
        }
    }
</style>

 

案例浏览:

https://sortablejs.github.io/Vue.Draggable/

https://david-desmaisons.github.io/draggable-example/

 

github上地址: https://github.com/SortableJS/Vue.Draggable

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值