element ui el-table 实现拖拽效果

https://blog.youkuaiyun.com/weixin_46527264/article/details/111693851  参考这个

 

 

https://www.cnblogs.com/jin-zhe/p/10181852.html

https://www.cnblogs.com/e0yu/p/11163924.html

引入三方插件   sortable.js

 

1.引入sortable.js的包:

npm install sortablejs --save

2. 引用el-table 中   导入

import Sortable from 'sortablejs'

3.mounted(){

 this.rowDrop()
    this.columnDrop()

}

4.

//行拖拽           //tableData 表格数据
    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)
        }
      })
    },
    //列拖拽             dropCol 表格定义的header
    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)
        }
      })
    }
 
### 实现 Vue 和 Element UI 中 `el-table` 组件的行和列拖拽排序 #### 行拖拽排序 为了实现拖拽排序,在 HTML 部分,需要配置 `el-table` 的一些必要属性: - 添加 `ref` 属性以便 JavaScript 可以访问组件实例。 - 设置唯一的 `row-key` 来标识每一行的数据项。 对于拖拽操作的具体实施,可以通过引入 Sortable.js 库来增强原生的功能。以下是具体的代码示例: ```html <template> <div> <!-- 定义表格 --> <el-table :data="tableData" ref="dragTable" row-key="id"> <el-table-column prop="name" label="名称"></el-table-column> <el-table-column prop="value" label="数值"></el-table-column> <el-table-column class-name="allowDrag" align="center" width="80px"> <i slot-scope="{ row }" @click="handleRowClick(row)" class="el-icon-drag-dot"/> </el-table-column> </el-table> </div> </template> <script> import { Table, TableColumn } from 'element-ui'; import Sortable from 'sortablejs'; export default { components: { ElTable: Table, ElTableColumn: TableColumn }, data() { return { tableData: [ { id: 1, name: "Item A", value: "Value A" }, { id: 2, name: "Item B", value: "Value B" } ] }; }, mounted() { const el = this.$refs.dragTable.$el.querySelectorAll('.el-table__body-wrapper > table > tbody')[0]; this.sortable = Sortable.create(el, { ghostClass: 'sortable-ghost', // Class name for the drop placeholder onEnd: evt => { const targetRow = this.tableData.splice(evt.oldIndex, 1)[0]; this.tableData.splice(evt.newIndex, 0, targetRow); // 更新到服务器或其他逻辑处理... } }); }, methods: { handleRowClick(row) { console.log('点击了:', row); } } }; </script> <style scoped> .sortable-ghost{ opacity: .8; color: #fff!important; background: red !important; } </style> ``` 上述代码实现了基于 `SortableJS` 的行拖拽效果,并通过自定义事件监听器捕获拖放完成后的索引变化并调整数组顺序[^1]。 #### 列拖拽排序 针对列的重新排列,则主要依赖于 `ElementUI` 自身提供的插槽机制以及 CSS 类名控制。这里的关键在于利用 `header-cell-class-name` 或者直接修改表头单元格样式的方式指定哪些列为可拖动状态。不过需要注意的是,官方并没有直接支持列间互换位置的操作,因此可能还需要借助第三方库如 `draggable` 或者手动编写相应逻辑来进行交互设计。 由于涉及到更复杂的 DOM 操作与样式管理,建议开发者先熟悉基础的行级拖拽案例后再尝试扩展至列级别的功能开发。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值