You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
2.3 KiB
Vue

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<!--
* @Descripttion:
* @Author: liu.shiyi
* @Date: 2023-03-14 10:20:54
* @LastEditTime: 2023-05-23 15:14:47
-->
<template>
<el-select
ref="dragSelect"
v-model="selectVal"
v-bind="$attrs"
class="drag-select"
v-on="$listeners"
>
<slot />
</el-select>
</template>
<script>
import { EventBus } from 'data-room-ui/js/utils/eventBus'
import Sortable from 'sortablejs'
export default {
name: 'DragSelect',
props: {
value: {
type: [Array, String],
required: true
}
},
computed: {
selectVal: {
get () {
if (Array.isArray(this.value)) {
return [...this.value]
} else {
return this.value
}
},
set (val) {
if (Array.isArray(val)) {
this.$emit('input', [...val])
} else {
this.$emit('input', val)
}
}
}
},
watch: {
// 监听数组变化根据上次的数据的值判断这次拖拽后数据是否变化如果数据位置发生变化则出发emit事件
selectVal: {
handler (newVal, oldVal) {
if (Array.isArray(newVal) && Array.isArray(oldVal)) {
if ((newVal.length === oldVal.length) && (JSON.stringify(oldVal) !== JSON.stringify(newVal))) {
// 告诉右侧的数据配置面板选择器内的选项顺序发生变化修改config的数据顺序
this.$emit('valuePositionChange', newVal)
}
}
}
}
},
created () {
},
mounted () {
this.setSort()
},
methods: {
setSort () {
const el = this.$refs.dragSelect.$el.querySelectorAll('.el-select__tags > span')[0]
if (el) {
this.sortable = Sortable.create(el, {
animation: 350,
ghostClass: 'sortable-ghost',
setData: function (dataTransfer) {
dataTransfer.setData('Text', '')
},
onEnd: (evt) => {
const targetRow = this.value.splice(evt.oldIndex, 1)[0]
this.value.splice(evt.newIndex, 0, targetRow)
}
})
}
}
}
}
</script>
<style lang="scss" scoped>
.drag-select {
::v-deep {
.sortable-ghost {
opacity: 0.8;
color: #fff !important;
background:var(--bs-el-color-primary) !important;
}
.el-tag {
cursor: move;
}
}
}
</style>