el-upload组件实现上传拖拽排序图片顺序

10 篇文章 0 订阅
9 篇文章 0 订阅

之前是使用vuedraggable完成了一次上传拖拽的使用

但是在完成过程中css样式修改上传图标和拖拽区域不能很好的组合在一起

刷到了vuedraggable组件的js方法  sortablejs

Sortable.js是一款优秀的js拖拽库,支持ie9及以上版本ie浏览器和现代浏览器,也可以运行在移动触摸设备中。不依赖jQuery。支持 Meteor、AngularJS、React、Vue、Knockout框架和任何CSS库,如Bootstrap、Element UI。你可以用来拖拽div、table等元素。

相关文档推荐:
官方文档 https://github.com/SortableJS/Sortable
中文文档 https://www.itxst.com/sortablejs

需要进行npm安装 

$ npm install sortablejs --save
<template>
   <el-upload ref="ImageChang" :file-list.sync="photoList"></el-upload>
 </template>
 <script>
 import Sortable from 'sortablejs';
 export default {
   data() {
     return {
       photoList:[]
     };
   },
   mounted() {
     this.initDragSort();
   },
   methods: {
    // 支持拖拽排序
     initDragSort() {
       const el = this.$refs.ImageChang.$el.querySelectorAll('.el-upload-list')[0];
       Sortable.create(el, {
         onEnd: ({ oldIndex, newIndex }) => {
           // 交换位置
           const arr = this.photoList;
           const page = arr[oldIndex];
           arr.splice(oldIndex, 1);
           arr.splice(newIndex, 0, page);
         }
       });
     }
   }
 };
 </script>

  • 8
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
el-uploadElement UI 提供的一个文件上传组件,可以方地实现文件上传功能。要实现多张上传排序的功能,可以按照以下步骤进行操作: 1. 首先,在 el-upload 组件设置 multiple 属性为 true,以支持多文件上传。 2. 在上传成功后的回调函数,将上传成功的文件保存到一个数组,用于后续的排序操作。 3. 在页面上展示已上传的文件列表时,可以使用 v-for 指令遍历保存上传成功文件的数组,并显示文件名和排序按钮。 4. 为排序按钮绑定点击事件,在点击时触发排序操作。 5. 排序操作可以使用 JavaScript 的数组排序方法进行实现,根据需要可以选择升序或降序排序。 6. 排序完成后,更新保存上传成功文件的数组,并重新渲染文件列表。 下面是一个示例代码: ```html <template> <el-upload action="/upload" :multiple="true" :on-success="handleUploadSuccess" > <el-button slot="trigger">点击上传</el-button> </el-upload> <div v-for="(file, index) in uploadedFiles" :key="file.name"> {{ file.name }} <el-button @click="sortFiles">排序</el-button> </div> </template> <script> export default { data() { return { uploadedFiles: [], // 保存上传成功的文件 }; }, methods: { handleUploadSuccess(response, file) { // 上传成功后保存文件到数组 this.uploadedFiles.push(file); }, sortFiles() { // 对保存的文件数组进行排序 this.uploadedFiles.sort((a, b) => a.name.localeCompare(b.name)); }, }, }; </script> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值