Which method is most best to add the HTML in rows?

Which method is most best to add the HTML in rows?

EpexaEpexa Posts: 4Questions: 1Answers: 0

Hello!

Which method is most best to add the HTML in rows?

I need add HTML tag in column for every rows the client-side from server-side response.
Example <span class="danger">Test</span>.

With help ajax.dataSrc ?

$('#example').dataTable( {
  "serverSide": true,
  "ajax": {
    "url": "data.php",
    "dataSrc": function ( json ) {
      for ( var i=0, ien=json.data.length ; i<ien ; i++ ) {
        json.data[i][0] = '<span class="danger">'+json.data[i][0]+'</span>';
      }
      return json.data;
    }
  }
} );

Or has another way to the most best and fastest? Or ajax.dataSrc the only way?

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin
    Answer ✓

    I would suggest you use a renderer.

    Allan

  • EpexaEpexa Posts: 4Questions: 1Answers: 0

    Allan, thank you!

    Renderers better than ajax.dataSrc ?

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin
    Answer ✓

    For this yes. ajax.dataSrc is useful for modifying the structure of the JSON, but for modifying the output data I would certainly recommend a renderer. That way any changes made via the API in future (cell().data() for example) would be reflected through the renderer.

    Allan

  • EpexaEpexa Posts: 4Questions: 1Answers: 0

    Ok! Thank you very much!

This discussion has been closed.