ant-design-vue + vue3 表格(拖拽)伸缩列实现

ant-design-vue + vue3 表格(拖拽)伸缩列实现

备注:新版ant-design-vue已增加resizable属性实现此功能,只是我这里升级版本改动太大,所以需要自己写

1.下载vue3-draggable-resizable
npm i vue3-draggable-resizable
2.增加components属性
<template>
  <Table
    :bordered="true"
    :dataSource="dataSource"
    :columns="columns"
    :components="components"
    :scroll="scroll"
  />
</template>

<script lang="ts">
import { Table } from 'ant-design-vue';
import { ref } from 'vue';
import 'ant-design-vue/dist/antd.css';
import { useTableHeadResizable } from './useTableHeadResizable';

const columns = ref([
  {
    title: '姓名',
    dataIndex: 'name',
    key: 'name',
    width: 100,
  },
  {
    title: '年龄',
    dataIndex: 'age',
    key: 'age',
    width: 100,
  },
  {
    title: '住址',
    dataIndex: 'address',
    key: 'address',
    width: 100,
  },
]);
const dataSource = [
  {
    key: '1',
    name: '胡彦斌',
    age: 32,
    address: '西湖区湖底公园1号',
  },
  {
    key: '2',
    name: '胡彦祖',
    age: 42,
    address: '西湖区湖底公园1号',
  },
];
const scroll = {
  x: '100%',
};

export default {
  components: { Table },
  setup() {
    return {
      columns,
      dataSource,
      scroll,
      components: {
        header: {
          cell: (columnAttrs, props) =>
            useTableHeadResizable(columnAttrs, props, columns),
        },
      },
    };
  },
};
</script>

<style>
.table-draggable-handle {
  height: 50px !important;
  left: auto !important;
  right: -5px;
  cursor: col-resize;
  touch-action: none;
  border: none;
  position: absolute;
  transform: none !important;
  bottom: 0;
  border: none !important;
}
.resize-table-th {
  position: relative;
}
.ant-table-column-sorters {
  display: table;
}
</style>


// useTableHeadResizable.ts
import { h } from 'vue';
import VueDraggableResizable from 'vue3-draggable-resizable';
import { DraggableContainer } from 'vue3-draggable-resizable';
import 'vue3-draggable-resizable/dist/Vue3DraggableResizable.css';

export const useTableHeadResizable = (columnAttrs, props, columns) => {
  const { key } = columnAttrs;
  const { children } = props[0];

  const colIndex = columns.value.findIndex((col) => {
    const k = col.key || col.dataIndex;
    return k === key;
  });
  if (!columns.value[colIndex] || !columns.value[colIndex].width) {
    return h('th', { ...columnAttrs }, [children]);
  }

  const dragProps = {
    key: columns.value[colIndex].dataIndex || columns.value[colIndex].key,
    class: 'table-draggable-handle',
    w: 5,
    x: columns.value[colIndex].width,
    disabledY: true,
    draggable: true,
    resizable: false,
    onDragging: (e) => {
      columns.value[colIndex].width = Math.max(e.x, 1);
    },
  };

  const resizable = h(VueDraggableResizable, { ...dragProps });
  const container = h(
    DraggableContainer,
    { disabled: true, style: 'position: unset' },
    { default: () => resizable }
  );
  return h(
    'th',
    { ...columnAttrs, class: 'resize-table-th' },
    {
      default: () => [children, container],
    }
  );
};

项目示例:https://stackblitz.com/edit/vue-cef6of?file=src%2FApp.vue

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 24
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值