vue 2.0中如何使用sortable
来源:互联网 发布:mac os sierra 卡 编辑:程序博客网 时间:2024/06/07 23:01
vue 2.0中使用sortable会出现el 是标签元素,不是未定义的对象
vue 2.0开发项目中要实现拖拽排序的功能,推荐的用vue-sortable
网址:(https://github.com/RubaXa/Sortable)
安装步骤:
npm install vue-sortable
npm run dev
import Vue from 'vue'import Sortable from 'vue-sortable'Vue.use(Sortable)
在.vue文件的用法
<div id="box">
<h2>{{ title }}</h2>
<div id="items">
<div class="item">11111</div>
<div class="item">22222</div>
<div class="item">123</div>
<div class="item">123</div>
</div>
</div>
.vue中的js部分
import Sortable from 'vue-sortable';export default{ data: { title: 'Sortable items', items: [ {id: 1, title: 'Item 1'}, {id: 2, title: 'Item 2'}, {id: 3, title: 'Item 3'} ] }, mounted: function(){ var self = this; self.$nextTick(function(){ console.log(Sortable); var sortable = Sortable.create(document.getElementById('items'), { onEnd: function(e) { var clonedItems = self.items.filter(function(item){ return item; }); clonedItems.splice(e.newIndex, 0, clonedItems.splice(e.oldIndex, 1)[0]); self.items = []; self.$nextTick(function(){ self.items = clonedItems; }); } }); });}
在html文件的用法
<!DOCTYPE html><html><head> <!-- VueJS --> <script src="https://cdn.bootcss.com/vue/2.4.2/vue.min.js"></script> <!-- SortableJS --> <script src="https://unpkg.com/sortablejs@1.4.2"></script> <!-- VueSortable --> <!-- <script src="https://unpkg.com/vue-sortable@0.1.3"></script> --> <style> * { font-family: Arial;}.item{ padding: 10px; background: #ccc; margin: 10px;} </style></head><body id="app"> <div id="box"> <h2>{{ title }}</h2> <div id="items"> <div class="item">11111</div> <div class="item">22222</div> <div class="item">123</div> <div class="item">123</div> </div> <!-- <div><button v-on:click.stop.prevent="addItem">Add item</button></div> --></div> <script>window.box = new Vue({ el: '#box', data: { title: 'Sortable items', items: [ {id: 1, title: 'Item 1'}, {id: 2, title: 'Item 2'}, {id: 3, title: 'Item 3'} ] }, mounted: function(){ var self = this; self.$nextTick(function(){ console.log(Sortable); var sortable = Sortable.create(document.getElementById('items'), { onEnd: function(e) { var clonedItems = self.items.filter(function(item){ return item; }); clonedItems.splice(e.newIndex, 0, clonedItems.splice(e.oldIndex, 1)[0]); self.items = []; self.$nextTick(function(){ self.items = clonedItems; }); } }); }); }}); </script></body></html>
阅读全文
0 0
- vue 2.0中如何使用sortable
- 如何使用jQuery UI的sortable组件做一个更为友好的界面(中)
- Vue中如何使用less
- vue中如何使用less
- Vue中如何使用vue-resource获取端口数据
- 如何在Vue Project中使用vue-apollo
- 如何在vue中使用sass
- 在Vue中如何使用Cookie
- vue项目中如何使用字体图标
- 在Vue.js中如何使用Mixins?
- 如何在vue中使用sass
- 如何在vue中使用sass
- jquery sortable使用详解
- vue中使用vue-router
- sortable
- 学习如何使用Vue
- vue 如何使用websocket
- Vue.js 2.0 中#$on与$emit如何使用之实例讲解
- 判断一个整数的奇偶性
- 使用DebugDia定位解决内存泄漏
- Linux命令个人收藏版(持续更新……)
- Blog13@linux延迟、定时任务介绍及临时文件时间更改
- ckplayer 播放器参数详细设置
- vue 2.0中如何使用sortable
- 51Nod 1289 大鱼吃小鱼
- Android开发中java.lang.RuntimeException: Unable to start activity ComponentInfo{xxx}: java.lang.NullPoi
- Cocos2d C++与lua互相调用
- java代理
- android jni C++ java 相互调用
- [leetcode] 79. Word Search
- JavaScript鼠标事件2
- 简单的实现融云SDK集成