Parsing "\n" new lines in HTML from JSON string.

Parsing "\n" new lines in HTML from JSON string.

inteldigitalinteldigital Posts: 1Questions: 1Answers: 0

So, a little background. I have a datatable set up, it's really rough around the edges, just to display a whole bunch of data from a JSON database. This is how I have it set out

$(document).ready(function() {
    $('#data').DataTable( {
        "ajax": {
            "dataType": 'json',
            "url": "domains.json",
            "dataSrc": "cards"
        },
        "order": [ 3, "desc" ],
        "columns": [
            { "data": "name" },
            { "data": "desc"},
            { "data": "shortUrl",
              "render": function(data, type, row, meta)
              {
                if(type === 'display')
                {
                    data = '<a class="trello-link" target="_blank" href="' + data + '">' + data + '</a> \n Open in Incognito';
                }

                return data;}
            },
            {  "data": "dateLastActivity"}
        ]
    } );
} ); 

And this is pulling information from the JSON database source. The data in the second column "desc" is a string that displays as such in the JSON:

"Some information here\nMore information here\nNext information here"

The "\n" when parsed through into the datatable doesn't form a new line, is there any way this can be performed without major changes to the database structure itself?

Kind Regards,
Ben

Answers

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947
    edited November 2018

    You can use columns.render and replace the \n with <br> (HTML line break). That should display with new lines in the Datatable.

    Kevin

This discussion has been closed.