为什么我的列值没有出现在 jQuery 数据表导出中?

Why does my column value not appear in jQuery datatable export?

我有 jquery 个这样的数据表:

$(document).ready(function() {
$('#example1').DataTable( {

"fnRowCallback" : function(nRow, aData, iDisplayIndex){
            $("td:first", nRow).html(iDisplayIndex +1);
           return nRow;
        },
 "columnDefs": [ {
        "paging": true,
      "lengthChange": false,
      "searching": true,
      "ordering": true,
      "info": true,
      "autoWidth": true,
        "searchable": false,
        "orderable": false,
        "targets": 0
    } ],
 dom: 'lBfrtip',  
buttons: [
    'copyHtml5','excelHtml5','csvHtml5',pdfHtml5']
} );

在我的本地运行良好:

但如果我导出为 pdfhtml5、excelhtml5 和 csv html,则不会显示列编号。

如何解决?使用 html5?

在导出数据表中显示列编号

fnRowCallback 用于 post 处理 (即向内容添加额外的样式或格式)并称为 每当 显示一行。

因此,您在第 1 列中看到的连续数字只是对 fnRowCallback 的连续调用的输出——基础数据永远不会改变,因此当 table 被导出。如果你想操纵 table 的内容 - 所以更改是持久的 - 你必须通过 API。

在几个解决方案中,您可以使用 createdRow 回调:

createdRow: function (row, data, index) {
  this.api().cell({ row:index, column:0 }).data(index+1)
}    

cell().data()持续更新底层数据;或者您可以在 columnDefs 中利用 render 方法:

columnDefs: [{
  targets: 0,
  autoWidth: true,
  searchable: false,
  orderable: false,
  render: function(data, type, row, info) {
     return parseInt(info.row)+1;
  }   
}]