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

### 实现 Ant Design Vue 表格组件的手动调整列宽 为了在 Ant Design Vue 的表格组件实现手动调整列宽的功能,可以利用 `Resizable` 列属性以及自定义事件处理程序来完成这一功能。具体来说: #### 使用 Resizable 属性 Ant Design Vue 提供了内置的支持用于创建可调整宽度的列。通过设置表头配置中的 `resizable` 属性为 true 可以启用此特性。 ```javascript const columns = [ { title: &#39;Name&#39;, dataIndex: &#39;name&#39;, key: &#39;name&#39;, resizable: true, // 启用列宽调整 }, { title: &#39;Age&#39;, dataIndex: &#39;age&#39;, key: &#39;age&#39;, resizable: true, } ]; ``` #### 处理列宽变化事件 当用户拖拽改变某一列的宽度时,会触发相应的事件。可以通过监听这些事件并更新状态来保存新的列宽设定。 ```vue <template> <a-table :columns="columns" :data-source="dataSource"> <!-- 其他配置 --> </a-table> </template> <script setup lang="ts"> import { ref } from &#39;vue&#39;; let columns = ref([ { title: &#39;Name&#39;, dataIndex: &#39;name&#39;, key: &#39;name&#39;, width: 200, // 设置初始宽度 onHeaderCell: (column) => ({ style: { cursor: &#39;col-resize&#39; }, onresize() { console.log(&#39;Column resized&#39;); } }), resizable: true }, /* ... */ ]); // 假设的数据源 const dataSource = [...]; function handleResize(event, column){ const newWidth = event.target.offsetWidth; Object.assign(column, {width:newWidth}); } </script> ``` 上述代码展示了如何使用 `onHeaderCell` 来附加样式和为到特定单元格上,在这里主要是用来响应用户的鼠标操作以便于识别何时发生了尺寸更改,并据此调用回调函数 `handleResize()` 更新对应列的实际宽度[^1]。 需要注意的是,实际项目开发过程中可能还需要考虑更多细节问题,比如持久化存储用户的偏好设置、跨浏览器兼容性测试等。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值