vue使用sortable.js实现文件拖拽

vue的Draggable拖拽:https://github.com/SortableJS/Vue.Draggable
sortablejs拖拽: https://www.npmjs.com/package/sortablejs
官方拖动的效果:http://sortablejs.github.io/Sortable/

1.安装sortable.js

npm install sortablejs --save


image.png

2.导入sortable并使用

在组件中导入sortable

import Sortable from 'sortablejs'

在组件中使用


image.png

以上画框打印台打印结果:

 onUpdate: (event) => {
        console.log('event值为:', event, event.newIndex, event.oldIndex)
        this.onUpEvent(event)
      }
image.png
  onUpEvent (e) {
      var item = this.friends[e.oldIndex]
      console.log(item)
      this.friends.splice(e.oldIndex, 1)
      this.friends.splice(e.newIndex, 0, item)
      console.log('拖动后的数据显示', this.friends)
    }

该方法被调用后就会修改friends数组中数据顺序,friends拖拽后数据顺序如下


image.png
image.png
image.png

以上组件完整代码如下:

<template>
  <div class="helloWord">
    <h3>朋友列表</h3>
    <div id="list">梧桐芊雨
      <mt-cell class="cellmat" v-for="(item,index) in friends" :key="index"
        :title="item.name"
        :value="item.address"></mt-cell>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
import Sortable from 'sortablejs'
export default {
  name: 'HelloWorld',
  data () {
    return {
      friends: [],
      sortList: Object
    }
  },
  methods: {
    getFriend () {
      axios.get('../../static/mock/friend.json').then(this.getFriendInfo)
    },
    getFriendInfo (res) {
      if (res.data.success) {
        this.friends = res.data.data.friend
      }
    },
    onUpEvent (e) {
      var item = this.friends[e.oldIndex]
      console.log(item)
      this.friends.splice(e.oldIndex, 1)
      this.friends.splice(e.newIndex, 0, item)
      console.log('拖动后的数据显示', this.friends)
    }
  },
  mounted () {
    this.getFriend()
    var $ul = this.$el.querySelector('#list')
    this.sortList = Sortable.create($ul, {
      handle: '.cellmat',
      animation: 150,
      ghostClass: 'blue-background-class',
      onUpdate: (event) => {
        console.log('event值为:', event, event.newIndex, event.oldIndex)
        this.onUpEvent(event)
      }
    })
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>

5.拖动效果

将第一个拖动到最后一个,其效果如下:


未拖拽.png
已拖拽.png

若感兴趣,你可以使用以下文档来实现。
相关文档:
https://blog.csdn.net/A_bet_of_three_years/article/details/78417454

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 基于Vue的一些资料 内容 UI组件 开发框架 实用库 服务端 辅助工具 应用实例 Demo示例 element★...
    尝了又尝阅读 1,187评论 0 1
  • 简说Vue (组件库) https://github.com/ElemeFE/element" 饿了么出品的VUE...
    Estrus丶阅读 1,808评论 0 1
  • 终于可以躺在舒适的床上身个懒腰了,时间过得真快,四点半从店里出发回家,五点和老小买好菜,急急忙忙往家赶。 ...
    小桔子之家阅读 353评论 0 1
  • 转眼间又到了立秋时节。一年之计在于春,一叶落而知天下秋。中午时分还下了一场雨,应了这个自古逢秋悲寂寥的秋景。 秋季...
    阿钵阅读 281评论 0 0
  • 各位简书的朋友们,大家好: 本人,刘裕波,2011年开始在M公司电话销售中心人力发展与培训部负责人,授证集团...
    刘裕波阅读 366评论 0 0