当前位置:   article > 正文

vue-draggable拖拽组件的学习和使用_draggable组件的使用

draggable组件的使用

效果图

安装 

npm install --save vuedraggable || yarn add vuedraggable

 引入组件

  1. //导入draggable组件
  2. import draggable from "vuedraggable";
  3. //注册draggable组件
  4. components: {
  5. draggable
  6. },

组件使用(不含后台数据接收返回处理)

  1. <template>
  2. <!--使用draggable组件-->
  3. <div class="dragCol" ref="dragCol">
  4. <div class="dragItem dragHeader" ref="dragHeader">
  5. <el-checkbox id="dragAll" v-model="checkAll" :indeterminate="isIndeterminate" @change="handleAllChecked"
  6. class="checkbox-common">{{
  7. this.vmodel.label
  8. }}
  9. </el-checkbox>
  10. <span class="svgIcon">
  11. <!-- <svg-icon icon-class="sort" style="cursor: pointer"></svg-icon> -->
  12. </span>
  13. </div>
  14. <!-- <div class="dragMain" style="height:dragMainHeight;"> -->
  15. <div class="dragMain" ref="dragMain" :style="{ height: mainHeight }">
  16. <draggable v-model="testList"
  17. :options="{ group: { name: 'itxst', pull: 'clone' }, sort: true }" animation="300"
  18. :move="onMove" dragClass="dragClass" ghostClass="ghostClass" chosenClass="chosenClass"
  19. @input="handleListChange($event)">
  20. <transition-group>
  21. <div class="dragItem canDragon" v-for="(item, index) in testList" :key="index" ref="dragItem">
  22. <el-checkbox v-model="item.checked" :disabled="item.locked" name="checkItem"
  23. @change="handleChecked(item, $event)" class="checkbox-common"> {{ item.displayName
  24. }}</el-checkbox>
  25. <span class="svgIcon">
  26. <img :src="iconSort" alt="" width="100%" />
  27. </span>
  28. </div>
  29. </transition-group>
  30. </draggable>
  31. </div>
  32. </div>
  33. </template>
  34. <script>
  35. //导入draggable组件
  36. import draggable from "vuedraggable";
  37. export default {
  38. //注册draggable组件
  39. name: "ct-dragSort",
  40. components: {
  41. draggable
  42. },
  43. props: {
  44. vmodel: Object,
  45. api: String
  46. },
  47. data() {
  48. return {
  49. iconSort: require("../../../assets/sort.png"),
  50. drag: false,
  51. checkAll: true,
  52. //定义要被拖拽对象的数组
  53. testList: [],
  54. mainHeight: 500,
  55. isIndeterminate: true
  56. };
  57. },
  58. watch: {
  59. testList: {
  60. // 监听事件,监听复选框是否全部选中,全部选中则全选的复选框勾选上
  61. handler(val) {
  62. var i = 0;
  63. this.testList.forEach(item => {
  64. if (item.checked === true) {
  65. i++;
  66. }
  67. if (i === this.testList.length) {
  68. this.checkAll = true;
  69. this.isIndeterminate = false
  70. } else {
  71. this.checkAll = false;
  72. if (i > 0 && i < this.testList.length) {
  73. this.isIndeterminate = true
  74. } else {
  75. this.isIndeterminate = false
  76. }
  77. }
  78. });
  79. },
  80. deep: true
  81. }
  82. },
  83. created() {
  84. this.$nextTick(function () {
  85. if (typeof this.vmodel === 'undefined') {
  86. this.model = this.loaderObj.DragSort(this.source);
  87. }
  88. else {
  89. this.model = this.vmodel;
  90. this.testList = this.vmodel.dragList;
  91. }
  92. });
  93. this.testList.forEach(item => {
  94. // 处理后端传过来的数据,如果没有可以判断是否勾选复选框的字段,则需给数据作处理,加上一个isChecked字段,判断复选框勾选
  95. this.$set(item, "checked", false); // 添加判断的字段
  96. });
  97. },
  98. mounted() {
  99. this.$nextTick(() => {
  100. this.sjcount();
  101. });
  102. },
  103. methods: {
  104. sjcount() {
  105. this.$nextTick(() => {
  106. // const height = window.innerHeight;
  107. let screenHeight = document.body.clientHeight;
  108. let dragHeaderHeight = this.$refs.dragHeader.offsetHeight;
  109. let dragMainHeight = this.$refs.dragMain.style.height;
  110. dragMainHeight =
  111. screenHeight - dragHeaderHeight - 26 - 8 - 44 - 40;
  112. this.mainHeight = dragMainHeight + "px";
  113. let num = this.testList.length
  114. let dragItemAll = 44.68 * num;
  115. if (dragMainHeight > dragItemAll) {
  116. this.mainHeight = dragItemAll + "px";
  117. this.$refs.dragMain.style.overflowY = 'hidden'
  118. } else {
  119. this.mainHeight = dragMainHeight + "px";
  120. this.$refs.dragMain.style.overflowY = 'overlay'
  121. }
  122. });
  123. },
  124. // 更新位置
  125. handleListChange(event) {
  126. this.testList = event;
  127. this.testList.forEach((item, i) => {
  128. this.model.dragList.splice(i, 1, item);
  129. });
  130. },
  131. //move回调方法
  132. onMove(e, originalEvent) { },
  133. //开始拖拽事件
  134. onStart() {
  135. this.drag = true;
  136. },
  137. //拖拽结束事件
  138. onEnd() {
  139. this.drag = false;
  140. },
  141. handleAllChecked(v) {
  142. // 实现全选,反选(点击会传递true或者false过来)
  143. this.testList.map(item => {
  144. if (!item.locked) {
  145. item.checked = v;
  146. } else {
  147. return false;
  148. }
  149. });
  150. this.vmodel.draglist = this.testList;
  151. },
  152. handleChecked(item, e) {
  153. // 单个选中
  154. item.checked = e;
  155. this.vmodel.draglist = this.testList;
  156. }
  157. }
  158. };
  159. </script>
  160. <style scoped>
  161. /*定义要拖拽元素的样式*/
  162. .ghostClass {
  163. background-color: #fff !important;
  164. }
  165. .chosenClass {
  166. background-color: #ccc !important;
  167. opacity: 1 !important;
  168. }
  169. /* 拖拽过程 */
  170. .dragClass {
  171. background-color: #fff !important;
  172. opacity: 1 !important;
  173. box-shadow: none !important;
  174. outline: none !important;
  175. background-image: none !important;
  176. }
  177. .dragCol {
  178. width: 100%;
  179. flex: 1;
  180. border-radius: 5px;
  181. float: left;
  182. margin-top: -5px;
  183. }
  184. .dragCol+.dragCol {
  185. margin-left: 10px;
  186. }
  187. .dragItem {
  188. width: auto;
  189. padding: 12px 10px;
  190. margin: 0 -5px;
  191. border-bottom: solid 1px #eee;
  192. display: flex;
  193. flex-flow: row nowrap;
  194. justify-content: space-between;
  195. }
  196. .dragItem:hover {
  197. background-color: #f5f7fa;
  198. cursor: move;
  199. }
  200. .dragItem+.dragItem {
  201. border-top: none;
  202. }
  203. #dragAll {
  204. color: #909399;
  205. font-weight: 700;
  206. }
  207. .svgIcon {
  208. width: 16px;
  209. text-align: center;
  210. cursor: pointer;
  211. }
  212. .dragHeader {}
  213. .dragMain {
  214. overflow-y: overlay;
  215. overflow-x: hidden;
  216. }
  217. /* .checkbox-common>>>span.el-checkbox__label {
  218. padding-left: 30px;
  219. } */
  220. </style>

