Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to display html formatted text in antd table?

Tags:

reactjs

antd

My backend service(elasticsearch percolator) annotates text with html tags to highlight matches.

I can't find a way to display such html data in antd Table.

I've tried Highlighter component, but it applies keywords to whole column, but I need to highlight different words in each row.

link to fiddle

const { Table } = antd

class TableColor extends React.Component {
  constructor (props) {
    super(props)

    this.state = {
        data: []
    }
  }

  componentDidMount() {
    this.setState({
        data: [
        {id:1, name: 'Lazy < bclass="myBackgroundColor">fox</b>', match: 'fox'},
        {id:2, name: '<b class="myBackgroundColor">Dog</b> runs', match: 'Dog'},
        {id:3, name: 'I saw <b class="myBackgroundColor">duck</b>', match: 'duck'}
      ]
    })
  }

  render () {
   const columns = [{
      title: 'ID',
      dataIndex: 'id',
    }, {
      title: 'Name',
      dataIndex: 'name',
    }, {
      title: 'Match',
      dataIndex: 'match',
    }]

    return (
        <div style={{padding: '20px'}}>
        <Table
          columns={columns}
          dataSource={this.state.data}
        />
        </div>
    )
  }
}

ReactDOM.render(<TableColor />, document.querySelector('#app'))
like image 939
mindis Avatar asked Mar 22 '19 15:03

mindis


People also ask

How do I make an ANTD table responsive?

You can make use of the responsive property on the column that you want to control for screen sizes. Just add a From To column with a custom render function, and set the responsive property on that column to only show on xs screens. The From and To columns will have the responsive property set to show on md and above.

What is typography in ant design?

The Ant Design typography system is based on the design principle of "dynamic order" combined with the law of natural logarithm and temperament.

How do I add sorting to ANTD table?

Simply define a new sorting routine in utils/sorter. js , add it to the Sorter “enum,” and use it in your sorter prop for any column that needs it. You can check out the CodeSandbox for this tutorial to play around with the result.

How do I import an ANTD style?

Click the "Open in Editor" icon in the first example to open an editor with source code to use out-of-the-box. Now you can import the Alert component into the codesandbox: - import { DatePicker, message } from 'antd'; + import { DatePicker, message, Alert } from 'antd';


1 Answers

Since it looks like the name column already has highlighted html you could just add a render property to the name column definition that uses dangerouslySetInnerHtml to render the raw html.

...something like:

render: function(html) { return <div dangerouslySetInnerHtml({__html: html}) />

https://reactjs.org/docs/dom-elements.html#dangerouslysetinnerhtml https://ant.design/components/table/#Column

If you wanted to use react-highlight-words you could do the same thing with a render property but use the second argument passed to that function to get the .match property of the record and use that as the highlighted word.

like image 129
evan Avatar answered Nov 02 '22 04:11

evan