index.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <template>
  2. <div class="dndList">
  3. <div :style="{width:width1}" class="dndList-list">
  4. <h3>{{ list1Title }}</h3>
  5. <draggable :set-data="setData" :list="list1" group="article" class="dragArea">
  6. <div v-for="element in list1" :key="element.id" class="list-complete-item">
  7. <div class="list-complete-item-handle">
  8. {{ element.id }}[{{ element.author }}] {{ element.title }}
  9. </div>
  10. <div style="position:absolute;right:0px;">
  11. <span style="float: right ;margin-top: -20px;margin-right:5px;" @click="deleteEle(element)">
  12. <i style="color:#ff4949" class="el-icon-delete" />
  13. </span>
  14. </div>
  15. </div>
  16. </draggable>
  17. </div>
  18. <div :style="{width:width2}" class="dndList-list">
  19. <h3>{{ list2Title }}</h3>
  20. <draggable :list="list2" group="article" class="dragArea">
  21. <div v-for="element in list2" :key="element.id" class="list-complete-item">
  22. <div class="list-complete-item-handle2" @click="pushEle(element)">
  23. {{ element.id }} [{{ element.author }}] {{ element.title }}
  24. </div>
  25. </div>
  26. </draggable>
  27. </div>
  28. </div>
  29. </template>
  30. <script>
  31. import draggable from 'vuedraggable'
  32. export default {
  33. name: 'DndList',
  34. components: { draggable },
  35. props: {
  36. list1: {
  37. type: Array,
  38. default() {
  39. return []
  40. }
  41. },
  42. list2: {
  43. type: Array,
  44. default() {
  45. return []
  46. }
  47. },
  48. list1Title: {
  49. type: String,
  50. default: 'list1'
  51. },
  52. list2Title: {
  53. type: String,
  54. default: 'list2'
  55. },
  56. width1: {
  57. type: String,
  58. default: '48%'
  59. },
  60. width2: {
  61. type: String,
  62. default: '48%'
  63. }
  64. },
  65. methods: {
  66. isNotInList1(v) {
  67. return this.list1.every(k => v.id !== k.id)
  68. },
  69. isNotInList2(v) {
  70. return this.list2.every(k => v.id !== k.id)
  71. },
  72. deleteEle(ele) {
  73. for (const item of this.list1) {
  74. if (item.id === ele.id) {
  75. const index = this.list1.indexOf(item)
  76. this.list1.splice(index, 1)
  77. break
  78. }
  79. }
  80. if (this.isNotInList2(ele)) {
  81. this.list2.unshift(ele)
  82. }
  83. },
  84. pushEle(ele) {
  85. for (const item of this.list2) {
  86. if (item.id === ele.id) {
  87. const index = this.list2.indexOf(item)
  88. this.list2.splice(index, 1)
  89. break
  90. }
  91. }
  92. if (this.isNotInList1(ele)) {
  93. this.list1.push(ele)
  94. }
  95. },
  96. setData(dataTransfer) {
  97. // to avoid Firefox bug
  98. // Detail see : https://github.com/RubaXa/Sortable/issues/1012
  99. dataTransfer.setData('Text', '')
  100. }
  101. }
  102. }
  103. </script>
  104. <style lang="scss" scoped>
  105. .dndList {
  106. background: #fff;
  107. padding-bottom: 40px;
  108. &:after {
  109. content: "";
  110. display: table;
  111. clear: both;
  112. }
  113. .dndList-list {
  114. float: left;
  115. padding-bottom: 30px;
  116. &:first-of-type {
  117. margin-right: 2%;
  118. }
  119. .dragArea {
  120. margin-top: 15px;
  121. min-height: 50px;
  122. padding-bottom: 30px;
  123. }
  124. }
  125. }
  126. .list-complete-item {
  127. cursor: pointer;
  128. position: relative;
  129. font-size: 14px;
  130. padding: 5px 12px;
  131. margin-top: 4px;
  132. border: 1px solid #bfcbd9;
  133. transition: all 1s;
  134. }
  135. .list-complete-item-handle {
  136. overflow: hidden;
  137. text-overflow: ellipsis;
  138. white-space: nowrap;
  139. margin-right: 50px;
  140. }
  141. .list-complete-item-handle2 {
  142. overflow: hidden;
  143. text-overflow: ellipsis;
  144. white-space: nowrap;
  145. margin-right: 20px;
  146. }
  147. .list-complete-item.sortable-chosen {
  148. background: #4AB7BD;
  149. }
  150. .list-complete-item.sortable-ghost {
  151. background: #30B08F;
  152. }
  153. .list-complete-enter,
  154. .list-complete-leave-active {
  155. opacity: 0;
  156. }
  157. </style>