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

简介: 基于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>


相关文章
|
3天前
|
前端开发
Element UI 【实战】纯前端对表格数据进行增删改查(内含弹窗表单、数据校验、时间日期格式)
Element UI 【实战】纯前端对表格数据进行增删改查(内含弹窗表单、数据校验、时间日期格式)
24 6
|
1天前
Element UI 多选表格--判断勾选数据行的 Checkbox 时为选中还是取消选中
Element UI 多选表格--判断勾选数据行的 Checkbox 时为选中还是取消选中
9 1
|
1天前
Element UI 多选表格【翻页多选】简易版(不支持翻页多选数据反显)
Element UI 多选表格【翻页多选】简易版(不支持翻页多选数据反显)
3 0
Element UI 多选表格【翻页多选】简易版(不支持翻页多选数据反显)
|
1天前
|
前端开发
Element UI 表格常用改造(表头添加注释、翻页连续序号【内含前端分页】)
Element UI 表格常用改造(表头添加注释、翻页连续序号【内含前端分页】)
5 0
|
1天前
|
JavaScript
vue项目中升级element ui(含常见报错及解决方案,如表格不显示,el-table无效, “__v_isRef“ is not defined,Use :deep() instead)
vue项目中升级element ui(含常见报错及解决方案,如表格不显示,el-table无效, “__v_isRef“ is not defined,Use :deep() instead)
3 0
|
1天前
Element UI 多选表格【翻页多选】全能版(含翻页多选数据反显、toggleRowSelection失效的原因解析和解决方案)
Element UI 多选表格【翻页多选】全能版(含翻页多选数据反显、toggleRowSelection失效的原因解析和解决方案)
7 0
|
3天前
Element UI 表格数据格式化
Element UI 表格数据格式化
5 0
|
2月前
|
前端开发 搜索推荐 开发者
SAP UI5 sap.m.Column 控件的 minScreenWidth 属性介绍
SAP UI5 sap.m.Column 控件的 minScreenWidth 属性介绍
|
2月前
|
JavaScript 前端开发 开发者
SAP UI5 控件 sap.m.ListBase 的 inset 属性的作用介绍
SAP UI5 控件 sap.m.ListBase 的 inset 属性的作用介绍
|
2月前
|
前端开发 JavaScript API
SAP UI5 sap.ui.require.toUrl 的作用介绍
SAP UI5 sap.ui.require.toUrl 的作用介绍