Vue3 + Ts 实现 Ant Design Vue 中 table 组件行拖拽排序

前言

项目整体架构使用的 Vue3 + TypeScript + Ant Design Vue,需求为 table 表格组件支持行数据拖拽排序。本文主要用于记录。

一、新建 useTableDraggable.ts 文件,代码如下。

/**
 * useTableDraggable.ts 表格列拖拽
 * @param dataSource table数据集合
 * @returns customRow 行属性方法
 */
export default <T>(dataSource: Array<T>) => {
  let dragItem: T
  let targItem: T
  const customRow = (record: T) => {
    return {
      draggable: true,
      ondrag(e: DragEvent) {
        dragItem = record
      },
      ondrop(e: DragEvent) {
        targItem = record
      },
      ondragend(e: DragEvent) {
        if (dragItem !== targItem) {
          const dragItemIndex = dataSource.indexOf(dragItem);
          const targItemIndex = dataSource.indexOf(targItem);
          // 解构交换
          [dataSource[dragItemIndex], dataSource[targItemIndex]] = [dataSource[targItemIndex], dataSource[dragItemIndex]]
        }
      },
      ondragover(e: DragEvent) {
        return false
      }
    }
  }

  return {
    customRow
  }
}

二、使用

<template>
  <a-table
    bordered
    :dataSource="dataSource"
    :columns="columns"
    :customRow="customRow"
  ></a-table>
</template>

<script lang="ts">
import { defineComponent, ref} from 'vue'
import useTableDraggable from '@/hooks/useTableDraggable'

export default defineComponent({
  name: 'Table',
  setup() {
    const dataSource = ref([
      { title: '张三', dataIndex: 'name' },
      { title: '男', dataIndex: 'sex' },
      { title: '18', dataIndex: 'age' }
    ])
    const { customRow } = useTableDraggable(dataSource.value)

    return {
      dataSource,
      columns:[
        { title: "名称", dataIndex: "title" },
        { title: "键值", dataIndex: "dataIndex" }
      ],
      customRow
    }
  }
})
</script>

 参考地址:https://github.com/vueComponent/ant-design-vue/issues/1804

下面是一个完整的示例代码,基于Vue2,antd和sortablejs实现了两个表格之间的数据拖拽功能。 ```html <template> <div> <h2>表格1</h2> <a-table :columns="columns" :dataSource="list1" rowKey="id"> <template #name="{text, record}"> <span v-text="text" v-sortable="'table1'"></span> </template> </a-table> <h2>表格2</h2> <a-table :columns="columns" :dataSource="list2" rowKey="id"> <template #name="{text, record}"> <span v-text="text" v-sortable="'table2'"></span> </template> </a-table> </div> </template> <script> import Sortable from 'sortablejs' import { Table } from 'ant-design-vue' export default { components: { 'a-table': Table, }, data() { return { columns: [ { title: '姓名', dataIndex: 'name' }, { title: '年龄', dataIndex: 'age' }, ], list1: [ { id: 1, name: '张三', age: 18 }, { id: 2, name: '李四', age: 20 }, { id: 3, name: '王五', age: 22 }, ], list2: [], } }, mounted() { Sortable.create(this.$el.querySelector('.ant-table-tbody'), { group: { name: 'table1', put: ['table2'], }, animation: 150, fallbackOnBody: true, swapThreshold: 0.65, onEnd: this.onEnd, }) Sortable.create(this.$el.querySelectorAll('.ant-table-tbody')[1], { group: { name: 'table2', put: ['table1'], }, animation: 150, fallbackOnBody: true, swapThreshold: 0.65, onEnd: this.onEnd, }) }, methods: { onEnd(evt) { const { oldIndex, newIndex, item } = evt if (oldIndex !== newIndex) { const table = evt.to.dataset.table if (table === 'table1') { // 从表格1拖拽表格2 this.list2.splice(newIndex, 0, this.list1.splice(oldIndex, 1)[0]) } else { // 从表格2拖拽表格1 this.list1.splice(newIndex, 0, this.list2.splice(oldIndex, 1)[0]) } } }, }, } </script> ``` 在此示例,我们使用了Ant Design Vue的`<a-table>`组件来渲染表格,并在模板使用了Vue的`v-sortable`指令来绑定Sortable实例。在mounted钩子函数,我们为每个表格的tbody元素创建了Sortable实例,并将它们分组为“table1”和“table2”。然后,我们在onEnd方法处理拖拽完成事件,并根据拖拽的方向和目标表格移动数据。 需要注意的是,在模板,我们使用了`<span>`元素包装了表格的文本数据,来实现拖拽时只能拖拽文本数据而不是整个。同时,在Sortable实例的options,我们设置了`fallbackOnBody`为true,这样拖拽元素在拖拽结束时会回到原来的位置,而不是消失。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值