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

How dynamically change column title of dataTable jQuery plugin?

我想更改由 jQuery 数据table 插件

生成的我的数据table 的列的标题

你知道我能不能做这样的事情:

 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")

?

它呈现 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>

...

在正常的 table 中,下面的代码可以工作,但是对于 jQuery 插件呈现的数据 table,它不会:

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

也许有 API 方法或其他 dom 方法?

使用下面的代码。检查 DEMO

if 第一个 tr 与 td

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

如果第一次与 th

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

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

Draw event - 一旦 table 完成平局

示例

 $('#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");
 } );

使用回调。
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");
    }
  } );

检查所有回调选项HERE

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

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

可能是这样的:

"columns": [{ sTitle: "Column1"},{ sTitle: "Column2"}]
var SelectedTable = $('# [Table ID]').DataTable();
var HeaderText = SelectedTable.columns([col index]).header();
$(HeaderText).html('[new col name]');