如何自定义jquery数据表导出,例如PDF Excel Print和CSV?

时间:2017-09-29 01:31:15

标签: jquery datatables datatables-1.10

我正在使用datatables中的jQuery DataTables。我想自定义这些表的导出文件插件,如CSV,Excel,PDF和打印按钮。如果我打印PDF,它总是在标题中说jQuery数据表文件的标题导出。我该如何定制?我还想在导出CSV,PDF和Excel文件时自定义文件名。我检查了插件中的代码,我无法在导出文件的选项中看到代码来自定义它。我需要扩展程序才能下载吗?对不起,我只是jQuery数据表的新手。

以下是enter image description here

下面的示例

如何为PDF,CSV和Excel文件自定义相同内容。抱歉编辑错误。

如何自定义正在下载的文件名?

感谢有人可以提供帮助。

提前致谢。

1 个答案:

答案 0 :(得分:6)

您可以使用buttons选项自定义文件名和标题。所有按钮都包含自定义filenametitlecsv button以外的选项。 csv button只有filename选项。

以下是按钮选项的参考列表:

以下是摘录。

$(document).ready(function() {
  $('#example').DataTable({
    dom: 'Bfrtip',
    buttons: [{
      extend: 'pdf',
      title: 'Customized PDF Title',
      filename: 'customized_pdf_file_name'
    }, {
      extend: 'excel',
      title: 'Customized EXCEL Title',
      filename: 'customized_excel_file_name'
    }, {
      extend: 'csv',
      filename: 'customized_csv_file_name'
    }]
  });
});
<link href="https://cdn.datatables.net/1.10.12/css/jquery.dataTables.css" rel="stylesheet" />
<link href="https://cdn.datatables.net/buttons/1.2.2/css/buttons.dataTables.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.4.2/js/dataTables.buttons.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.32/pdfmake.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.32/vfs_fonts.js"></script>
<script src="https://cdn.datatables.net/buttons/1.4.2/js/buttons.html5.min.js"></script>
<div class="container">
  <table id="example" class="display nowrap" 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>$3,120</td>
      </tr>
      <tr>
        <td>Garrett Winters</td>
        <td>Director</td>
        <td>Edinburgh</td>
        <td>63</td>
        <td>2011/07/25</td>
        <td>$5,300</td>
      </tr>
    </tbody>
  </table>
</div>