el-table使用sortable进行拖拽排序

sortable.js

npm install sortablejs
sortablejs中文网: [http://www.sortablejs.com/options.html]
<template>
  <div>
    <el-table
      :data="tableData"
      id="id"
      ref="sortableTable"
      class="drag-table"
      stripe
    >
      <el-table-column label="姓名" prop="name"></el-table-column>
      <el-table-column label="年龄" prop="age"></el-table-column>
      <el-table-column label="工龄" prop="workage"></el-table-column>
      <el-table-column label="性别" prop="sex"></el-table-column>
    </el-table>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import Sortable from 'sortablejs'
const tableData = ref([
  { id: 1, name: '账单', age: '18', workage: '2', sex: '男' },
  { id: 2, name: '账单1', age: '20', workage: '4', sex: '女' },
  { id: 3, name: '中间', age: '18', workage: '2', sex: '男' },
  { id: 4, name: '出发', age: '20', workage: '4', sex: '女' },
  { id: 5, name: '哦哦', age: '18', workage: '2', sex: '男' },
  { id: 6, name: '鱼鱼', age: '20', workage: '4', sex: '女' }
])
const sortableTable = ref(null)
const handleDragEnd = (event) => {
  const movedItem = tableData.value.splice(event.oldIndex, 1)[0]
  tableData.value.splice(event.newIndex, 0, movedItem)
  console.log(tableData.value, 'tableData.value')
}
onMounted(() => {
  const tableElement = sortableTable.value.$el.querySelector(
    '.el-table__body-wrapper tbody'
  )
  new Sortable(tableElement, {
    animation: 150, // 拖拽时的动画速度,单位为毫秒
    handle: '.el-table__row', // 拖拽的手柄元素,这里使用表格的行作为手柄
    onEnd: handleDragEnd // 拖拽结束时的回调函数
  })
})
</script>

<style lang="less">
.drag-table {
  tbody {
    td {
      &:first-child {
        .cell {
          padding-left: 0;
          display: inline-flex;
          align-items: center;
          &:before {
            content: '';
            display: inline-flex;
            align-items: center;
            background-image: url('./drag.svg') !important;
            background-position: 0 center !important;
            background-repeat: no-repeat !important;
            cursor: grab;
            width: 15px;
            height: 15px;
            margin-right: 5px;
          }
        }
      }
    }
  }

  .blue-background-class {
    td {
      background: #5a8cf0 !important;
      border-top: 1px solid #dcebff;
      border-bottom: 1px solid #dcebff;
      color: #fff;
    }
  }
  .dragHandle {
    cursor: grab !important;
  }
  .grabbing,
  .isSelected,
  .isSelected > .dragHandle {
    cursor: grabbing !important;
  }
}
</style>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值