Last active
October 15, 2019 20:54
-
-
Save marwinious/7b9d164c9c448cb0fae04cf3575d418a to your computer and use it in GitHub Desktop.
Vue.js component for Vue.Draggable (2-list version)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <template> | |
| <!-- | |
| Vue.Draggable: https://github.com/SortableJS/Vue.Draggable | |
| --> | |
| <div class="draggable_two_list_container"> | |
| <div class="row"> | |
| <div class="col-6"> | |
| <h5 | |
| v-if="list1Label" | |
| > | |
| {{list1Label}} | |
| </h5> | |
| <draggable | |
| tag="ul" | |
| class="list-group" | |
| :list="currentList1" | |
| :group="{name: groupName, pull: 'clone', put: false}" | |
| @change="log" | |
| > | |
| <li | |
| class="list-group-item" | |
| v-for="(item, index) in currentList1" | |
| :key="item.name" | |
| > | |
| <font-awesome-icon icon="align-justify" class="handle" /> | |
| <span class="text">{{ item[labelField] }}</span> | |
| <font-awesome-icon icon="times" class="close" @click="removeAt('currentList1', index)" /> | |
| </li> | |
| </draggable> | |
| </div> | |
| <div class="col-6"> | |
| <h5 | |
| v-if="list2Label" | |
| > | |
| {{list2Label}} | |
| </h5> | |
| <draggable | |
| tag="ul" | |
| class="list-group" | |
| :list="currentList2" | |
| :group="groupName" | |
| @change="log" | |
| > | |
| <li | |
| class="list-group-item" | |
| v-for="(item, index) in currentList2" | |
| :key="item.name" | |
| > | |
| <font-awesome-icon icon="align-justify" class="handle" /> | |
| <span class="text">{{ item[labelField] }}</span> | |
| <font-awesome-icon icon="times" class="close" @click="removeAt('currentList2', index)" /> | |
| </li> | |
| </draggable> | |
| </div> | |
| </div> | |
| </div> | |
| </template> | |
| <script> | |
| import draggable from 'vuedraggable' | |
| export default { | |
| name: 'BaseDraggableTwoLists', | |
| props: { | |
| labelField: { | |
| type: String, | |
| required: true | |
| }, | |
| groupName: { | |
| type: String, | |
| required: true | |
| }, | |
| list1: { | |
| type: Array, | |
| required: true | |
| }, | |
| list1Label: { | |
| type: String, | |
| required: false, | |
| }, | |
| list2: { | |
| type: Array, | |
| required: false | |
| }, | |
| list2Label: { | |
| type: String, | |
| required: false | |
| } | |
| }, | |
| data () { | |
| return { | |
| currentList1: [], | |
| currentList2: [] | |
| } | |
| }, | |
| mounted() { | |
| this.checkLists(); | |
| }, | |
| computed: { | |
| }, | |
| methods: { | |
| checkLists: function() { | |
| const vm = this; | |
| if(vm.list1 && vm.list1.length > 0) { | |
| vm.currentList1 = vm.list1; | |
| } | |
| if(vm.list2 && vm.list2.length > 0) { | |
| vm.currentList2 = vm.list2; | |
| } | |
| }, | |
| removeAt(list, index) { | |
| this[list].splice(index, 1); | |
| }, | |
| log: function(evt) { | |
| window.console.log(evt); | |
| } | |
| }, | |
| components: { | |
| draggable | |
| } | |
| } | |
| </script> | |
| <style lang="scss" scoped> | |
| .draggable_two_list_container { | |
| margin-top: 1rem; | |
| margin-bottom: 2rem; | |
| .list-group { | |
| height: 30vh; | |
| overflow-y: scroll; | |
| } | |
| .handle { | |
| margin-right: 0; | |
| color: #aaa; | |
| cursor: grab; | |
| &:active { | |
| cursor: grabbing; | |
| } | |
| } | |
| .close { | |
| float: right; | |
| color: #666; | |
| } | |
| .text { | |
| margin: 20px; | |
| } | |
| } | |
| </style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment