vue+element表格拖拽

1、使用命令行安装sortablejs

npm install sortablejs --save

然后再需要表格的vue文件里面引入

import Sortable from 'sortablejs'

2、完整代码

<template>
  <div style="width:800px">

    <el-table :data="tableData"
      border
      row-key="id"
      align="left">
     <el-table-column v-for="(item, index) in col"
        :key="`col_${index}`"
        :prop="dropCol[index].prop"
        :label="item.label"> 
      </el-table-column>
    </el-table>
    <pre style="text-align: left">
      {{dropCol}}
    </pre>
    <hr>
    <pre style="text-align: left">
      {{tableData}}
    </pre>
  </div>
</template>
<script>
import Sortable from 'sortablejs'
export default {
  data() {
    return {
      col: [
        {
          label: '日期',
          prop: 'date'
        },
        {
          label: '姓名',
          prop: 'name'
        },
        {
          label: '地址',
          prop: 'address'
        }
      ],
      dropCol: [
        {
          label: '日期',
          prop: 'date'
        },
        {
          label: '姓名',
          prop: 'name'
        },
        {
          label: '地址',
          prop: 'address'
        }
      ],
      tableData: [
        {
          id: '1',
          date: '2016-05-02',
          name: '王小虎1',
          address: '上海市普陀区金沙江路 100 弄'
        },
        {
          id: '2',
          date: '2016-05-04',
          name: '王小虎2',
          address: '上海市普陀区金沙江路 200 弄'
        },
        {
          id: '3',
          date: '2016-05-01',
          name: '王小虎3',
          address: '上海市普陀区金沙江路 300 弄'
        },
        {
          id: '4',
          date: '2016-05-03',
          name: '王小虎4',
          address: '上海市普陀区金沙江路 400 弄'
        }
      ]
    }
  },
  mounted() {
    this.rowDrop()
    this.columnDrop()
  },
  methods: {
    //行拖拽
    rowDrop() {
      const tbody = document.querySelector('.el-table__body-wrapper tbody')
      const _this = this
      Sortable.create(tbody, {
        onEnd({ newIndex, oldIndex }) {
          const currRow = _this.tableData.splice(oldIndex, 1)[0]
          _this.tableData.splice(newIndex, 0, currRow)
        }
      })
    },
    //列拖拽
    columnDrop() {
      const wrapperTr = document.querySelector('.el-table__header-wrapper tr')
      this.sortable = Sortable.create(wrapperTr, {
        animation: 180,
        delay: 0,
        onEnd: evt => {
          const oldItem = this.dropCol[evt.oldIndex]
          this.dropCol.splice(evt.oldIndex, 1)
          this.dropCol.splice(evt.newIndex, 0, oldItem)
        }
      })
    }
  }
}
</script>
<style scoped>
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现列拖拽功能可以使用 SortableJS 库,并结合 Element Plus 的表格组件和 Vue3 的响应式数据。 首先需要在项目中安装 SortableJS: ``` npm install sortablejs --save ``` 然后在需要使用的组件中引入 SortableJS 和 Element Plus 的表格组件: ```javascript import Sortable from 'sortablejs'; import { ElTable, ElTableColumn } from 'element-plus'; ``` 接着,在组件中定义表格数据和表格列,以及拖拽回调函数: ```javascript export default { data() { return { tableData: [ { name: 'John', age: 20, address: 'New York' }, { name: 'Tom', age: 25, address: 'London' }, { name: 'Lucy', age: 18, address: 'Paris' }, { name: 'Lily', age: 22, address: 'Tokyo' } ], tableColumns: [ { label: 'Name', prop: 'name' }, { label: 'Age', prop: 'age' }, { label: 'Address', prop: 'address' } ] }; }, mounted() { // 绑定拖拽回调函数 const el = this.$refs.table.$el.querySelector('.el-table__body-wrapper tbody'); Sortable.create(el, { animation: 150, onEnd: evt => { const { newIndex, oldIndex } = evt; const item = this.tableColumns.splice(oldIndex - 1, 1)[0]; this.tableColumns.splice(newIndex - 1, 0, item); } }); }, render() { return ( <ElTable ref="table" data={this.tableData}> {this.tableColumns.map(column => ( <ElTableColumn label={column.label} prop={column.prop}></ElTableColumn> ))} </ElTable> ); } }; ``` 这里使用 `Sortable.create` 方法创建一个拖拽对象,并且绑定了 `onEnd` 回调函数,当拖拽结束后,将表格列数组中的相应元素移动到新位置。 最后渲染表格时,使用 `map` 方法动态创建表格列。 这样就实现了列拖拽功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值