ImgDragSort.vue 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <template>
  2. <div>
  3. <draggable @end="end" v-model="dataArr" item-key="id" style="display: flex">
  4. <template #item="{ element }">
  5. <div class="imgDiv">
  6. <img :src="element.filePath" preview="index" />
  7. </div>
  8. </template>
  9. <template #footer>
  10. <a-button @click="sureChange" type="primary" style="margin-top: 100px">确定</a-button>
  11. </template>
  12. </draggable>
  13. <a-divider>json数据</a-divider>
  14. <a-row>
  15. <a-col :span="12">
  16. <p>拖拽前:</p>
  17. <textarea rows="25" style="width: 780px">{{ oldDateSource }}</textarea>
  18. </a-col>
  19. <a-col :span="12">
  20. <p>拖拽后:</p>
  21. <textarea rows="25" style="width: 780px">{{ newDateSource }}</textarea>
  22. </a-col>
  23. </a-row>
  24. </div>
  25. </template>
  26. <script lang="ts" setup>
  27. import draggable from 'vuedraggable';
  28. import { defineComponent, ref, unref } from 'vue';
  29. const mockData = [
  30. { id: '000', sort: 0, filePath: 'https://static.jeecg.com/upload/test/1_1588149743473.jpg' },
  31. { id: '111', sort: 1, filePath: 'https://static.jeecg.com/upload/test/u27356337152749454924fm27gp0_1588149731821.jpg' },
  32. { id: '222', sort: 2, filePath: 'https://static.jeecg.com/upload/test/u24454681402491956848fm27gp0_1588149712663.jpg' },
  33. { id: '333', sort: 3, filePath: 'https://static.jeecg.com/temp/国炬软件logo_1606575029126.png' },
  34. { id: '444', sort: 4, filePath: 'https://static.jeecg.com/upload/test/u8891206113801177793fm27gp0_1588149704459.jpg' },
  35. ];
  36. //数据集
  37. const dataArr = ref(mockData);
  38. //原始数据
  39. const oldDateSource = ref(mockData);
  40. //更改后的数据
  41. const newDateSource = ref([]);
  42. /**
  43. * 拖动结束事件
  44. * @param evt
  45. */
  46. function end(evt) {
  47. console.log('拖动前的位置' + evt.oldIndex);
  48. console.log('拖动后的位置' + evt.newIndex);
  49. }
  50. /**
  51. * 确认更改事件
  52. * @param evt
  53. */
  54. function sureChange() {
  55. for (let i = 0; i < unref(dataArr).length; i++) {
  56. dataArr.value[i].sort = i;
  57. }
  58. newDateSource.value = unref(dataArr);
  59. }
  60. </script>
  61. <style scoped lang="less">
  62. .imgDiv {
  63. padding: 8px;
  64. border: 1px solid #d9d9d9;
  65. border-radius: 4px;
  66. margin: 0 8px 8px 0;
  67. img {
  68. width: 120px;
  69. height: 120px;
  70. object-fit: cover;
  71. }
  72. }
  73. </style>