ElDragSelect.vue 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <!--
  2. * @Descripttion:
  3. * @Author: liu.shiyi
  4. * @Date: 2023-03-14 10:20:54
  5. * @LastEditTime: 2023-05-23 15:14:47
  6. -->
  7. <template>
  8. <el-select
  9. ref="dragSelect"
  10. v-model="selectVal"
  11. v-bind="$attrs"
  12. class="drag-select"
  13. v-on="$listeners"
  14. >
  15. <slot />
  16. </el-select>
  17. </template>
  18. <script>
  19. import Sortable from 'sortablejs'
  20. export default {
  21. name: 'DragSelect',
  22. props: {
  23. value: {
  24. type: [Array, String],
  25. required: true
  26. }
  27. },
  28. computed: {
  29. selectVal: {
  30. get () {
  31. if (Array.isArray(this.value)) {
  32. return [...this.value]
  33. } else {
  34. return this.value
  35. }
  36. },
  37. set (val) {
  38. if (Array.isArray(val)) {
  39. this.$emit('input', [...val])
  40. } else {
  41. this.$emit('input', val)
  42. }
  43. }
  44. }
  45. },
  46. mounted () {
  47. this.setSort()
  48. },
  49. methods: {
  50. setSort () {
  51. const el = this.$refs.dragSelect.$el.querySelectorAll(
  52. '.el-select__tags > span'
  53. )[0]
  54. if (el) {
  55. this.sortable = Sortable.create(el, {
  56. animation: 350,
  57. ghostClass: 'sortable-ghost',
  58. setData: function (dataTransfer) {
  59. dataTransfer.setData('Text', '')
  60. },
  61. onEnd: (evt) => {
  62. const targetRow = this.value.splice(evt.oldIndex, 1)[0]
  63. this.value.splice(evt.newIndex, 0, targetRow)
  64. }
  65. })
  66. }
  67. }
  68. }
  69. }
  70. </script>
  71. <style lang="scss" scoped>
  72. .drag-select {
  73. ::v-deep {
  74. .sortable-ghost {
  75. opacity: 0.8;
  76. color: #fff !important;
  77. background:var(--bs-el-color-primary) !important;
  78. }
  79. .el-tag {
  80. cursor: move;
  81. }
  82. }
  83. }
  84. </style>