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>

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Sortable.js是一款优秀的JavaScript拖拽库,它可以在IE9及以上版本的IE浏览器和现代浏览器中运行,也可以在移动触摸设备上使用。它不依赖于jQuery,并且支持多种框架和CSS库,如Meteor、AngularJS、React、Vue、Knockout、Bootstrap和Element UI。你可以使用它来实现拖拽功能,比如拖拽div、table等元素。在Vue文件中使用Sortable.js,你需要在文件头部导入组件,并在created()方法中调用listDrop()方法来初始化拖拽功能。listDrop()方法中,你需要选择要拖拽的元素,然后使用Sortable.create()方法来创建拖拽实例,并在onEnd回调函数中处理拖拽结束后的逻辑,比如重新排序列表。\[1\]\[2\] #### 引用[.reference_title] - *1* [sortablejs — 强大的拖拽库](https://blog.csdn.net/huangpb123/article/details/120539533)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [vue+element 使用sortablejs的可拖拽列表](https://blog.csdn.net/mmmmmmmmzw/article/details/123116123)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值