Vue UI Components Documentation
Vue Sortable Component
The Sortable component for Vue represents a plugin which allows you to reorder elements in a HTML list or div tags using the mouse.
Prerequisites
Refer to Vue Getting Started before you start with this help topic.
Configuration
After you have created your App.vue file, here is how you should structure it:
The Sortable component for Vue requires the following import:
import JqxSortable from 'jqwidgets-scripts/jqwidgets-vue/vue_jqxsortable.vue';
Add the jqxSortable component to the components section of the the Vue class:
components: { JqxSortable },
Template
The App.vue has a <template>
structural tag where we determine the application structure.
There we will also set the tags for the new components - <JqxSortable/>
<template> <JqxSortable :placeholderShow="placeholderShow" :scroll="scroll"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> </JqxSortable></template>
Properties
The properties of the <JqxSortable/> component are defined in the data
member of the Vue class.
We should put them in the return object of the data function:
data: function () { return { placeholderShow: 'sortable-placeholder' scroll: false }}
Events
The events in Vue are set as an attribute with @ prefix, for example:
<JqxSortable @change="onChange()" :placeholderShow="placeholderShow" :scroll="scroll"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div></JqxSortable>
All events that are bound to a component are implemented in the methods
member of the Vue class.
methods: { onChange: function () { // Do something... }}
Methods
To use a component's method we should have its reference. In Vue we refer to a component by the special $refs
property.
Before that we need to add the desired name reference to that component:
<JqxSortable ref="sortable"></JqxSortable>
Here how you can use a component's method:
this.$refs.sortable.toArray();
methods: { // Add here all used callbacks and/or events onChange: function () { // Do something... let array = this.refs.sortable.toArray(); alert(array); }}
If we want to add additional methods we should also implement them in the methods
member.
In case we need to do some precalculation or something else before the components are rendered, we should use the beforeCreate
member.
It depends on the case.
If you have followed the above steps, you App.vue file would look like this:
App.vue:
<template> <JqxSortable ref="sortable" @change="onChange()" :placeholderShow="placeholderShow" :scroll="scroll"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> </JqxSortable></template> <script> // Import the components that will be used import JqxSortable from 'jqwidgets-scripts/jqwidgets-vue/vue_jqxsortable.vue'; export default { components: { // Adding imported widgets here JqxSortable }, data: function () { // Define properties which will use in the widget return { placeholderShow: 'sortable-placeholder', scroll: false } }, beforeCreate: function () { // Add here any data where you want to transform before components be rendered }, methods: { // Add here all used callbacks and/or events onChange: function () { // Do something... let array = this.refs.sortable.toArray(); alert(array); } } }</script> <style></style>
Sortable Example
<template> <JqxSortable ref="mySortable"></JqxSortable></template> <script> import JqxSortable from 'jqwidgets-scripts/jqwidgets-vue/vue_jqxsortable.vue'; export default { components: { JqxSortable }, data: function () { return { firstNames: ['Nancy', 'Andrew', 'Janet', 'Margaret', 'Steven'], lastNames: ['Davolio', 'Fuller', 'Leverling', 'Peacock', 'Buchanan'], titles: ['Sales Representative', 'Vice President, Sales', 'Sales Representative', 'Sales Representative', 'Sales Manager'] } }, mounted: function () { this.loadInfo(); }, methods: { loadInfo: function () { for (let i = 0; i < this.firstNames.length; i++) { const element = document.createElement('div'); const imgurl = 'images/' + this.firstNames[i].toLowerCase() + '.png'; const img = '<img height="50" width="40" src="' + imgurl + '"/>'; element.innerHTML = '<table style="min-width: 130px;"><tr><td style="width: 40px;" rowspan="2">' + img + '</td><td>' + this.firstNames[i] + ' ' + this.lastNames[i] + '</td></tr><tr><td>' + this.titles[i] + '</td></tr></table>'; this.$refs.mySortable.$el.appendChild(element); } } } }</script> <style> .jqx-sortable { margin: 2px; padding: 5px; float: left; width: 200px; border: lightblue solid 1px; } .jqx-sortable > div { border-radius: 5px; padding: 5px; cursor: pointer; background-color: white; color: black; border: 1px solid transparent; } .jqx-sortable div:hover { border: 1px solid #356AA0; }</style>