'How dynamically change column title of dataTable jQuery plugin?

I would like to change the title of a column my datatable generated by jQuery Datatable plugin

Do you know if I can do something like this:

 table = $('#example').DataTable({
        "data": source_dataTable,
        "columnDefs": defs,

    "dom": 't<"top"f>rt<"bottom"lpi><"clear">',
});
// WHAT I WANT TO DO:
    table.column(0).title.text("new title for the column 0")

?

It renders a html a first line like that:

 <table id="example" class="row-border hover dataTable no-footer" role="grid" aria-describedby="example_info" style="width: 1140px;">
   <thead>
       <tr role="row">
              <th class="sorting" tabindex="0" aria-controls="example" rowspan="1" colspan="1" aria-label="S&amp;#233;lectionn&amp;#233;: activer pour trier la colonne par ordre croissant" style="width: 94px;">Sélectionné</th>

               <th class="sorting" tabindex="0" aria-controls="example" rowspan="1" colspan="1" aria-label="Anglais : activer pour 

                  trier la colonne par ordre croissant" style="width: 

62px;">Anglais </th>

</tr>
</thead>
 </table>

...

In a normal table the code bellow work , but for datatable rendered by jQuery plugin, it doesn't:

$('#example tr:eq(0) th:eq(0)').text("Text update by code");

Maybe there is a API method or another dom way?



Solution 1:[1]

I found a solution that really do it dynamically

$(table.column(1).header()).text('My title');

Solution 2:[2]

use below code . check DEMO

if first tr with td

  $('table tr:eq(0) td:eq(0)').text("Text update by code");

if first tr with th

  $('table tr:eq(0) th:eq(0)').text("Text update by code");

when using service side process or want to do some action once data table load all data use Draw event .

Draw event - fired once the table has completed a draw

Example

 $('#example').dataTable();

 $('#example').on( 'draw.dt', function () {
   console.log( 'Redraw occurred at: '+new Date().getTime() );
   $('#example tr:eq(0) th:eq(0)').text("Text update by code");
 } );

Using callback.
drawCallback Function that is called every time DataTables performs a draw. check DEMO

   $('#example').dataTable( {
    "drawCallback": function( settings ) {
      $('#example tr:eq(0) th:eq(0)').text("Text update by code");
    }
  } );

check all callback options HERE

Solution 3:[3]

Possible from this way :

"columns": [{ sTitle: "Column1"},{ sTitle: "Column2"}]

Solution 4:[4]

var SelectedTable = $('# [Table ID]').DataTable();
var HeaderText = SelectedTable.columns([col index]).header();
$(HeaderText).html('[new col name]');

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pipo
Solution 2
Solution 3 Chinthaka Suren Fernando
Solution 4 Tyler2P