elementui表格列拖动(使用sortablejs插件)

54 篇文章 0 订阅
sortablejs文档:https://segmentfault.com/a/1190000008209715

1.下载并引入sortablejs插件

import Sortable from 'sortablejs'

2.渲染表格

<el-table ref='table' :data="tableData" stripe :height="relHeight" :row-class-name="tableRowClassName"
  @selection-change="handleSelectionChange" @row-contextmenu="rowMenu" style="width: 100%" v-loading="listLoading">
  <el-table-column class-name="cannotDrag" v-if="showSelection" type="selection" width="55" /> <!--checkbox-->
  <el-table-column class-name="cannotDrag" label='序号' fixed type="index" min-width="50" max-width="50" />
  <el-table-column :label='$t("tableTitle.code")' prop="code" sortable class-name="cannotDrag"></el-table-column>
<el-table-column v-for="(item,index) in col" sortable :key="index" :prop="dropCol[index].prop" :label="item.label"
 v-if="item.label!=''" :class-name="index==1?'cannotDrag':'canDrag'"></el-table-column>
<el-table-column class-name="cannotDrag" :label='$t("tableTitle.handle")' width="120">
   <template slot-scope="scope">
      <el-button size="mini" @click="openDraw('1',scope.row)">修改</el-button>
      <el-button size="mini" type="danger" @click="handleDelete(scope.$index,scope.row)">删除</el-button>
   </template>
</el-table-column>
</el-table>

<!--不需要拖动的列添加类名cannotDrag,需要拖动的列添加类名canDrag,两个都要添加,如果只添加一个会出现部分仍然能拖动的情况-->

3.js部分

data() {
   return {
      col: [   //th和对于的td字段
          {
            label: "",    //label为空的表示为序号和checkbox,占位
            prop: "",
            isConcat: false
         },
         {
            label: "",
            prop: "",
            isConcat: false
         },
         {
            label: this.$t("tableTitle.chineseName"),
            prop: "cn",
            isConcat: false
         },
         {
            label: this.$t("tableTitle.englishName"),
            prop: "en",
            isConcat: false
         },
         {
            label: this.$t("tableTitle.areaType"),
            prop: "type",
            isConcat: false
         },
         {
            label: this.$t("tableTitle.editUser"),
            prop: "modifyuser",
            isConcat: false
         },
         {
            label: this.$t("tableTitle.editDate"),
            prop: "modifytime",
            isConcat: false
         },
         {
            label: this.$t("tableTitle.createUser"),
            prop: "adduser",
            isConcat: false
         },
         {
            label: this.$t("tableTitle.createDate"),
            prop: "createTime",
            isConcat: false
         },

      ],
      dropCol: [  //拖动所需要的th和td,与上面数组一样
          {
            label: "",
            prop: "",
            isConcat: false
         },
         {
            label: "",
            prop: "",
            isConcat: false
         },
         {
            label: this.$t("tableTitle.chineseName"),
            prop: "cn",
            isConcat: false
         },
         {
            label: this.$t("tableTitle.englishName"),
            prop: "en",
            isConcat: false
         },
         {
            label: this.$t("tableTitle.areaType"),
            prop: "type",
            isConcat: false
         },
         {
            label: this.$t("tableTitle.editUser"),
            prop: "modifyuser",
            isConcat: false
         },
         {
            label: this.$t("tableTitle.editDate"),
            prop: "modifytime",
            isConcat: false
         },
         {
            label: this.$t("tableTitle.createUser"),
            prop: "adduser",
            isConcat: false
         },
         {
            label: this.$t("tableTitle.createDate"),
            prop: "createTime",
            isConcat: false
         },

      ],
   }
},
mounted() {
   this.columnDrop()
}
methods: {
   columnDrop() {
      const wrapperTr = document.querySelector('.el-table__header-wrapper tr')
      this.sortable = Sortable.create(wrapperTr, {
         animation: 180,
         delay: 0,
         filter: ".cannotDrag",
         draggable: ".canDrag",
         onEnd: evt => {
            const oldItem = this.dropCol[evt.oldIndex]
            this.dropCol.splice(evt.oldIndex, 1)
            this.dropCol.splice(evt.newIndex, 0, oldItem)
         }
      })
   },
},

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值