我在javascript中有一个数据表。加载的JS库按以下顺序列出。
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.19/js/jquery.dataTables.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.5.2/js/dataTables.buttons.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.5.2/js/buttons.flash.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
<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/buttons/1.5.2/js/buttons.html5.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.5.2/js/buttons.print.min.js"></script>
数据表的创建如下:
myTable = $('#example').on('error.dt', function (e, settings, techNote, message) {
console.log('An error has been reported by DataTables: ', message);
}).DataTable({
dom: 'lBfrtip',
buttons: ['copyHtml5', 'excelHtml5', 'pdfHtml5', 'csvHtml5'
],
fixedColumns: true,
"columnDefs": [{
//"bSortable": false,
"width": '60%',
"defaultContent": "-",
"targets": "_all"
}]
});
问题是,我在浏览器上看不到导出按钮。我正在使用Google Chrome。在类似的问题中,人们说JS库的顺序很重要,但是我的顺序与它们的顺序相匹配。在“网站设置”中,还允许使用Flash。当我按F12键时,Chrome控制台中没有错误。我找不到为什么按钮没有显示在屏幕上的原因。尝试在设置按钮时不使用Html5,但无效。任何帮助将不胜感激。
答案 0 :(得分:2)
您可能已经做到了,但是只是为了检查...您是否还为按钮加载了CSS?
<link href="https://cdn.datatables.net/buttons/1.5.1/css/buttons.dataTables.min.css" rel="stylesheet">
这个基于您代码的示例对我有用。
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>DataTables With Export</title>
<link href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css" rel="stylesheet">
<link href="https://cdn.datatables.net/buttons/1.5.1/css/buttons.dataTables.min.css" rel="stylesheet">
</head>
<body>
<h1>DataTables</h1>
<table class="table display" id="example" style="width:100%">
<thead>
<tr>
<th>Col 1</th>
<th>Col 2</th>
<th>Col 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
</tr>
</tbody>
</table>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.19/js/jquery.dataTables.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.5.2/js/dataTables.buttons.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.5.2/js/buttons.flash.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
<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/buttons/1.5.2/js/buttons.html5.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.5.2/js/buttons.print.min.js"></script>
<script>
jQuery(document).ready(function($) {
myTable = $('#example').on('error.dt', function (e, settings, techNote, message) {
console.log('An error has been reported by DataTables: ', message);
}).DataTable({
dom: 'lBfrtip',
buttons: ['copyHtml5', 'excelHtml5', 'pdfHtml5', 'csvHtml5'],
fixedColumns: true,
"columnDefs": [{
//"bSortable": false,
"width": '60%',
"defaultContent": "-",
"targets": "_all"
}]
});
} );
</script>
</body>
</html>