vuejs2.0使用Sortable.js实现的拖拽功能


简介

http://www.cnblogs.com/moqiutao/p/6423754.html在使用vue1.x之前的版本的时候,页面中的拖拽功能,我在项目中是直接用的jQuery ui中的sortable.js,只是在拖拽完成后,在update的回调函数中又重新排序了存放数据的数组。但是当把vue升级到2.0以上后发现拖拽功能失效了,于是使用了下面代码。

该案例主要是在用于vuejs2.0中实现的拖拽功能,用到的的js有Sortable.js,vuedraggable.js,当然还有vue.min.js,提供的案例使用的require.js加载。

实现效果

实现后的效果如图所示:

html主要代码

复制代码
<draggable :list="list2" :move="getdata" @update="datadragEnd" :options="{animation: 300,handle:'.dargDiv'}">
        <transition-group name="list-complete" >
            <div v-for="element in list2" :key="element.it.name"  class="list-complete-item">
                <div class="styleclass dargDiv">{{element.id}}</div>
                <div class="styleclass">{{element.it.name}}</div>
            </div>
        </transition-group>
    </draggable>
复制代码

css代码

复制代码
 body{
    font-family:'微软雅黑'
}
[v-cloak]{
    display:none;
}
#example{
    width:1000px;
    margin:0 auto;
}
.list-complete-item {
  transition: all 1s;
    height:50px;
    line-height: 50px;
    background: #000;
    color:#fff;
    text-align: center;
    font-size:24px;
    margin-top:10px;
}
.styleclass{
    width:100px;
    float:left;
}
.list-complete-enter, .list-complete-leave-active {
  opacity: 0;
  height: 0px;
  margin-top: 0px;
  padding: 0px;
  border: solid 0px;
}
.list-complete-sortable-chosen,.list-complete-sortable-ghost{
 opacity: 0;
  height: 0px;
  margin-top: 0px;
  padding: 0px;
  border: solid 0px;
}
.dargDiv{
    cursor:move;
    background:red;
}
.wrods{
    margin-top:50px;
}
p{
    line-height:24px;
    text-align:center;
}
复制代码

js代码

复制代码
require.config({
    urlArgs: "ver=1.0_0",
    
    paths:{
        "vue":'vue.min2',
        "sortablejs":'Sortable',
        "vuedraggable":'vuedraggable'    
    },
    shim:{
        'vue':{
            exports:'vue'
        } 
    }
}),

require(['vue','vuedraggable'],function(Vue,draggable){
    Vue.component('draggable', draggable);
     new Vue({
        el: '#example',
        data: {
           list2:[
           {id:"id1",it:{name:'bbbb'}},
           {id:"id2",it:{name:'2222'}},
           {id:"id3",it:{name:'3333'}},
           {id:"id4",it:{name:'4444'}}
           ]
        },
        methods:{
            getdata: function(evt){
                console.log(evt.draggedContext.element.id);
            },
            datadragEnd:function(evt){
                console.log('拖动前的索引:'+evt.oldIndex);
                console.log('拖动后的索引:'+evt.newIndex);
                
            }
            
        }
    })
    
})
复制代码

里面的可配置的很多细节请参考参考地址,这里不做详细介绍。

页面展示地址:https://hxlmqtily1314.github.io/Vue.Draggable-case/
github地址:https://github.com/hxlmqtily1314/Vue.Draggable-case

参考地址:https://github.com/SortableJS/Vue.Draggable


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值