he wei
2025-04-03 c6a2d2debf161b987ff91c6ac9560d10fc98c54b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<template>
  <el-select ref="dragSelect" v-model="selectVal" v-bind="$attrs" class="drag-select" multiple v-on="$attrs">
    <slot />
  </el-select>
</template>
 
<script>
import { defineComponent } from 'vue';
import Sortable from 'sortablejs';
 
export default defineComponent({
  name: 'DragSelect',
  props: {
    modelValue: {
      type: Array,
      required: true
    },
    placeholder: {
      type: String,
      default: null
    },
    style: {
      type: [String, Object],
      default: null
    }
  },
  computed: {
    selectVal: {
      get() {
        return [...this.modelValue];
      },
      set(val) {
        this.$emit('update:modelValue', [...val]);
      }
    }
  },
  mounted() {
    this.setSort();
  },
  methods: {
    setSort() {
      const el = this.$refs.dragSelect.$el.querySelector('.el-select__wrapper .el-select__selection');
      this.sortable = Sortable.create(el, {
        ghostClass: 'sortable-ghost', // Class name for the drop placeholder,
        setData: function(dataTransfer) {
          dataTransfer.setData('Text', '');
          // to avoid Firefox bug
          // Detail see : https://github.com/RubaXa/Sortable/issues/1012
        },
        onEnd: evt => {
          const theValue = this.modelValue;
          const targetRow = theValue.splice(evt.oldIndex, 1)[0];
          theValue.splice(evt.newIndex, 0, targetRow);
        }
      });
    }
  }
});
</script>
 
<style lang="less" scoped>
.drag-select {
  :deep(.sortable-ghost .el-tag) {
    opacity: .8;
    color: #fff !important;
    background: #42b983 !important;
  }
  :deep(.el-tag) {
    cursor: pointer;
  }
}
</style>