How to use $.fn.dataTable.render.text on a columns without defining all columns?

How to use $.fn.dataTable.render.text on a columns without defining all columns?

kowadagokowadago Posts: 24Questions: 5Answers: 2

Hello,

I am using dataTable on an HTML table that we have already with defined headers and body. I would like to use the function $.fn.dataTable.render.number without having to define all columns again like the example here:

https://datatables.net/manual/data/renderers

{
"product": "Toy car",
"creator": {
"firstName": "Fiona",
"lastName": "White"
},
"created": "2015-11-01",
"price": 19.99,
"cost": 12.53
}

Can I target the price column for example by mentioning the column number directly or do I have to redefine the entire table header scheme?

data: 3,    <--Column number of price assuming starting with Column 0
render: function ( data, type, row ) {
    return $.fn.dataTable.render.text(data)
}

Thank you

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 20,275Questions: 26Answers: 4,765

    You can use columnDefs to assign renderers to specific columns using the targets parameter.

    Kevin

  • kowadagokowadago Posts: 24Questions: 5Answers: 2

    Would you be kind enough to write an example? I already have columnDefs defined for sorting but not sure how to apply $.fn.dataTable.render.text() function to all data in a column

  • kowadagokowadago Posts: 24Questions: 5Answers: 2

    Tried this but no luck
    { render: $.fn.dataTable.render.text(data), targets: 3 }

  • kowadagokowadago Posts: 24Questions: 5Answers: 2

    Also tried

     { "targets": [4], "render": function ( data, type, full ){
          data = $.fn.dataTable.render.text(data);
          }
      }
    
  • allanallan Posts: 61,665Questions: 1Answers: 10,096 Site admin
    Answer ✓
    { 
      "targets": '_all',
      "render":$.fn.dataTable.render.text()
     }
    

    will apply the text renderer to all columns. See the columnDefs.targets documentation for more information about the targeting options.

    Allan

  • kowadagokowadago Posts: 24Questions: 5Answers: 2

    Thank you! I was wondering if that is required if the backend already escapes HTML when displaying values? I'm using Jinja2 to be specific

  • allanallan Posts: 61,665Questions: 1Answers: 10,096 Site admin
    Answer ✓

    I was wondering if that is required if the backend already escapes HTML when displaying values?

    I think this is rhetorical, but just to be explicit, no - the text renderer will do it for you.

    Allan

This discussion has been closed.