Echotest
Echotest

Reputation: 129

DataTables JSON Api escape content

I got some data that I'm pulling from JSON API endpoint, one of the data records has object property called requestor. Here is what I mean :

data looks like so -> [{requestor: "First Lastname <[email protected]>", some_other: 'prop'}, {requestor: "Lastname first <[email protected]>", some_other: 'prop'}...{requestor n}]

The problem with this is that when it renders the table column content it renders it like so :

enter image description here

As a result only First Lastname shows up for the column content on the screen.

Is there a way I can 'escape' these < > or do something so this is treated like a text and not like HTML tag?

Datatables does offer render function callback, where I can return anything, but what do I return for this to be treated like a text?

Update per first edit below:

I can't want to replace < and > I need to print this into the table column First Lastname <[email protected]>

Upvotes: 1

Views: 251

Answers (1)

Rahul Pal
Rahul Pal

Reputation: 106

A simple regex in render worked for me. Here is the code:

$('#dtable').DataTable({
    data: [{requestor: "First Lastname <[email protected]>", some_other: 'prop'}, {requestor: "Lastname first <[email protected]>", some_other: 'prop'}],
    columns: [
      {
        data: 'requestor',
        render: data => data.replace(/[<]/g, '&lt;').replace(/[>]/g, '&gt;')
      },
      {data: 'some_other'}
    ],
})

Upvotes: 1

Related Questions