UNPKG

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