test-drage.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <template>
  2. <!-- 拖拽demo-->
  3. <transition-group name="drag"
  4. class="list"
  5. tag="ul"
  6. >
  7. <li
  8. v-for="(item, index) in sortList"
  9. :key="item.groupId"
  10. >
  11. <span>{{`第${index + 1}志愿:`}}</span>
  12. <span v-if="!btnDisabled" @dragenter="dragenter($event, index)"
  13. @dragover="dragover($event, index)"
  14. @dragstart="dragstart(index)"
  15. class="list-item move"
  16. draggable
  17. >{{ item.groupName }}</span>
  18. <span class="list-item" v-else>{{ item.groupName }}</span>
  19. <el-button style="cursor: pointer" @click="del(index)" size="mini" v-if="!btnDisabled" type="danger">删除</el-button>
  20. </li>
  21. </transition-group>
  22. </template>
  23. <script>
  24. export default {
  25. props: {
  26. selectedList: Array,
  27. btnDisabled: {
  28. type: Boolean,
  29. default: false
  30. },
  31. },
  32. data() {
  33. return {
  34. dragIndex: '',
  35. enterIndex: '',
  36. sortList: [],
  37. }
  38. },
  39. watch: {
  40. selectedList: {
  41. immediate:true,
  42. handler(val){
  43. this.sortList = val
  44. }
  45. }
  46. },
  47. methods: {
  48. dragstart(index) {
  49. this.dragIndex = index
  50. },
  51. dragenter(e, index) {
  52. e.preventDefault()
  53. // 避免源对象触发自身的dragenter事件
  54. if (this.dragIndex !== index) {
  55. const source = this.sortList[this.dragIndex]
  56. this.sortList.splice(this.dragIndex, 1)
  57. this.sortList.splice(index, 0, source)
  58. // 排序变化后目标对象的索引变成源对象的索引
  59. this.dragIndex = index
  60. }
  61. },
  62. shuffle() {
  63. this.sortList = this.$shuffle(this.sortList)
  64. },
  65. del(index) {
  66. this.sortList[index].selected = false
  67. this.sortList.splice(index,1)
  68. },
  69. dragover(e, index) {
  70. e.preventDefault()
  71. }
  72. }
  73. }
  74. </script>
  75. <style lang="scss" scoped>
  76. .list {
  77. padding-left: 0;
  78. list-style: none;
  79. .drag-move {
  80. transition: transform .3s;
  81. }
  82. li{
  83. font-size: 14px;
  84. margin-bottom: 6px;
  85. }
  86. .move{
  87. cursor: move;
  88. }
  89. .list-item {
  90. display:inline-block ;
  91. margin: 0 10px;
  92. width: 160px;
  93. background: #42b983;
  94. border-radius: 4px;
  95. font-size: 14px;
  96. color: #FFF;
  97. height: 30px;
  98. line-height: 30px;
  99. text-align: center;
  100. }
  101. }
  102. </style>