Antd table how to put text into cell in several lines
Asked Answered
A

3

11

In Antd is there a way to show the text in table cell into several lines. I try to put </br>, \n, \r into the text.

Is there someone who has already find a way to do that?

Anthropoid answered 15/6, 2017 at 8:56 Comment(1)
Maybe a solution is to used a render of the cell column and format as we would like the text ?Anthropoid
A
24

Finally here is my solution.

The text for each column contains an \n when there is necessary to have a new line.

After into the table definition I put the style whiteSpace: 'pre':

<Table style={{ whiteSpace: 'pre'}} columns={columns} dataSource={data} title={title} .../>

Thats seems to work as expected.

Anthropoid answered 15/6, 2017 at 9:24 Comment(0)
W
3

In the hopes that it's never too late to answer a question here :D

Use the renderer of the table cell, like below, where you can use HTML tags (I'm using React so it's formatted as ReactNode with <> </> parent elements):

const columns = [
    {
      title: "Start",
      dataIndex: "start",
      key: "start",
      render: (text) => <>
      <p>{text.split("@")[0]}</p>
      <p>{text.split("@")[1]}</p>
      </>
    },
    {
      title: "Name",
      dataIndex: "name",
      key: "name",
      render: (text) => text
    }]
Windbound answered 19/8, 2021 at 16:38 Comment(0)
B
0

My paragraph text had no special character. it was a plain text that I got from the API. so I inspected the table cell and found that it was using white-space: nowrap; That is why changing the cell width didn't create a new line for text. so I changed it like that

{
    title: "Address",
    dataIndex: "Address",
    key: "Address",
    width: 250,
    render: text => <div style={{ whiteSpace: 'break-spaces', width: '250px' }}>Lorem ipsum dolor sit amet consectetur adipisicing elit. Maxime mollitia,</div>,
},

I put whiteSpace: 'break-spaces' and it created a new line.

Behavior answered 14/6 at 3:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.