官方文档例子

  1. <template>
  2. <div>
  3. <div>{{drag?'拖拽中':'拖拽停止'}}</div>
  4. <!--使用draggable组件-->
  5. <draggable v-model="myArray" chosenClass="chosen" forceFallback="true" group="people" animation="1000" @start="onStart" @end="onEnd">
  6. <transition-group>
  7. <div class="item" v-for="element in myArray" :key="element.id">{{element.name}}</div>
  8. </transition-group>
  9. </draggable>
  10. </div>
  11. </template>
  12. <style scoped>
  13. /*被拖拽对象的样式*/
  14. .item {
  15. padding: 6px;
  16. background-color: #fdfdfd;
  17. border: solid 1px #eee;
  18. margin-bottom: 10px;
  19. cursor: move;
  20. }
  21. /*选中样式*/
  22. .chosen {
  23. border: solid 2px #3089dc !important;
  24. }
  25. </style>
  26. <script>
  27. //导入draggable组件
  28. import draggable from 'vuedraggable'
  29. export default {
  30. //注册draggable组件
  31. components: {
  32. draggable,
  33. },
  34. data() {
  35. return {
  36. drag:false,
  37. //定义要被拖拽对象的数组
  38. myArray:[
  39. {people:'cn',id:1,name:'www.itxst.com'},
  40. {people:'cn',id:2,name:'www.baidu.com'},
  41. {people:'cn',id:3,name:'www.taobao.com'},
  42. {people:'us',id:4,name:'www.google.com'}
  43. ]
  44. };
  45. },
  46. methods: {
  47. //开始拖拽事件
  48. onStart(){
  49. this.drag=true;
  50. },
  51. //拖拽结束事件
  52. onEnd() {
  53. this.drag=false;
  54. },
  55. },
  56. };
  57. </script>

效果图

附上官方文档地址:vue.draggable中文文档 - itxst.com 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/421751
推荐阅读
相关标签
  

闽ICP备14008679号