vxe-table实现表格行拖拽

方式一:vex-table + sortablejs

1.插件文档

vex-table:https://vxetable.cn/v3/#/table/base/basic

sortablejs: http://www.sortablejs.com/

2.引入插件

vxe-table:

javascript 复制代码
import VXETable from 'vxe-table'
import 'vxe-table/lib/style.css'

Vue.use(VXETable)

sortablejs:

javascript 复制代码
import Sortable from "sortablejs";

3.核心拖拽函数

javascript 复制代码
rowDrop() {
      this.$nextTick(() => {
        let xTable = this.$refs.xTable;
        this.sortable = Sortable.create(
          xTable.$el.querySelector(".body--wrapper>.vxe-table--body tbody"),
          {
            handle: ".vxe-body--row",
            animation: 150,
            onEnd: ({ newIndex, oldIndex }) => { // 拖拽后回调
	            /*
	            常规想法
				此方法数组处理正常,但表格渲染顺序有问题
	            */
				// let currRow = this.tableData.splice(oldIndex, 1)[0];
	            // this.tableData.splice(newIndex, 0, currRow);
           
             // 解决方案
              this.tableData.splice(newIndex, 0, this.tableData.splice(oldIndex, 1)[0]);
              var newArray = this.tableData.slice(0);
              this.tableData = [];
              this.$nextTick(function () {
                this.tableData = newArray;
              });
            },
          }
        );
      });
    },

渲染问题解决方法链接:sortablejs拖拽列表渲染问题

4.全代码

javascript 复制代码
<template>
  <div>
    <vxe-table
      ref="xTable"
      resizable
      show-overflow
      :data="tableData"
      :edit-config="{ trigger: 'click', mode: 'row', icon: ' ' }"
    >
      <vxe-column type="seq" width="60"></vxe-column>
      <vxe-column field="name" title="姓名"> </vxe-column>
      <vxe-column field="sex" title="性别" :edit-render="{}"> </vxe-column>
      <vxe-column title="操作" width="160"> </vxe-column>
    </vxe-table>
  </div>
</template>

<script>
import Sortable from "sortablejs";
export default {
  data() {
    return {
      tableData: [
        { id: 10001, name: "张三", sex: "男" },
        { id: 10002, name: "李四", sex: "男" },
        { id: 10003, name: "王五", sex: "男" },
      ],
    };
  },
  mounted() {
    this.rowDrop();
  },
  beforeDestroy() {
    if (this.sortable) {
      this.sortable.destroy();
    }
  },
  methods: {
    // 行拖动
    rowDrop() {
      this.$nextTick(() => {
        let xTable = this.$refs.xTable;
        this.sortable = Sortable.create(
          xTable.$el.querySelector(".body--wrapper>.vxe-table--body tbody"),
          {
            handle: ".vxe-body--row",
            animation: 150,
            onEnd: ({ newIndex, oldIndex }) => {
              this.tableData.splice(newIndex, 0, this.tableData.splice(oldIndex, 1)[0]);
              var newArray = this.tableData.slice(0);
              this.tableData = [];
              this.$nextTick(function () {
                this.tableData = newArray;
              }); 
            },
          }
        );
      });
    },
  },
};
</script>

方式二:ant+sortablejs

全代码

javascript 复制代码
<template>
  <a-table ref="xTable" :columns="columns" :data-source="tableData"></a-table>
</template>

<script>
import Sortable from "sortablejs";
const columns = [
  {
    title: '#',
    key: 'index',
    customRender(text,row,index){
      return index+1
    }
  },
  {
    title: 'name',
    dataIndex: 'name',
    key: 'name',
  },
  {
    title: 'age',
    dataIndex: 'age',
    key: 'age',
  }
];

const tableData = [
  { name: "张三", status: "1" },
  { name: "李四", status: "1" },
  { name: "王五", status: "0" },
];
export default {
  data() {
    return {
      columns,
      tableData
    };
  },
  mounted() {
    this.rowDrop();
  },
  methods: {
    // 行拖动
    rowDrop() {
        let xTable = this.$refs.xTable;
        this.sortable = Sortable.create(
          xTable.$el.querySelector(".ant-table-wrapper .ant-table-content>.ant-table-body .ant-table-tbody"),
          {
            handle: ".ant-table-row",
            animation: 200,
            onEnd: ({ newIndex, oldIndex }) => {
              this.tableData.splice(newIndex, 0, this.tableData.splice(oldIndex, 1)[0]);
              var newArray = this.tableData.slice(0);
              this.tableData = [];
              this.$nextTick(function () {
                this.tableData = newArray;
              });
            },
          }
        );
    }
  },
};
</script>
相关推荐
GISBox8 分钟前
GIS新手入门首选!GISBox中文界面+一键安装,零依赖轻松搞定三维数据发布
vue.js·json·gis
谢小飞10 分钟前
Echarts高级柱状图开发:渐变与3D效果实现
前端·echarts
FogLetter13 分钟前
Vite vs Webpack:前端构建工具的双雄对决
前端·面试·vite
tianchang15 分钟前
JS 排序神器 sort 的正确打开方式
前端·javascript·算法
怪可爱的地球人18 分钟前
ts的类型兼容性
前端
方圆fy25 分钟前
探秘Object.prototype.toString(): 揭开 JavaScript 深层数据类型的神秘面纱
前端
FliPPeDround28 分钟前
🚀 定义即路由:definePage宏如何让uni-app路由配置原地起飞?
前端·vue.js·uni-app
怪可爱的地球人29 分钟前
ts的类型推论
前端
林太白35 分钟前
动态角色权限和动态权限到底API是怎么做的你懂了吗
前端·后端·node.js
每一天,每一步40 分钟前
React页面使用ant design Spin加载遮罩指示符自定义成进度条的形式
前端·react.js·前端框架