在使用vue1.x以前的版本的時候,頁面中的拖拽功能,我在項目中是直接用的jquery ui中的sortable.js,只是在拖拽完成後,在update的回調函數中又從新排序了存放數據的數組。可是當把vue升級到2.0以上後發現拖拽功能失效了,因而使用了下面代碼。css
該案例主要是在用於vuejs2.0中實現的拖拽功能,用到的的js有Sortable.js,vuedraggable.js,固然還有vue.min.js,提供的案例使用的require.js加載。html
實現後的效果如圖所示:vue
<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>
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; }
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); } } }) })
裏面的可配置的不少細節請參考參考地址,這裏不作詳細介紹。jquery
頁面展現地址:https://hxlmqtily1314.github.io/Vue.Draggable-case/
github地址:https://github.com/hxlmqtily1314/Vue.Draggable-casegit
參考地址:https://github.com/SortableJS/Vue.Draggablegithub