【问题标题】:Why does my column value not appear in jQuery datatable export?为什么我的列值没有出现在 jQuery 数据表导出中?
【发布时间】:2016-01-25 07:47:51
【问题描述】:

我有这样的 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 在导出数据表中显示列编号?

【问题讨论】:

  • 您也可以发布您的 html 吗?

标签: jquery datatables datatables-1.10


【解决方案1】:

fnRowCallback 用于后期处理(即向内容添加额外的样式或格式),每当显示一行。

因此,您在第 1 列中直观看到的连续数字只是对fnRowCallback 的连续调用的输出 - 基础数据永远不会更改,因此在导出表时数据会丢失。如果你想操作表格的内容——所以改变是持久的——你必须通过 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;
  }   
}]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-26
    • 2015-07-27
    • 2011-03-05
    • 1970-01-01
    • 2020-06-09
    • 1970-01-01
    • 2015-03-30
    相关资源
    最近更新 更多