53eab776b8601e3742dc60d1fd86b1093113e1f7ae92d97238cbbc462f8f0a4961c31b6fc8931a55e0e71978e333bddffc9d7b5f2b4fbbc294183f43f4f80f 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <p align="center"><img width="140"src="https://raw.githubusercontent.com/SortableJS/Vue.Draggable/master/logo.svg?sanitize=true"></p>
  2. <h1 align="center">Vue.Draggable</h1>
  3. [![CircleCI](https://circleci.com/gh/SortableJS/Vue.Draggable.svg?style=shield)](https://circleci.com/gh/SortableJS/Vue.Draggable)
  4. [![Coverage](https://codecov.io/gh/SortableJS/Vue.Draggable/branch/master/graph/badge.svg)](https://codecov.io/gh/SortableJS/Vue.Draggable)
  5. [![codebeat badge](https://codebeat.co/badges/7a6c27c8-2d0b-47b9-af55-c2eea966e713)](https://codebeat.co/projects/github-com-sortablejs-vue-draggable-master)
  6. [![GitHub open issues](https://img.shields.io/github/issues/SortableJS/Vue.Draggable.svg)](https://github.com/SortableJS/Vue.Draggable/issues?q=is%3Aopen+is%3Aissue)
  7. [![npm download](https://img.shields.io/npm/dt/vuedraggable.svg?maxAge=30)](https://www.npmjs.com/package/vuedraggable)
  8. [![npm download per month](https://img.shields.io/npm/dm/vuedraggable.svg)](https://www.npmjs.com/package/vuedraggable)
  9. [![npm version](https://img.shields.io/npm/v/vuedraggable.svg)](https://www.npmjs.com/package/vuedraggable)
  10. [![MIT License](https://img.shields.io/github/license/SortableJS/Vue.Draggable.svg)](https://github.com/SortableJS/Vue.Draggable/blob/master/LICENSE)
  11. Vue component (Vue.js 2.0) or directive (Vue.js 1.0) allowing drag-and-drop and synchronization with view model array.
  12. Based on and offering all features of [Sortable.js](https://github.com/RubaXa/Sortable)
  13. ## Demo
  14. ![demo gif](https://raw.githubusercontent.com/SortableJS/Vue.Draggable/master/example.gif)
  15. ## Live Demos
  16. https://sortablejs.github.io/Vue.Draggable/
  17. https://david-desmaisons.github.io/draggable-example/
  18. ## Features
  19. * Full support of [Sortable.js](https://github.com/RubaXa/Sortable) features:
  20. * Supports touch devices
  21. * Supports drag handles and selectable text
  22. * Smart auto-scrolling
  23. * Support drag and drop between different lists
  24. * No jQuery dependency
  25. * Keeps in sync HTML and view model list
  26. * Compatible with Vue.js 2.0 transition-group
  27. * Cancellation support
  28. * Events reporting any changes when full control is needed
  29. * Reuse existing UI library components (such as [vuetify](https://vuetifyjs.com), [element](http://element.eleme.io/), or [vue material](https://vuematerial.io) etc...) and make them draggable using `tag` and `componentData` props
  30. ## Backers
  31. <a href="https://flatlogic.com/admin-dashboards">
  32. <img width="190" style="margin-top: 10px;" src="https://flatlogic.com/assets/logo-d9e7751df5fddd11c911945a75b56bf72bcfe809a7f6dca0e32d7b407eacedae.svg">
  33. </a>
  34. Admin Dashboard Templates made with Vue, React and Angular.
  35. ## Donate
  36. Find this project useful? You can buy me a :coffee: or a :beer:
  37. [![paypal](https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=GYAEKQZJ4FQT2&currency_code=USD&source=url)
  38. ## Installation
  39. ### With npm or yarn
  40. ```bash
  41. yarn add vuedraggable
  42. npm i -S vuedraggable
  43. ```
  44. **Beware it is vuedraggable for Vue 2.0 and not vue-draggable which is for version 1.0**
  45. ### with direct link
  46. ```html
  47. <script src="//cdnjs.cloudflare.com/ajax/libs/vue/2.5.2/vue.min.js"></script>
  48. <!-- CDNJS :: Sortable (https://cdnjs.com/) -->
  49. <script src="//cdn.jsdelivr.net/npm/sortablejs@1.8.4/Sortable.min.js"></script>
  50. <!-- CDNJS :: Vue.Draggable (https://cdnjs.com/) -->
  51. <script src="//cdnjs.cloudflare.com/ajax/libs/Vue.Draggable/2.20.0/vuedraggable.umd.min.js"></script>
  52. ```
  53. [cf example section](https://github.com/SortableJS/Vue.Draggable/tree/master/example)
  54. ## For Vue.js 2.0
  55. Use draggable component:
  56. ### Typical use:
  57. ``` html
  58. <draggable v-model="myArray" group="people" @start="drag=true" @end="drag=false">
  59. <div v-for="element in myArray" :key="element.id">{{element.name}}</div>
  60. </draggable>
  61. ```
  62. .vue file:
  63. ``` js
  64. import draggable from 'vuedraggable'
  65. ...
  66. export default {
  67. components: {
  68. draggable,
  69. },
  70. ...
  71. ```
  72. ### With `transition-group`:
  73. ``` html
  74. <draggable v-model="myArray">
  75. <transition-group>
  76. <div v-for="element in myArray" :key="element.id">
  77. {{element.name}}
  78. </div>
  79. </transition-group>
  80. </draggable>
  81. ```
  82. Draggable component should directly wrap the draggable elements, or a `transition-component` containing the draggable elements.
  83. ### With footer slot:
  84. ``` html
  85. <draggable v-model="myArray" draggable=".item">
  86. <div v-for="element in myArray" :key="element.id" class="item">
  87. {{element.name}}
  88. </div>
  89. <button slot="footer" @click="addPeople">Add</button>
  90. </draggable>
  91. ```
  92. ### With header slot:
  93. ``` html
  94. <draggable v-model="myArray" draggable=".item">
  95. <div v-for="element in myArray" :key="element.id" class="item">
  96. {{element.name}}
  97. </div>
  98. <button slot="header" @click="addPeople">Add</button>
  99. </draggable>
  100. ```
  101. ### With Vuex:
  102. ```html
  103. <draggable v-model='myList'>
  104. ```
  105. ```javascript
  106. computed: {
  107. myList: {
  108. get() {
  109. return this.$store.state.myList
  110. },
  111. set(value) {
  112. this.$store.commit('updateList', value)
  113. }
  114. }
  115. }
  116. ```
  117. ### Props
  118. #### value
  119. Type: `Array`<br>
  120. Required: `false`<br>
  121. Default: `null`
  122. Input array to draggable component. Typically same array as referenced by inner element v-for directive.<br>
  123. This is the preferred way to use Vue.draggable as it is compatible with Vuex.<br>
  124. It should not be used directly but only though the `v-model` directive:
  125. ```html
  126. <draggable v-model="myArray">
  127. ```
  128. #### list
  129. Type: `Array`<br>
  130. Required: `false`<br>
  131. Default: `null`
  132. Alternative to the `value` prop, list is an array to be synchronized with drag-and-drop.<br>
  133. The main difference is that `list` prop is updated by draggable component using splice method, whereas `value` is immutable.<br>
  134. **Do not use in conjunction with value prop.**
  135. #### All sortable options
  136. New in version 2.19
  137. Sortable options can be set directly as vue.draggable props since version 2.19.
  138. This means that all [sortable option](https://github.com/RubaXa/Sortable#options) are valid sortable props with the notable exception of all the method starting by "on" as draggable component expose the same API via events.
  139. kebab-case propery are supported: for example `ghost-class` props will be converted to `ghostClass` sortable option.
  140. Example setting handle, sortable and a group option:
  141. ```HTML
  142. <draggable
  143. v-model="list"
  144. handle=".handle"
  145. :group="{ name: 'people', pull: 'clone', put: false }"
  146. ghost-class="ghost"
  147. :sort="false"
  148. @change="log"
  149. >
  150. <!-- -->
  151. </draggable>
  152. ```
  153. #### tag
  154. Type: `String`<br>
  155. Default: `'div'`
  156. HTML node type of the element that draggable component create as outer element for the included slot.<br>
  157. It is also possible to pass the name of vue component as element. In this case, draggable attribute will be passed to the create component.<br>
  158. See also [componentData](#componentdata) if you need to set props or event to the created component.
  159. #### clone
  160. Type: `Function`<br>
  161. Required: `false`<br>
  162. Default: `(original) => { return original;}`<br>
  163. Function called on the source component to clone element when clone option is true. The unique argument is the viewModel element to be cloned and the returned value is its cloned version.<br>
  164. By default vue.draggable reuses the viewModel element, so you have to use this hook if you want to clone or deep clone it.
  165. #### move
  166. Type: `Function`<br>
  167. Required: `false`<br>
  168. Default: `null`<br>
  169. If not null this function will be called in a similar way as [Sortable onMove callback](https://github.com/RubaXa/Sortable#move-event-object).
  170. Returning false will cancel the drag operation.
  171. ```javascript
  172. function onMoveCallback(evt, originalEvent){
  173. ...
  174. // return false; — for cancel
  175. }
  176. ```
  177. evt object has same property as [Sortable onMove event](https://github.com/RubaXa/Sortable#move-event-object), and 3 additional properties:
  178. - `draggedContext`: context linked to dragged element
  179. - `index`: dragged element index
  180. - `element`: dragged element underlying view model element
  181. - `futureIndex`: potential index of the dragged element if the drop operation is accepted
  182. - `relatedContext`: context linked to current drag operation
  183. - `index`: target element index
  184. - `element`: target element view model element
  185. - `list`: target list
  186. - `component`: target VueComponent
  187. HTML:
  188. ```HTML
  189. <draggable :list="list" :move="checkMove">
  190. ```
  191. javascript:
  192. ```javascript
  193. checkMove: function(evt){
  194. return (evt.draggedContext.element.name!=='apple');
  195. }
  196. ```
  197. See complete example: [Cancel.html](https://github.com/SortableJS/Vue.Draggable/blob/master/examples/Cancel.html), [cancel.js](https://github.com/SortableJS/Vue.Draggable/blob/master/examples/script/cancel.js)
  198. #### componentData
  199. Type: `Object`<br>
  200. Required: `false`<br>
  201. Default: `null`<br>
  202. This props is used to pass additional information to child component declared by [tag props](#tag).<br>
  203. Value:
  204. * `props`: props to be passed to the child component
  205. * `attrs`: attrs to be passed to the child component
  206. * `on`: events to be subscribe in the child component
  207. Example (using [element UI library](http://element.eleme.io/#/en-US)):
  208. ```HTML
  209. <draggable tag="el-collapse" :list="list" :component-data="getComponentData()">
  210. <el-collapse-item v-for="e in list" :title="e.title" :name="e.name" :key="e.name">
  211. <div>{{e.description}}</div>
  212. </el-collapse-item>
  213. </draggable>
  214. ```
  215. ```javascript
  216. methods: {
  217. handleChange() {
  218. console.log('changed');
  219. },
  220. inputChanged(value) {
  221. this.activeNames = value;
  222. },
  223. getComponentData() {
  224. return {
  225. on: {
  226. change: this.handleChange,
  227. input: this.inputChanged
  228. },
  229. attrs:{
  230. wrap: true
  231. },
  232. props: {
  233. value: this.activeNames
  234. }
  235. };
  236. }
  237. }
  238. ```
  239. ### Events
  240. * Support for Sortable events:
  241. `start`, `add`, `remove`, `update`, `end`, `choose`, `unchoose`, `sort`, `filter`, `clone`<br>
  242. Events are called whenever onStart, onAdd, onRemove, onUpdate, onEnd, onChoose, onUnchoose, onSort, onClone are fired by Sortable.js with the same argument.<br>
  243. [See here for reference](https://github.com/RubaXa/Sortable#event-object-demo)
  244. Note that SortableJS OnMove callback is mapped with the [move prop](https://github.com/SortableJS/Vue.Draggable/blob/master/README.md#move)
  245. HTML:
  246. ```HTML
  247. <draggable :list="list" @end="onEnd">
  248. ```
  249. * change event
  250. `change` event is triggered when list prop is not null and the corresponding array is altered due to drag-and-drop operation.<br>
  251. This event is called with one argument containing one of the following properties:
  252. - `added`: contains information of an element added to the array
  253. - `newIndex`: the index of the added element
  254. - `element`: the added element
  255. - `removed`: contains information of an element removed from to the array
  256. - `oldIndex`: the index of the element before remove
  257. - `element`: the removed element
  258. - `moved`: contains information of an element moved within the array
  259. - `newIndex`: the current index of the moved element
  260. - `oldIndex`: the old index of the moved element
  261. - `element`: the moved element
  262. ### Slots
  263. Limitation: neither header or footer slot works in conjunction with transition-group.
  264. #### Header
  265. Use the `header` slot to add none-draggable element inside the vuedraggable component.
  266. Important: it should be used in conjunction with draggable option to tag draggable element.
  267. Note that header slot will always be added before the default slot regardless its position in the template.
  268. Ex:
  269. ``` html
  270. <draggable v-model="myArray" draggable=".item">
  271. <div v-for="element in myArray" :key="element.id" class="item">
  272. {{element.name}}
  273. </div>
  274. <button slot="header" @click="addPeople">Add</button>
  275. </draggable>
  276. ```
  277. #### Footer
  278. Use the `footer` slot to add none-draggable element inside the vuedraggable component.
  279. Important: it should be used in conjunction with draggable option to tag draggable elements.
  280. Note that footer slot will always be added after the default slot regardless its position in the template.
  281. Ex:
  282. ``` html
  283. <draggable v-model="myArray" draggable=".item">
  284. <div v-for="element in myArray" :key="element.id" class="item">
  285. {{element.name}}
  286. </div>
  287. <button slot="footer" @click="addPeople">Add</button>
  288. </draggable>
  289. ```
  290. ### Gotchas
  291. - Vue.draggable children should always map the list or value prop using a v-for directive
  292. * You may use [header](https://github.com/SortableJS/Vue.Draggable#header) and [footer](https://github.com/SortableJS/Vue.Draggable#footer) slot to by-pass this limitation.
  293. - Children elements inside v-for should be keyed as any element in Vue.js. Be carefull to provide revelant key values in particular:
  294. * typically providing array index as keys won't work as key should be linked to the items content
  295. * cloned elements should provide updated keys, it is doable using the [clone props](#clone) for example
  296. ### Example
  297. * [Clone](https://sortablejs.github.io/Vue.Draggable/#/custom-clone)
  298. * [Handle](https://sortablejs.github.io/Vue.Draggable/#/handle)
  299. * [Transition](https://sortablejs.github.io/Vue.Draggable/#/transition-example-2)
  300. * [Nested](https://sortablejs.github.io/Vue.Draggable/#/nested-example)
  301. * [Table](https://sortablejs.github.io/Vue.Draggable/#/table-example)
  302. ### Full demo example
  303. [draggable-example](https://github.com/David-Desmaisons/draggable-example)
  304. ## For Vue.js 1.0
  305. [See here](documentation/Vue.draggable.for.ReadME.md)
  306. ```