sortablejs 瞎写写

<template>
  <el-table
    ref="elTable"
    class="tables"
    size="mini"
    :data="tableData"
    style="width: 100%"
    :row-key="tableId"
  >
    <el-table-column label="序号" width="40" align="center" type="index" key="">
      <template slot-scope="scope">
        <div type="text" class="drag_btn">
          {{ getIndex(scope.$index) }}
        </div>
      </template>
    </el-table-column>
    <el-table-column
      v-for="item in tableColConfig"
      :key="item.name"
      :prop="item.name"
      :label="item.label"
    >
      <!-- 如果存在 slotName 属性,则加入动态插槽 -->
      <template #default="scope">
        <template v-if="item.slotName">
          <slot :name="item.slotName" :row="scope.row"></slot>
        </template>
        <template v-else>
          <span>{{ scope.row[item.name] }}</span>
        </template>
      </template>
    </el-table-column>
    <template slot="empty">
      <EmptyTable :title="emptyTitle" :custom-height="154" />
    </template>
  </el-table>
</template>

<script>
import Sortable from 'sortablejs'
export default {
  name: 'DragTable',
  props: {
    // 实际显示的表格数据
    tableData: {
      type: Array,
      default: () => []
    },
    // 配置表格的数据
    tableColConfig: {
      type: Array,
      default: () => []
    },
    // tableId
    tableId: {
      type: String,
      default: 'table1'
    },
    emptyTitle: {
      type: String,
      default: '暂无数据'
    }
  },
  data() {
    return {
      isShowIndex: true
    }
  },
  mounted() {
    // 获取el-table DOM
    const el = document.querySelector('.tables .el-table__body-wrapper tbody')
    new Sortable(el, {
      animation: 150,
      ghostClass: 'blue-background-class',
      handle: '.drag_btn', // 如果需要点击某个图标拖拽的话需要吧那个图标的class写在这里
      // 写完以上部分就已经可以实现基本的拖拽功能了,以下是拓展
      //始拖拽事件
      onEnd: function (/**Event*/ evt) {
        let newIndex = evt.newIndex // 排序后的索引位置
        let oldIndex = evt.oldIndex // 排序前的索引位置
        var itemEl = evt.item // 拖拽 HTMLElement
        evt.to // 目标表
        evt.from // 上一个列表
        evt.oldIndex // 元素在旧父级中的旧索引
        evt.newIndex // 元素在新父级中的新索引
        evt.clone // 克隆元件
        evt.pullMode // 当项目在另一个可排序表中时:`“clone”`如果克隆,`true`如果移动
        const El = document.querySelectorAll('.drag_btn')
        El[evt.oldIndex].innerHTML = evt.oldIndex + 1
        El[evt.newIndex].innerHTML = evt.newIndex + 1
      },
      //点击选中元素事件
      onChoose: function (/**Event*/ evt) {},
      //取消选中事件
      onUnchoose: function (/**Event*/ evt) {}
    })
  },
  methods: {
    getIndex(v) {
      return v + 1
    }
  }
}
</script>

<style lang="scss" scoped>
.drag_btn:hover{
  cursor: move;
}
</style>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值