sortable.js的应用

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <h1>{{ message }}</h1>
        <div>{{groupList}}</div>
        <div id="example1">
            <div class="list-group-edit" @click="handleEdit">{{isEdit ? '完成':'编辑'}}</div>
            <div class="list-group-item" v-for="(item,index) in groupList" :key="item.key" :data-id="item.key">
                <div>{{item.msg}}</div>
                <span class="delLog" v-show="isEdit" @click="delList(index)">x</span>
            </div>
        </div>
    </div>
    
</body>
</html>

<style>
    #app {
        width: 500px;
        margin: 100px auto;
    }
    #example1 {
        border: 1px gray solid;
        padding: 10px;
        display: flex;
        flex-wrap: wrap;
        position: relative;
    }
    .list-group-edit {
        cursor: pointer;
        position: absolute;
        top: 9;
        right: 0;
    }
    .list-group-item {
        margin: 5px;
        cursor: pointer;
        text-align: center;
        line-height: 100px;
        width: 100px;
        height: 100px;
        background-color: rgba(134, 134, 190, .5);
        position: relative;
    }
    .blue-background-class {
        background-color: rgba(83, 83, 136, 0.5);
    }
    .delLog {
        width: 5px;
        height: 5px;
        position: absolute;
        right: 5px;
        top: -45px;
    }
</style>

<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
<script src="./Sortable.min.js"></script>
<script>
    var app = new Vue({
        el: '#app',
        data: {
            message: '拖拽案列',
            groupList: [{icon: "", msg: "图标1"},{icon: "", msg: "图标2"},{icon: "", msg: "图标3"},{icon: "", msg: "图标4"},{icon: "", msg: "图标5"}],
            sortable: null,
            isEdit: false
        },
        created() {
            this.groupList.forEach((item, index) => {
                item.key = index + 1
            });
        },
        methods: {
            delList(index) {
                this.groupList.splice(index, 1)
            },
            handleEdit() {
                this.sortable.options.disabled = this.isEdit
                this.isEdit = !this.isEdit
            }
        },
        mounted() {
            this.sortable = new Sortable(example1, {
                disabled: true,
                animation: 100,
                ghostClass: 'blue-background-class',
                draggable: ".list-group-item",
                onEnd: (evt) => {
                    let newTable = this.sortable.toArray()
                    app.groupList.sort((a,b) =>  newTable.indexOf('' + a.key) - newTable.indexOf('' + b.key))
                }
            });
        }
    })
    
</script>

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值