vue中使用Sortable.js实现拖拽功能

安装Sortable.js
npm install sortablejs --save

<template>
  <div id="box">
    <!--  ref就是为了获取el对象  -->
    <div ref="item1" class="box">
      <div>item 1</div>
      <div>item 2</div>
      <div>item 3</div>
    </div>
    <div ref="item2" class="box">
      <div>item 1</div>
      <div>item 2</div>
      <div>item 3</div>
    </div>
  </div>
  <div id="box1" ref="item3">
    <div class="box_1">1</div>
    <div class="box_2">2</div>
    <div class="box_3">3</div>
    <div class="box_4">4</div>
    <div class="box_5">5</div>
    <div class="box_6">6</div>
    <div class="box_7">7</div>
    <div class="box_8">8</div>
    <div class="box_9">9</div>
  </div>
</template>

<script>
// 引入 sortablejs
import Sortable from 'sortablejs';
import {onMounted, ref} from "vue";
export default {
  name: "SortableJS",
  setup(){
    const item1 = ref(null)
    const item2 = ref(null)
    const item3 = ref(null)
    onMounted(()=>{
      Sortable.create(item1.value,{
        //动画效果
        animation: 1000,
        // 是否执行两个盒子之间互换
        group:'box'
      })
      Sortable.create(item2.value,{
        //动画效果
        animation: 1000,
        // 是否执行两个盒子之间互换,就是不同盒子之间可以互换
        group:'box'
      })
      Sortable.create(item3.value,{
        //动画效果
        animation: 1000,
      })
      console.log(Sortable.create(item1.value))
      console.log(Sortable.create(item2.value))
      console.log(Sortable.create(item3.value))
    })
    return{
      item1,
      item2,
      item3
    }
  }
}
</script>

<style scoped lang="less">
  #box{
    width: 100%;
    display: flex;
    margin-top: 20px;
    grid-gap: 20px;
    div{
      flex: 1;

      div{
        cursor: move;
        width: 100%;
        height: 100px;
        background: #ccc;
        margin-bottom: 20px;
      }
    }
  }
  #box1{
    display: grid;
    // 每一列多少个
    grid-template-columns: repeat(3,auto);
    // 每一行多少个
    grid-template-rows: repeat(3,auto);
    // 行与行之间的距离
    grid-row-gap: 20px;
    // 列与列之间的距离
    grid-column-gap: 20px;
    text-align: center;
    height: 300px;
    width: 500px;
    div{
      font-size: 20px;
      cursor: move;
    }
    .box_1{
      background: rebeccapurple;
    }
    .box_2{
      background: bisque;
    }
    .box_3{
      background: yellow;
    }
    .box_4{
      background: blue;
    }
    .box_5{
      background: slategray;
    }
    .box_6{
      background: chartreuse;
    }
    .box_7{
      background: fuchsia;
    }
    .box_8{
      background: saddlebrown;
    }
    .box_9{
      background: aqua;
    }
  }
</style>


效果图

在这里插入图片描述

实现拖拽变化

在这里插入图片描述

Github地址:https://github.com/SortableJS/Sortable

中文文档地址:https://www.itxst.com/sortablejs/neuinffi.html

官网地址:http://www.sortablejs.com/index.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Vue2使用Sortable.js实现两个el-table之间的树形结构拖拽功能,可以按照以下步骤进行操作: 1. 安装Sortable.jsVue-Sortable插件: ```bash npm install sortablejs vue-sortable ``` 2. 在Vue组件引入并使用Vue-Sortable插件: ```vue <template> <div> <el-table :data="treeData1" :row-key="row => row.id" ref="table1" > <!-- 表格列定义 --> </el-table> <el-table :data="treeData2" :row-key="row => row.id" ref="table2" > <!-- 表格列定义 --> </el-table> </div> </template> <script> import Sortable from 'sortablejs'; import { VueSortable } from 'vue-sortable'; export default { components: { VueSortable }, data() { return { treeData1: [ // 表格数据 ], treeData2: [ // 表格数据 ] }; }, mounted() { this.$nextTick(() => { this.makeTablesSortable(); }); }, methods: { makeTablesSortable() { Sortable.create(this.$refs.table1.$el, { group: 'table', animation: 150, onEnd: this.onTableDragend }); Sortable.create(this.$refs.table2.$el, { group: 'table', animation: 150, onEnd: this.onTableDragend }); }, onTableDragend(event) { const sourceTable = event.from === this.$refs.table1.$el ? 'table1' : 'table2'; const targetTable = event.to === this.$refs.table1.$el ? 'table1' : 'table2'; const sourceData = this[sourceTable]; const targetData = this[targetTable]; // 根据event对象的信息,更新sourceData和targetData的顺序 } } }; </script> ``` 在上面的示例代码,我们使用Vue-Sortable插件将Sortable.js应用于el-table。通过在mounted钩子函数调用makeTablesSortable方法,我们创建了两个可拖拽Sortable实例,并将其应用于el-table组件。在onEnd回调函数,我们可以根据拖拽事件的信息来更新sourceData和targetData的顺序,从而实现树形结构的拖拽功能。 请注意,上述代码仅提供了一个基本的示例,具体的更新逻辑需要根据你的业务需求来实现。 希望以上信息对你有所帮助!如果有任何问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值