我正在尝试使用jquery datatable生成xls,csv,pdf报告,但它无法正常工作。
这是示例代码。 css和js链接是
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/tabletools/2.2.4/css/dataTables.tableTools.min.css">
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.10/css/dataTables.bootstrap.min.css">
这是onload函数:
<script type="text/javascript">
$(document).ready(function () {
$('#example').dataTable({
"sDom": 'T<"clear">lfrtip',
"oTableTools": {
"sSwfPath": "https://cdn.datatables.net/tabletools/2.2.4/swf/copy_csv_xls_pdf.swf",
"aButtons": [
{
"sExtends": "collection",
"sButtonText":"Save / Print",
"sPdfMessage": "My Report",
"sTitle": "My Report",
"aButtons": [ "xls", "pdf","print" ]
}
]
}
});
});
</script>
,表格如下:
<div style="padding:1%">
<table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</tfoot>
<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>
</div>
无法理解这是什么错误。请事先帮助。谢谢。
答案 0 :(得分:0)
您需要安装Buttons扩展程序并按照新文档进行操作,因为它已从之前的版本中进行了大量更改。
以下是我使用标准按钮
的示例dataTable = $('#myTable').DataTable({
dom: '<"col-md-7"lr><"col-md-3"B><"col-md-2"f>tr<"col-md-8"i><"col-md-4"p>',
ajax: {
url: myHandler,
type: "POST",
},
lengthMenu: [ [10, 25, 50, 100, -1], [10, 25, 50, 100, "All"] ],
buttons: ['copy', 'excel', 'pdf', 'print'],
serverSide: true,
processing: true,
autoWidth: true,
deferRender: true,
});