要实现antd表格内容展示多行时的上下居中,可以使用ellipsis属性来控制内容溢出时的显示方式,并设置whiteSpace属性为normal,这样可以让内容自动换行。

另外,可以使用自定义的样式来设置表格单元格的高度和垂直居中方式。

以下是一个示例代码:

import React from "react";
import { Table } from "antd";

const data = [
  {
    key: "1",
    name: "John Brown",
    age: 32,
    address: "New York No. 1 Lake Park"
  },
  {
    key: "2",
    name: "Jim Green",
    age: 42,
    address: "London No. 1 Lake Park"
  },
  {
    key: "3",
    name: "Joe Black",
    age: 32,
    address: "Sidney No. 1 Lake Park"
  }
];

const columns = [
  {
    title: "Name",
    dataIndex: "name",
    key: "name",
    ellipsis: true,
    render: (text) => <div style={{ height: "60px", display: "flex", alignItems: "center" }}>{text}</div>
  },
  {
    title: "Age",
    dataIndex: "age",
    key: "age",
    ellipsis: true,
    render: (text) => <div style={{ height: "60px", display: "flex", alignItems: "center" }}>{text}</div>
  },
  {
    title: "Address",
    dataIndex: "address",
    key: "address",
    ellipsis: true,
    render: (text) => <div style={{ height: "60px", display: "flex", alignItems: "center" }}>{text}</div>
  }
];

const App = () => {
  return <Table columns={columns} dataSource={data} />;
};

export default App;

在上面的代码中,通过设置ellipsis: true来实现内容溢出时的省略显示,然后通过render函数来自定义单元格的展示方式,设置单元格的高度为60px,并使用display: flexalignItems: center来实现垂直居中效果

antd 表格 内容展示多行时实现上下居中

原文地址: https://www.cveoy.top/t/topic/iEYf 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录