5

I am trying to use the Datatables Rows Created Callback to modify the data in the row before drawing it. What I am trying to do is replace all &lt; and &gt; with '<' and '>' so I can put a line break in each cell and have text on separate lines. '\n' or linefeed does not work.

var oTable = $('#table').DataTable( { 
      "createdRow" : function( row, data, index) {
         console.log( 'DATA WAS ' + data[0]);
         data[0] = data[0].replace(/&lt;/g,'<').replace(/&gt;/g,'>');
         console.log( 'DATA IS ' + data[0]);
       }

in the console I can see the data being modified correctly. But its not actually modifying the table. Is there a way to do this? or is the createdRow callback called after the row has already been drawn?

2 Answers 2

4

Yep, correct. createdRow callback is only called after the row has already been drawn. Instead of fixing your current code, I will show you how to do it using the proper (?) way by using column defs :D Also, I'd think/hope there was something somewhere that would convert your &lt; stuff automatically.

var oTable = $('#table').DataTable( { 
    "columnDefs": [ {
        "targets": 0,
        "render": function(data, type, row, meta) {
            html = data.replace(/&lt;/g,'<').replace(/&gt;/g,'>');
            return html;
        },
    ],
}
Sign up to request clarification or add additional context in comments.

1 Comment

I think there's a typo in your answer (that square bracket).
0

Add the modified element back to the td (column) in question.

var oTable = $('#table').DataTable( { 
    "createdRow" : function( row, data, index) {
    console.log( 'DATA WAS ' + data[0]);
    data[0] = data[0].replace(/&lt;/g,'<').replace(/&gt;/g,'>');
    console.log( 'DATA IS ' + data[0]);
    $('td', row).eq(0).append(data[0]);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.