For*_*ekh 9 javascript jquery html-table datatables export-to-pdf
我正在使用数据表来显示我的数据,我想将它们导出为pdf.
我按照此链接中给出的示例中列出的步骤操作.
我有一个表,我想要两个标题和两个标题,一个标题有colspan,如下所示
<th colspan=3>Run Code Online (Sandbox Code Playgroud)
因此,当我尝试将表导出为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>Run Code Online (Sandbox Code Playgroud)
那么,如何使用数据表获得pdf中的两个标题?
提前致谢.
pdf 导出功能目前仅考虑 1 行列标题,因此仅导出一个标题行。
为了导出两个标题行,我们可以使用导出按钮中提供的自定义pdf选项。此选项允许我们在导出之前操作 pdf 文档对象。通过参考pdfmake 文档和table的playground,我们可以看到需要进行以下更改才能拥有多个表头行。
headerRows将表的(标题行数)设置为 2。body,因为给定的标题行单元格具有col-Span,需要将空单元格添加到标题行以确保每行具有相同数量的单元格。以下代码片段演示了上述更改。
由于在沙盒 Iframes 中下载(已移除),代码片段中的按钮将不起作用,您可以将以下代码复制到 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>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3043 次 |
| 最近记录: |