javascript - DataTable,导出为 pdf 不能正常使用两个标题和 colspan

标签 javascript jquery datatables html-table export-to-pdf

我正在使用数据表来显示我的数据,我想将它们导出为 pdf。

我遵循了 example given in this link 中列出的步骤.

我有一个表,其中我想要两个标题和两个标题,一个标题有 colspan 即如下所示

<th colspan=3>

因此,当我尝试将表格导出为 pdf 时,它只给我一个标题,而且还有完整的列描述。 我的代码片段包含所有必需的 CSS 和 JS 文件,如下所示:

<link href="https://cdn.datatables.net/1.10.11/css/dataTables.bootstrap.min.css" rel="stylesheet"/>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.11/css/jquery.dataTables.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/buttons/1.1.2/css/buttons.dataTables.min.css">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.12.0.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.11/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.1.2/js/dataTables.buttons.min.js"></script>
<script type="text/javascript" src="http://cdn.datatables.net/buttons/1.1.2/js/buttons.flash.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.18/build/pdfmake.min.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.18/build/vfs_fonts.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.1.2/js/buttons.html5.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.1.2/js/buttons.print.min.js"></script>


<script type="text/javascript">
$(document).ready(function() {
    $('#dataTable').DataTable( {
            dom: 'Bfrtip',
            buttons: [
                'copy', 'csv', 'excel', 'pdf', 'print'
            ]
        } );
    } );
</script>

<table id="dataTable" cellspacing="0" width="auto">
        <thead>
            <tr>
            <th colspan = 3></th>
            <th colspan = 3>IMP values</th>
            </tr>
            
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Age</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </thead>
        
        <tbody>
            <tr>
                <td>Tiger Nixon</td>
                <td>System Architect</td>
                <td>Edinburgh</td>
                <td>61</td>
                <td>2011/04/25</td>
                <td>$320,800</td>
            </tr>
            <tr>
                <td>Garrett Winters</td>
                <td>Accountant</td>
                <td>Tokyo</td>
                <td>63</td>
                <td>2011/07/25</td>
                <td>$170,750</td>
            </tr>
          </tbody>
    </table>

下图显示了在浏览器中看到的表格 The table in the browser

下图为导出pdf后的表格 Shows image of the exported pdf file

那么,如何使用数据表获取pdf中的两个标题呢?

提前致谢。

最佳答案

pdf 导出功能目前只考虑 1 行列标题,因此只导出一个标题行。

为了导出两个标题行,我们可以使用 pdf 导出按钮中提供的 customize 选项。此选项允许我们在导出前操作 pdf 文档对象。引用pdfmake documentationplayground for table,我们可以看到,要有一个以上的表格标题行,需要进行以下更改。

  1. 将表的 headerRows(标题行数)设置为 2。
  2. 将缺失的标题行插入pdf表格的第一个body,因为给定的标题行单元格有col-Span,空单元格需要添加到标题行以确保每一行都有相同数量的细胞。

以下代码片段演示了上述更改。

由于Download in Sandboxed Iframes (removed)的原因,代码片段中的按钮将无法使用,您可以将以下代码复制到一个html文件中,用浏览器打开文件查看效果。

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/jszip-2.5.0/dt-1.10.21/b-1.6.2/b-flash-1.6.2/b-html5-1.6.2/b-print-1.6.2/datatables.min.css" />

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/pdfmake.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/vfs_fonts.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/v/dt/jszip-2.5.0/dt-1.10.21/b-1.6.2/b-flash-1.6.2/b-html5-1.6.2/b-print-1.6.2/datatables.min.js"></script>

<script type="text/javascript">
  $(document).ready(function() {
    $('#dataTable').DataTable({
      dom: 'Bfrtip',
      buttons: [
        'copy', {
          extend: 'csv',
          "download": "download"
        }, {
          extend: 'excel',
          "download": 'download'
        }, {
          extend: 'pdf',
          text: 'Export with two row headers',
          download: 'download',
          customize: function(pdfDocument) {
            pdfDocument.content[1].table.headerRows = 2;
            var firstHeaderRow = [];
            $('#dataTable').find("thead>tr:first-child>th").each(
              function(index, element) {
                var colSpan = element.getAttribute("colSpan");
                firstHeaderRow.push({
                  text: element.innerHTML,
                  style: "tableHeader",
                  colSpan: colSpan
                });
                for (var i = 0; i < colSpan - 1; i++) {
                  firstHeaderRow.push({});
                }
              });
            pdfDocument.content[1].table.body.unshift(firstHeaderRow);

          }
        }, {
          extend: 'print',
          download: 'download'
        }
      ]
    });
  });
</script>

<table id="dataTable" cellspacing="0" width="auto">
  <thead>
    <tr>
      <th colspan=3></th>
      <th colspan=3>IMP values</th>
    </tr>

    <tr>
      <th>Name</th>
      <th>Position</th>
      <th>Office</th>
      <th>Age</th>
      <th>Start date</th>
      <th>Salary</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>Tiger Nixon</td>
      <td>System Architect</td>
      <td>Edinburgh</td>
      <td>61</td>
      <td>2011/04/25</td>
      <td>$320,800</td>
    </tr>
    <tr>
      <td>Garrett Winters</td>
      <td>Accountant</td>
      <td>Tokyo</td>
      <td>63</td>
      <td>2011/07/25</td>
      <td>$170,750</td>
    </tr>
  </tbody>
</table>

关于javascript - DataTable,导出为 pdf 不能正常使用两个标题和 colspan,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36589793/

相关文章:

jquery - 当搜索未返回结果时,Select2 过滤器值消失

jquery - 使用 stateSave : true? 返回时如何用其值预填充多个搜索列

javascript - jQuery DataTables 和 Knockout 未正确绑定(bind)

javascript - 如果浏览器版本早于

jquery - Chrome 自动完成文本输入可防止触发 keydown 事件(jquery)

javascript - Jquery,将类添加到步骤

javascript - 如何在表单外调用 jquery 验证函数

javascript - 我试图在论坛设置中 float <p> 标签

javascript - 动画一个 - 停止另一个 |查询

javascript - 使用 Jquery 替换特定的名称属性?