javascript - 数据表 PDFExport 将行内容垂直居中对齐
问题描述
这是我的示例代码:
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.23/css/jquery.dataTables.css"/>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/buttons/1.6.5/css/buttons.dataTables.css"/>
</head>
<body>
<table id="example" class="display" style="width:100%">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start<br />date</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr>
<td>Tiger<br />Nixon</td>
<td>System<br />Architect</td>
<td>Edinburgh</td>
<td>61</td>
<td>2011/04/25</td>
<td>$320,800</td>
</tr>
<tr>
<td>Garrett<br />Winters</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>63</td>
<td>2011/07/25</td>
<td>$170,750</td>
</tr>
<tr>
<td>Ashton<br />Cox</td>
<td>Junior<br />Technical<br />Author</td>
<td>San<br />Francisco</td>
<td>66</td>
<td>2009/01/12</td>
<td>$86,000</td>
</tr>
<tr>
<td>Cedric<br />Kelly</td>
<td>Senior<br />Javascript<br />Developer</td>
<td>Edinburgh</td>
<td>22</td>
<td>2012/03/29</td>
<td>$433,060</td>
</tr>
<tr>
<td>Airi<br />Satou</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>33</td>
<td>2008/11/28</td>
<td>$162,700</td>
</tr>
</tbody>
<tfoot>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start<br />date</th>
<th>Salary</th>
</tr>
</tfoot>
</table>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/pdfmake.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/1.10.23/js/jquery.dataTables.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.6.5/js/dataTables.buttons.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.6.5/js/buttons.html5.js"></script>
<script>
$(document).ready(function() {
$('#example').DataTable({
dom: "Bftrip",
buttons: [
{
extend: 'pdfHtml5',
footer: true,
exportOptions: {
format: {
header: function ( data, row, column, node ) {
return data.replace(/<br\s*\/?>/ig, "\r\n");
},
body: function ( data, row, column, node ) {
return data.replace(/<br\s*\/?>/ig, "\r\n");
},
footer: function ( data, row, column, node ) {
return data.replace(/<br\s*\/?>/ig, "\r\n");
}
},
},
customize: function(doc) {
doc.pageMargins = [50, 50, 50, 50];
doc.content.splice(0,1);
doc.content[0].table.widths = [80, 80, 80, 50, 80, 80];
var rowCount = document.getElementById('example').rows.length-1;
for(var a=1; a<rowCount; a++) {
doc.content[0].table.body[a][0].alignment = 'left';
doc.content[0].table.body[a][1].alignment = 'center';
doc.content[0].table.body[a][2].alignment = 'center';
doc.content[0].table.body[a][3].alignment = 'center';
doc.content[0].table.body[a][4].alignment = 'center';
doc.content[0].table.body[a][5].alignment = 'right';
}
var objLayout = {};
objLayout['hLineWidth'] = function(i) { return .5; };
objLayout['vLineWidth'] = function(i) { return .5; };
objLayout['hLineColor'] = function(i) { return '#aaa'; };
objLayout['vLineColor'] = function(i) { return '#aaa'; };
objLayout['paddingLeft'] = function(i) { return 2; };
objLayout['paddingRight'] = function(i) { return 2; };
doc.content[0].layout = objLayout;
}
}
]
});
});
</script>
</body>
</html>
解决方案
推荐阅读
- linux - yum lock issue on amazon linux ec2
- python - Odoo - 在 python 中使用 onchange 事件在字段元素上设置“不可见”或“必需”属性
- windows - Windows上的WebSerial,未找到设备
- excel - SUMIFS - 对文本中所有带有“gelato”的单元格求和,但不包括“gelato_lime”的单元格
- google-oauth - oauth2 flow enpoint 是否也执行 google action 的健康检查?
- node.js - Dropbox 图像 URL 未在我的移动应用(iOS Chrome)中呈现为
- java - Why must I declare an exception if I know it will never be thrown?
- python - 如何在 Python 中使用多个 if 语句创建菜单
- catboost - Catboost Java 接口
- tensorflow - 如何从 Tensorflow 结果中的 .pb 文件中获取 .weights 文件