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>
相关推荐
foundbug99919 小时前
Modbus协议C语言实现(易于移植版本)
java·c语言·前端
Luna-player19 小时前
在前端中list.map的用法
前端·数据结构·list
用户479492835691519 小时前
面试官问 React Fiber,这一篇文章就够了
前端·javascript·react.js
LYFlied19 小时前
【一句话概述】Webpack、Vite、Rollup 核心区别
前端·webpack·node.js·rollup·vite·打包·一句话概述
用户8417948145619 小时前
vxe-table 实现滚动加载数据,无限加载数据教程
vue.js
reddingtons20 小时前
PS 参考图像:线稿上色太慢?AI 3秒“喂”出精细厚涂
前端·人工智能·游戏·ui·aigc·游戏策划·游戏美术
一水鉴天20 小时前
整体设计 定稿 之23+ dashboard.html 增加三层次动态记录体系仪表盘 之2 程序 (Q199 之2) (codebuddy)
开发语言·前端·javascript
刘发财20 小时前
前端一行代码生成数千页PDF,dompdf.js新增分页功能
前端·typescript·开源
_请输入用户名20 小时前
Vue 3 源码项目结构详解
前端·vue.js
前端无涯20 小时前
Vue3---(2)setup
vue.js