Antd4/5 Popconfirm + Table 页面滚动出现的问题

Antd 的 Popcomfirm 打开之后,如果页面有滚动条,会出现意向不到的情况:

当然,如果这个滚动条在我们的 body 上面,或许没有什么问题,但是,如果他出现在我们的里面的盒子的那个元素上面,那么可能会看不到,特别是在 Table 组件下面:

 解决方法:我们给 Popconfirm 指定滚动元素

<Popconfirm
   getPopupContainer={() => {
        return document.getElementById('app-content') as HTMLElement;
   }}>
       删除
</Popconfirm>

  document.getElementById('app-content') 是滚动元素的 id

<Popconfirm
      getPopupContainer={(triggerNode   ) => {
        let targetElement = triggerNode;
        while (targetElement.parentNode) {
          // 检查是否有滚动条
          const overflowY = window.getComputedStyle(targetElement).overflowY;
          if (overflowY === 'auto' || overflowY === 'scroll') {
            console.log('Found scrolling element:', targetElement);
            break; // 找到滚动元素后退出循环
          }
          targetElement = targetElement?.parentNode as HTMLElement; // 向上层查找
        }
        return targetElement;
      }}
      删除
</Popconfirm>

整个组件:

function App() {
  const columns = [
    {
      with: 200,
      title: "Name",
      dataIndex: "name",
    },
    {
      title: "Age",
      dataIndex: "age",
    },
    {
      with: 200,
      title: "Address",
      dataIndex: "address",
    },
    {
      with: 200,
      title: "Address2",
      dataIndex: "address2",
    },
    {
      with: 200,
      title: "Address3",
      dataIndex: "address3",
    },
    {
      with: 200,
      title: "Address4",
      dataIndex: "address4",
    },
    {
      fixed: "right",
      with: 200,
      title: "Address5",
      dataIndex: "address5",
    },
    {
      fixed: "right",
      title: "Action",
      key: "action",
      render: (_, record) => (
        <Popconfirm
          title="Sure to delete?"
          okText="Yes"
          cancelText="No"
          getPopupContainer={() => {
            return document.getElementById("app-content") as HTMLElement;
          }}
          onConfirm={() => {}}
        >
          <a>Delete</a>
        </Popconfirm>
      ),
    },
  ];

  const data = [];
  for (let i = 0; i < 46; i++) {
    data.push({
      key: i,
      name: `Edward King ${i}`,
      age: 32,
      address: `London, Park Lane no. ${i}`,
      address2: `London, Park Lane no. ${i}`,
      address3: `London, Park Lane no. ${i}`,
      address4: `London, Park Lane no. ${i}`,
      address5: `London, Park Lane no. ${i}`,
    });
  }

  return (
    <div className="app">
      {/* 这里加上 app-content */}
      <div id="app-content" className="content">
        <Table
          columns={columns}
          dataSource={data}
          bordered
          pagination={{}}
          scroll={{ x: 1300 }}
        ></Table>
      </div>
    </div>
  );
}

export default App;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小木苓

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值