如何动态更改dataTable jQuery插件的列标题?

Viv*_*ipo 9 javascript datatable jquery datatables

我想更改由jQuery Datatable插件生成的我的数据表列的标题

你知道我能不能这样做:

 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")
Run Code Online (Sandbox Code Playgroud)

它将html呈现为第一行:

 <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>
Run Code Online (Sandbox Code Playgroud)

...

在普通表中,代码工作正常,但对于jQuery插件呈现的数据表,它不会:

$('#example tr:eq(0) th:eq(0)').text("Text update by code");
Run Code Online (Sandbox Code Playgroud)

也许有一种API方法或另一种方法?

Viv*_*ipo 9

我找到了一个真正动态的解决方案

$(table.column(1).header()).text('My title');
Run Code Online (Sandbox Code Playgroud)


Nis*_*eta 5

使用下面的代码。检查演示

如果第一个 tr 与 td

  $('table tr:eq(0) td:eq(0)').text("Text update by code");
Run Code Online (Sandbox Code Playgroud)

如果第一个 tr 与 th

  $('table tr:eq(0) th:eq(0)').text("Text update by code");
Run Code Online (Sandbox Code Playgroud)

当使用服务端进程或想要在数据表加载所有数据后执行某些操作时使用 Draw 事件

Draw 事件- 在桌子完成抽奖后触发

例子

 $('#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");
 } );
Run Code Online (Sandbox Code Playgroud)

使用回调。
drawCallback 每次 DataTables 执行绘制时调用的函数。检查 演示

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

此处检查所有回调选项