vue结合antdv与sortable.js实现表格拖拽排序

vue结合antdv与sortable.js实现表格拖拽排序

最近在工作项目中使用-design-vue做表格时,有个业务需求是需要对表格进行拖拽排序的,但是在antdv官网的表格示例中是并没有对表格进行拖拽排序功能的,然后就在网上查询资料,发现可以使用sortable.js来对表格进行排序,所以写个文章记录一下。感兴趣的朋友可以去了解一下,这个是sortable官网的地址:http://www.sortablejs.com/index.html

效果图:

在这里插入图片描述

一、安装sortable.js

npm install sortablejs --save

二、demo

博客设置页面,选择一款你喜欢的代码片高亮样式,下面展示同样高亮的 代码片.

<template>
  <a-card>
    <div class="demo">
      <a-table
        id="mytb"
        rowKey="key"
        :dataSource="dataSource"
        :columns="columns"
      />
    </div>
  </a-card>
</template>
<script>
import Sortable from "sortablejs";
import { message } from "ant-design-vue";
export default {
  data() {
    return {
      dataSource: [
        {
          key: "1",
          name: "张三",
          age: 32,
          address: "西湖区湖底公园1号",
        },
        {
          key: "2",
          name: "胡彦祖",
          age: 42,
          address: "西湖区湖底公园2号",
        },
        {
          key: "3",
          name: "李四",
          age: 22,
          address: "西湖区湖底公园3号",
        },
      ],
      columns: [
        {
          title: "姓名",
          dataIndex: "name",
          key: "name",
        },
        {
          title: "年龄",
          dataIndex: "age",
          key: "age",
        },
        {
          title: "住址",
          dataIndex: "address",
          key: "address",
        },
      ],
    };
  },
  mounted() {
    //  拖拽事件一定要放在 mounted 生命周期内,因为这时真实的DOM才渲染出来
    this.initSortable();
  },
  methods: {
    //初始化表格拖动
    /**
     * oldIndex:谁
     * newIndex:移动到哪儿
     */
    initSortable() {
      console.log(this.dataSource[0]);
      const mytb = document.querySelector("#mytb tbody");
      const _this = this;
      new Sortable(mytb, {
        handle: ".ant-table-row",
        animation: 150,
        ghostClass: "blue-background-class",
        sort: true,
        onEnd({ newIndex, oldIndex }) {
          console.log(oldIndex, newIndex);
          const currRow = _this.dataSource.splice(oldIndex, 1)[0];
          _this.dataSource.splice(newIndex, 0, currRow);
          message.success(
            "排序完成!!!!位置从" + oldIndex + "移动到" + newIndex
          );
        },
      });
    },
  },
};
</script>

第一次写文章记录,有写的不好的地方欢迎各位大佬指正,谢谢。
參考链接: https://www.cnblogs.com/dshvv/p/15002303.html.

  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值