antd3树形表格树形列显示不全使用拖拽列宽解决问题

描述

antd 没有自带的拖拽api 借助库实现

react-resizable

表格代码

使用components自定义表头header
通过拖拽的onResize的api 动态去改变columns的每一项的宽度

import React, { useState } from 'react';
import { Table } from 'antd';
import { formatMessage } from 'umi/locale';
import { Resizable } from 'react-resizable';
import styles from './index.less';

const data = [
  {
    level: 1,
    key: 1,
    name: 'John Brown sr.',
    age: 60,
    address: 'New York No. 1 Lake Park',
    children: [
      {
        key: 11,
        name: 'John Brown',
        age: 42,
        address: 'New York No. 2 Lake Park',
      },
      {
        key: 12,
        name: 'John Brown jr.',
        age: 30,
        address: 'New York No. 3 Lake Park',
        children: [
          {
            key: 121,
            name: 'Jimmy Brown',
            age: 16,
            address: 'New York No. 3 Lake Park',
          },
        ],
      },
      {
        key: 13,
        name: 'Jim Green sr.',
        age: 72,
        address: 'London No. 1 Lake Park',
        children: [
          {
            key: 131,
            name: 'Jim Green',
            age: 42,
            address: 'London No. 2 Lake Park',
            children: [
              {
                key: 1311,
                name: 'Jim Green jr.',
                age: 25,
                address: 'London No. 3 Lake Park',
              },
              {
                key: 1312,
                name: 'Jimmy Green sr.',
                age: 18,
                address: 'London No. 4 Lake Park',
              },
            ],
          },
        ],
      },
    ],
  },
  {
    key: 2,
    name: 'Joe Black',
    age: 32,
    address: 'Sidney No. 1 Lake Park',
    level: 1,
  },
];

/** 可拖动表格头 */
const ResizeableTitle = props => {
  const { onResize, width, ...restProps } = props;

  if (!width) {
    return <th {...restProps} />;
  }
  return (
    <Resizable width={width} height={0} onResize={onResize} draggableOpts={{ enableUserSelectHack: false }}>
      <th {...restProps} />
    </Resizable>
  );
};

const Page = () => {
  const [selectedKeys, setSelectedKeys] = useState([]);

  const [columns, setColumns] = useState([
    {
      title: '名称',
      dataIndex: 'name',
      key: 'name',
      width: 150, // 用于拖拽
    },
    {
      title: '年龄',
      dataIndex: 'age',
      key: 'age',
      width: 150,
    },
    { title: '地址', dataIndex: 'address', key: 'address', width: 150 },
    { title: '地址', dataIndex: 'address', key: 'address', width: 150 },
    { title: '地址', dataIndex: 'address', key: 'address', width: 150 },
  ]);

  const onSelect = selectedKeysValue => {
    setSelectedKeys(selectedKeysValue);
  };

  const rowSelection = {
    selectedRowKeys: selectedKeys,
    onChange: onSelect,
  };

  const handleResize = index => (e, { size }) => {
    const maxWidth = document.body.offsetWidth / 2;// 处理移除屏幕外的问题
    if (index !== 0 || size.width > maxWidth) {
      return;
    }
    setColumns(oldcolumns => {
      const nextColumns = [...oldcolumns];
      nextColumns[index] = {
        ...nextColumns[index],
        width: size.width,
      };
      return nextColumns;
    });
  };

  const columnsTemp = columns.map((col, index) => ({
    ...col,
    onHeaderCell: column => ({
      width: column.width,
      onResize: handleResize(index),
    }),
  }));

  console.log('columnsTemp', columnsTemp);

  const components = {
    header: {
      cell: ResizeableTitle,
    },
  };

  return (
    <div className={styles.page}>
     
      <div className={styles.treeTable}>
        <Table columns={columnsTemp} components={components} bordered rowSelection={rowSelection} dataSource={data} />{' '}
      </div>
    </div>
  );
};
export default Page;

样式

重点 不设置样式,拖动不了

 .treeTable {
    :global {
      .ant-table-tbody > tr > td {
        border: none;
      }
      .react-resizable {
        position: relative;
        background-clip: padding-box;
      }

      .react-resizable-handle {
        position: absolute;
        width: 10px;
        height: 100%;
        bottom: 0;
        right: -5px;
        cursor: col-resize;
        z-index: 1;
      }
    }
  }

实现效果

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Ant Design Vue 3 中,你可以通过 `expanded-row-keys` 属性来指定展开节点。这个属性接受一个数组,数组中的每个元素代表一个需要展开的节点的 key 值。 以下是一个示例: ```html <template> <a-table :columns="columns" :data-source="data" :expanded-row-keys="expandedRowKeys"> <template #expanded-row="record"> <p>{{ record.description }}</p> </template> </a-table> </template> <script> import { defineComponent } from 'vue'; import { Table } from 'ant-design-vue'; export default defineComponent({ components: { Table }, data() { return { columns: [ { title: '名称', dataIndex: 'name', key: 'name' }, { title: '描述', dataIndex: 'description', key: 'description' }, ], data: [ { key: '1', name: '节点1', description: '这是节点1的描述', children: [ { key: '1-1', name: '节点1-1', description: '这是节点1-1的描述', }, ], }, { key: '2', name: '节点2', description: '这是节点2的描述', children: [ { key: '2-1', name: '节点2-1', description: '这是节点2-1的描述', }, { key: '2-2', name: '节点2-2', description: '这是节点2-2的描述', }, ], }, ], expandedRowKeys: ['1'], }; }, }); </script> ``` 在上面的示例中,我们在 `data` 中定义了两个节点,每个节点都有自己的 `key` 值。在 `expandedRowKeys` 属性中,我们指定了需要展开的节点的 `key` 值为 `['1']`,这样在渲染表格时,节点1就会自动展开。同时我们通过 `<template #expanded-row>` 来指定了展开后的内容。 如果你需要动态指定展开的节点,可以在 `expandedRowKeys` 中使用 `v-model` 双向绑定一个变量,然后在需要展开节点的时候修改这个变量即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值