123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <template>
- <!-- 拖拽demo-->
- <transition-group name="drag"
- class="list"
- tag="ul"
- >
- <li
- v-for="(item, index) in sortList"
- :key="item.groupId"
- >
- <span>{{`第${index + 1}志愿:`}}</span>
- <span v-if="!btnDisabled" @dragenter="dragenter($event, index)"
- @dragover="dragover($event, index)"
- @dragstart="dragstart(index)"
- class="list-item move"
- draggable
- >{{ item.groupName }}</span>
- <span class="list-item" v-else>{{ item.groupName }}</span>
- <el-button style="cursor: pointer" @click="del(index)" size="mini" v-if="!btnDisabled" type="danger">删除</el-button>
- </li>
- </transition-group>
- </template>
- <script>
- export default {
- props: {
- selectedList: Array,
- btnDisabled: {
- type: Boolean,
- default: false
- },
- },
- data() {
- return {
- dragIndex: '',
- enterIndex: '',
- sortList: [],
- }
- },
- watch: {
- selectedList: {
- immediate:true,
- handler(val){
- this.sortList = val
- }
- }
- },
- methods: {
- dragstart(index) {
- this.dragIndex = index
- },
- dragenter(e, index) {
- e.preventDefault()
- // 避免源对象触发自身的dragenter事件
- if (this.dragIndex !== index) {
- const source = this.sortList[this.dragIndex]
- this.sortList.splice(this.dragIndex, 1)
- this.sortList.splice(index, 0, source)
- // 排序变化后目标对象的索引变成源对象的索引
- this.dragIndex = index
- }
- },
- shuffle() {
- this.sortList = this.$shuffle(this.sortList)
- },
- del(index) {
- this.sortList[index].selected = false
- this.sortList.splice(index,1)
- },
- dragover(e, index) {
- e.preventDefault()
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- .list {
- padding-left: 0;
- list-style: none;
- .drag-move {
- transition: transform .3s;
- }
- li{
- font-size: 14px;
- margin-bottom: 6px;
- }
- .move{
- cursor: move;
- }
- .list-item {
- display:inline-block ;
- margin: 0 10px;
- width: 160px;
- background: #42b983;
- border-radius: 4px;
- font-size: 14px;
- color: #FFF;
- height: 30px;
- line-height: 30px;
- text-align: center;
- }
- }
- </style>
|