Element table 实现表格行、列拖拽功能

安装包

npm install sortablejs --save

javascript 复制代码
<template>
  <div class="draggable" style="padding: 20px">
    <el-table row-key="id" :data="tableData" style="width: 100%" border>
      <el-table-column
        v-for="(item, index) in oldList"
        :key="`col_${index}`"
        :prop="newList[index].prop"
        :label="item.label"
        align="center"
      >
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import Sortable from 'sortablejs'

export default {
  mounted () {
    this.oldList = JSON.parse(JSON.stringify(this.tableConfig.tableItems))
    this.newList = JSON.parse(JSON.stringify(this.tableConfig.tableItems))
    this.columnDrop()
    this.rowDrop()
  },
  data () {
    return {
      oldList: [],
      newList: [],
      tableData: [
        {
          id: 1,
          name: '李一',
          gender: '男',
          age: 30,
          job: '会计'
        },
        {
          id: 2,
          name: '王二',
          gender: '未知',
          age: 18,
          job: '无业游民'
        },
        {
          id: 3,
          name: '张三',
          gender: '男',
          age: 50,
          job: '老板'
        }
      ],
      tableConfig: {
        tableItems: [
          {
            label: '姓名',
            prop: 'name'
          },
          {
            label: '性别',
            prop: 'gender'
          },
          {
            label: '年龄',
            prop: 'age'
          },
          {
            label: '工作',
            prop: 'job'
          }
        ]
      }
    }
  },
  methods: {
    // 行拖拽
    rowDrop () {
      // 此时找到的元素是要拖拽元素的父容器
      const tbody = document.querySelector('.draggable .el-table__body-wrapper tbody')
      const _this = this
      Sortable.create(tbody, {
        //  指定父元素下可被拖拽的子元素
        draggable: '.draggable .el-table__row',
        onEnd ({ newIndex, oldIndex }) {
          const currRow = _this.tableData.splice(oldIndex, 1)[0]
          _this.tableData.splice(newIndex, 0, currRow)
        }
      })
    },
    // 列拖拽
    columnDrop () {
      const wrapperTr = document.querySelector('.draggable .el-table__header-wrapper tr')
      this.sortable = Sortable.create(wrapperTr, {
        animation: 180,
        delay: 0,
        onEnd: (evt) => {
          const oldItem = this.newList[evt.oldIndex]
          this.newList.splice(evt.oldIndex, 1)
          this.newList.splice(evt.newIndex, 0, oldItem)
        }
      })
    }
  }
}
</script>
<style scoped></style>
相关推荐
A了LONE16 分钟前
h5的底部导航栏模板
java·前端·javascript
轻语呢喃30 分钟前
JavaScript :事件循环机制的深度解析
javascript·后端
摆烂为不摆烂34 分钟前
😁深入JS(六): 一文让你完全理解浏览器进程与线程
前端·javascript
我血条子呢1 小时前
动态组件和插槽
前端·javascript·vue.js
前端付豪2 小时前
13、表格系统架构:列配置、嵌套数据、复杂交互
前端·javascript·架构
南屿im2 小时前
发布订阅模式和观察者模式傻傻分不清?一文搞懂两大设计模式
前端·javascript
JustHappy2 小时前
SPA?MPA?有啥关系?有啥区别?聊一聊页面形态 or 路由模式
前端·javascript·架构
每天开心2 小时前
🧙‍♂️闭包应用场景之--防抖和节流
前端·javascript·面试
归于尽2 小时前
Generator?从 yield 卡壳,到终于搞懂协程那点事
前端·javascript
FogLetter2 小时前
React组件开发进阶:本地存储与自定义Hooks的艺术
前端·javascript·react.js