Gathering detailed insights and metrics for zhyswan-vuedraggable
Gathering detailed insights and metrics for zhyswan-vuedraggable
Gathering detailed insights and metrics for zhyswan-vuedraggable
Gathering detailed insights and metrics for zhyswan-vuedraggable
Vue 3 compatible drag-and-drop component based on Sortable.js
npm install zhyswan-vuedraggable
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
15 Stars
660 Commits
3 Forks
7 Branches
1 Contributors
Updated on 09 Nov 2024
JavaScript (62.13%)
Vue (37.36%)
HTML (0.46%)
Shell (0.04%)
Cumulative downloads
Total Downloads
Last day
-64.1%
220
Compared to previous day
Last week
122%
3,408
Compared to previous week
Last month
65.7%
7,511
Compared to previous month
Last year
151.9%
60,931
Compared to previous year
1
1
26
Vue component (Vue.js 3.0) allowing drag-and-drop and synchronization with view model array.
Based on and offering all features of Sortable.js vue.draggable.next
https://zhyswan.github.io/vue3.draggable/
tag
and componentData
propsFind this project useful? You can buy me a :coffee: or a :beer:
1yarn add zhyswan-vuedraggable 2 3npm i zhyswan-vuedraggable
1<draggable 2 v-model="myArray" 3 group="people" 4 @start="drag=true" 5 @end="drag=false" 6 item-key="id"> 7 <template #item="{element}"> 8 <div>{{element.name}}</div> 9 </template> 10</draggable>
1 import draggable from 'vuedraggable' 2 ... 3 export default { 4 components: { 5 draggable, 6 }, 7 data() { 8 return { 9 drag: false, 10 } 11 }, 12 ...
The item
slot should be used to display items of the list. It receives the element value and the element index as slot-props.
transition-group
:1<draggable v-model="myArray" tag="transition-group" item-key="id"> 2 <template #item="{element}"> 3 <div> 4 <div> {{element.name}} </div> 5 </div> 6 </template> 7</draggable>
1<draggable v-model="myArray" item-key="id"> 2 <template #item="{element}"> 3 <div> {{element.name}} </div> 4 </template> 5 <template #footer> 6 <button @click="addPeople">Add</button> 7 </template> 8</draggable>
1<draggable v-model="myArray" item-key="id"> 2 <template #item="{element}"> 3 <div> {{element.name}} </div> 4 </template> 5 <template #header> 6 <button @click="addPeople">Add</button> 7 </template> 8</draggable>
1<draggable v-model='myList'>
1computed: { 2 myList: { 3 get() { 4 return this.$store.state.myList 5 }, 6 set(value) { 7 this.$store.commit('updateList', value) 8 } 9 } 10}
Breaking changes:
item
slot instead of default to display elementsitemKey
propsFrom:
1<!-- vue 2 version --> 2<draggable v-model="myArray"> 3 <div v-for="element in myArray" :key="element.id">{{element.name}}</div> 4</draggable>
To:
1<draggable v-model="myArray" item-key="id"> 2 <template #item="{element}"> 3 <div>{{element.name}}</div> 4 </template> 5</draggable>
Breaking changes:
3) When using transition, you should now use the tag
props and componentData
to create the transition
From
1<!-- vue 2 version --> 2<draggable v-model="myArray"> 3 <transition-group name="fade"> 4 <div v-for="element in myArray" :key="element.id"> 5 {{element.name}} 6 </div> 7 </transition-group> 8</draggable>
to
1<draggable v-model="myArray" tag="transition-group" :component-data="{name:'fade'}"> 2 <template #item="{element}"> 3 <div>{{element.name}}</div> 4 </template> 5</draggable>
Type: Array
Required: false
Default: null
Input array to draggable component. Typically same array as referenced by inner element v-for directive.
This is the preferred way to use Vue.draggable as it is compatible with Vuex.
It should not be used directly but only though the v-model
directive:
1<draggable v-model="myArray">
Type: Array
Required: false
Default: null
Alternative to the modelValue
prop, list is an array to be synchronized with drag-and-drop.
The main difference is that list
prop is updated by draggable component using splice method, whereas modelValue
is immutable.
Do not use in conjunction with modelValue prop.
Type: String
or Function
Required: true
The property to be used as the element key. Alternatively a function receiving an element of the list and returning its key.
Sortable options can be set directly as vue.draggable props since version 2.19.
This means that all sortable option are valid sortable props with the notable exception of all the method starting by "on" as draggable component expose the same API via events.
kebab-case property are supported: for example ghost-class
props will be converted to ghostClass
sortable option.
Example setting handle, sortable and a group option:
1<draggable 2 v-model="list" 3 handle=".handle" 4 :group="{ name: 'people', pull: 'clone', put: false }" 5 ghost-class="ghost" 6 :sort="false" 7 @change="log" 8 > 9 <!-- --> 10</draggable>
Type: String
Default: 'div'
HTML node type of the element that draggable component create as outer element for the included slot.
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.
See also componentData if you need to set props or event to the created component.
Type: Function
Required: false
Default: (original) => { return original;}
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.
By default vue.draggable reuses the viewModel element, so you have to use this hook if you want to clone or deep clone it.
Type: Function
Required: false
Default: null
If not null this function will be called in a similar way as Sortable onMove callback. Returning false will cancel the drag operation.
1function onMoveCallback(evt, originalEvent){ 2 ... 3 // return false; — for cancel 4}
evt object has same property as Sortable onMove event, and 3 additional properties:
draggedContext
: context linked to dragged element
index
: dragged element indexelement
: dragged element underlying view model elementfutureIndex
: potential index of the dragged element if the drop operation is acceptedrelatedContext
: context linked to current drag operation
index
: target element indexelement
: target element view model elementlist
: target listcomponent
: target VueComponentHTML:
1<draggable :list="list" :move="checkMove">
javascript:
1checkMove: function(evt){ 2 return (evt.draggedContext.element.name!=='apple'); 3}
See complete example: Cancel.html, cancel.js
Type: Object
Required: false
Default: null
This props is used to pass additional information to child component declared by tag props.
Value: an object corresponding to the attributes, props and events we would pass to the component.
Example (using element UI library):
1<draggable tag="el-collapse" :list="list" :component-data="getComponentData()" item-key="name"> 2 <template #item="{element}"> 3 <el-collapse-item :title="element.title" :name="element.name"> 4 <div>{{element.description}}</div> 5 </el-collapse-item> 6 </template> 7</draggable>
1methods: { 2 handleChange() { 3 console.log('changed'); 4 }, 5 inputChanged(value) { 6 this.activeNames = value; 7 }, 8 getComponentData() { 9 return { 10 onChange: this.handleChange, 11 onInput: this.inputChanged, 12 wrap: true, 13 value: this.activeNames 14 }; 15 } 16 }
Support for Sortable events:
start
, add
, remove
, update
, end
, choose
, unchoose
, sort
, filter
, clone
Events are called whenever onStart, onAdd, onRemove, onUpdate, onEnd, onChoose, onUnchoose, onSort, onClone are fired by Sortable.js with the same argument.
See here for reference
Note that SortableJS OnMove callback is mapped with the move prop
HTML:
1<draggable :list="list" @end="onEnd">
change event
change
event is triggered when list prop is not null and the corresponding array is altered due to drag-and-drop operation.
This event is called with one argument containing one of the following properties:
added
: contains information of an element added to the array
newIndex
: the index of the added elementelement
: the added elementremoved
: contains information of an element removed from to the array
oldIndex
: the index of the element before removeelement
: the removed elementmoved
: contains information of an element moved within the array
newIndex
: the current index of the moved elementoldIndex
: the old index of the moved elementelement
: the moved elementThe item
slot is used to display one element of the list. Vue.draggable will iterate the list and call this slot for each element.
Slot props:
element
the element in the listindex
the element indexIt is the only required slot.
1<draggable v-model="myArray" item-key="id"> 2 <template #item="{element, index}"> 3 <div> {{index}} - {{element.name}} </div> 4 </template> 5</draggable>
Use the header
slot to add none-draggable element inside the vuedraggable component.
Ex:
1<draggable v-model="myArray" item-key="id"> 2 <template #item="{element}"> 3 <div> {{element.name}} </div> 4 </template> 5 <template #header> 6 <button @click="addPeople">Add</button> 7 </template> 8</draggable>
Use the footer
slot to add none-draggable element inside the vuedraggable component.
Ex:
1<draggable v-model="myArray" item-key="id"> 2 <template #item="{element}"> 3 <div> {{element.name}} </div> 4 </template> 5 <template #footer> 6 <button @click="addPeople">Add</button> 7 </template> 8</draggable>
No vulnerabilities found.
No security vulnerabilities found.