vue+element-pus实现表格拖拽功能

一、引入插件(sortable.js)

npm install sortable.js --save

二、Sortable使用

import Sortable from 'sortablejs';

三、实例

html部分

 element table务必指定row-key,row-key必须是唯一的,如ID,不然会出现排序不对的情况。

<el-table
     :data="list"
     row-key="id"
>
     <el-table-column prop="lineNo" label="专线号" width="120px" sortable></el-table-column>
     <el-table-column prop="aNe" label="源端网元" width="250px" sortable></el-table-column>
     <el-table-column prop="zNe" label="宿端网元" width="250px" sortable></el-table-column>
</el-table>

js部分

<script>
export default{
    data(){
        return{
        // 拖拽表头数据
          dropCol: [
            {
              label: "专线号",
              prop: "lineNo"
            },
            {
              label:"源端网元",
              prop:"aNe"
            },
            {
              label:"宿端网元",
              prop:"zNe"
            }
          ],
          // 行拖拽
          tableData:[]
        }
    },
    methods:{
        //列拖拽
        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);
            }
          });
        },
        //行拖拽
        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)
            }
          })
        }
    },
    mounted() {
    this.getTableList();
    this.columnDrop();
  },
}
</script>

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值