基于sortablejs实现拖拽element-ui el-table表格行进行排序

可以用原生的dragstart、drag、dragend、dragover、drop、dragleave实现这个效果,但是有现成的轮子就不要重复造了,看效果:

 

<template>
  <el-table :class="$options.name" :data="tableData" ref="table" row-key="ID">
    <!-- 注意:必须要定义row-key="ID",否者会导致拖拽顺序错乱 -->
    <el-table-column type="index" label="序号" width="60" />
    <el-table-column prop="label" label="列名">
      <template slot-scope="scope">
        <el-link :type="scope.row.type" :underline="false">{{ scope.row.label }}</el-link>
      </template>
    </el-table-column>
  </el-table>
</template>
<script>
import sortablejs from "sortablejs"; //npm install sortablejs --save
export default {
  name: "dragTableRow",
  data() {
    return {
      tableData: [
        { ID: 1, value: 1, label: "显示文本1", type: `primary` },
        { ID: 2, value: 2, label: "显示文本2", type: `success` },
        { ID: 3, value: 3, label: "显示文本3", type: `warning` },
        { ID: 4, value: 4, label: "显示文本4", type: `danger` },
        { ID: 5, value: 5, label: "显示文本5", type: `info` },
      ],
    };
  },
  mounted() {
    this.initDragSortTableRow(); //拖拽表格行排序
  },
  methods: {
    initDragSortTableRow() {
      let el = this.$refs.table.$el.querySelectorAll(
        ".el-table__body-wrapper > table > tbody"
      )[0];
      sortablejs.create(el, {
        ghostClass: "ghostClass", //定义拖拽的时候接触到的行样式
        setData: (dataTransfer) => {
          dataTransfer.setData("自定义传参字段", "传输内容");
        },
        onEnd: (e) => {
          this.$g.array.moveArrayElement(this.tableData, e.oldIndex, e.newIndex); //修改数组的顺序
          console.log(e.originalEvent.dataTransfer.getData("自定义传参字段"));
        },
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.dragTableRow {
  >>> .ghostClass {
    background-color: #ecf5ff;
    td {
      border-bottom-color: #409eff;
    }
  }
}
</style>

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-18 09:44:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-18 09:44:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-18 09:44:01       82 阅读
  4. Python语言-面向对象

    2024-03-18 09:44:01       91 阅读

热门阅读

  1. To configure two different databases in Spring Boot

    2024-03-18 09:44:01       38 阅读
  2. odoo中传递上下文

    2024-03-18 09:44:01       44 阅读
  3. React高阶组件详解

    2024-03-18 09:44:01       47 阅读
  4. Flutter 当涉及Listview的复杂滑动布局良好布局方式

    2024-03-18 09:44:01       38 阅读
  5. Python实现连连看

    2024-03-18 09:44:01       41 阅读
  6. 如何优化查询ORM

    2024-03-18 09:44:01       42 阅读
  7. IDEA SpringBoot + Gradle无法运行测试问题

    2024-03-18 09:44:01       40 阅读
  8. Spring Data访问Elasticsearch----Elasticsearch对象映射

    2024-03-18 09:44:01       45 阅读
  9. Spring Boot(七十):利用Jasypt对数据库连接进行加密

    2024-03-18 09:44:01       36 阅读
  10. 如何在MATLAB中处理图像和视频?

    2024-03-18 09:44:01       41 阅读
  11. tcpudp面试题

    2024-03-18 09:44:01       36 阅读