【问题标题】:Datatable does not allow to include multiple footers rows into exported pdf fileDatatable 不允许在导出的 pdf 文件中包含多个页脚行
【发布时间】:2017-04-20 06:37:51
【问题描述】:

我正在尝试使用导出选项创建一个支持多个页脚的 Datatable PDF。

但它只允许第一行页脚在导出的 PDF 中

请告诉我是否可以在数据表中创建多个页脚? 和 如果是,那么如何在java脚本中实现?

我正在分享代码

在 Java Script 中创建数据表并在表中添加 tfoot

'<tfoot id="reportTblFoot' + chartIndex+ '">'+
'</tfoot>'

表格页脚变量

var tfoot='#reportTblFoot'+chartIndex;

数据添加到页脚

var footer1 = $("<tr />");
                footer1.append("<td style='border:none;font-style:italic;line-height:0;font-size:10px'>* Applicable row 1</td>");
                footer1.append("<td style='display:none'/>");
                footer1.append("<td style='display:none'/>");
                $(tfoot).append(footer1);

            var footer2 = $("<tr />");
            footer2.append("<td style='border:none;font-style:italic;line-height:0;font-size:10px'>** Not Applicable row 2</td>");
            footer2.append("<td style='display:none'/>");
            footer2.append("<td style='display:none'/>");
            $(tfoot).append(footer2);

            var footer3 = $("<tr />");
            footer3.append("<td style='border:none;font-style:italic;line-height:0;font-size:10px'>*** Not Applicable row 3</td>");
            footer3.append("<td style='display:none'/>");
            footer3.append("<td style='display:none'/>");
            $(tfoot).append(footer3);

            var percSumNote = $("<tr />");
            percSumNote.append("<td style='border:none;'><font size='1' color='grey'>**** Not Applicable row 4</font></td>");
            percSumNote.append("<td style='display:none'/>");
            percSumNote.append("<td style='display:none'/>");
            $(tfoot).append(percSumNote);

在数据表属性中为 footer

应用 TRUE
buttons: [ {
                                 extend: 'pdf',
                                 filename:fileName,
                                 message:tableSubTitle,
                                 title: tableTitle,
                                 footer:true,
                                 pageSize : "A3",
                                 customize: function (doc) {
                                     doc.defaultStyle.alignment = 'left';
                                     doc.styles.message = {
                                             alignment: 'center'
                                         }
                                     doc.styles.table = {
                                             alignment: 'center',
                                             width: '100%',
                                         }
                                     doc.defaultStyle.fontSize = 16;
                                     doc.styles.tableFooter.fontSize = 8;
                                     doc.pageMargins = [ 130, 20, 130, 20 ];
                                     doc.content.forEach(function(item) {
                                         if (item.table) {
                                            // Set width for 3 columns
                                            item.table.widths = [350, 100, 100,'*'] 
                                         } 
                                      });
                                 } 
                             }]

在下载的 PDF 中只添加第一个页脚

【问题讨论】:

  • @davidkonrad 是的,我用谷歌搜索了其中的一些链接,但没有找到解决方案。
  • 还有this explanation from the author 或者这个github issue 或者this another issue ...?谷歌的第一击?
  • 您可以使用包含多行的单元格设置单个页脚的样式,但要使其看起来很棒或作为多个页脚,您需要在 pdfmake 组件中进行一些认真的调查,我猜...

标签: javascript datatable datatables


【解决方案1】:

作为@davidkonrad mentioned,作者在this post 中解释说目前不支持。

解决方法是将多个页脚合并为一行,在行之间使用换行符\n,并将 PDF 库配置为不去除换行符。

例如:

var tfoot='#reportTblFoot'+chartIndex;

var footer1 = $("<tr />");
footer1.append(
    "<th>" +
    "<div style='font-style:italic;font-size:10px'>" +
    "* Applicable row 1<br>\n" +
    "** Not Applicable row 2<br>\n" +
    "*** Not Applicable row 3\n" +
    "</div>" +
    "<div>" +
    "<font size='1' color='grey'>**** Not Applicable row 4</font>" +
    "</div>" +
    "</th>"
);

footer1.append("<th style='display:none'/>");
footer1.append("<th style='display:none'/>");

$(tfoot).append(footer1);

您还需要为 PDF 按钮使用以下选项以避免条纹换行符。

buttons: [
   { 
      extend: 'pdfHtml5',
      footer:true,
      exportOptions: {
         stripNewlines: false
      }
      // ... skipped other options ...
   }
],

有关代码和演示,请参阅this example

【讨论】:

  • 我已经检查了这项工作。
    标签不允许在 PDF 文件中创建断行。在页脚中,所有数据都显示在单行中。
  • @SaurabhMehta,你说得对,更新了我的答案,添加了工作示例。
  • @Gryrocode.com 感谢您的付出!!!这完全符合我的要求
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-22
  • 1970-01-01
  • 2021-10-10
  • 1970-01-01
  • 2015-08-02
  • 2019-04-08
  • 2013-09-13
相关资源
最近更新 更多