【问题标题】:How to customize jquery datatables export such as PDF Excel Print and CSV?如何自定义 jquery 数据表导出,例如 PDF Excel Print 和 CSV?
【发布时间】:2018-03-10 20:48:52
【问题描述】:

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

下面是一个例子

如何自定义 PDF、CSV 和 Excel 文件的内容。很抱歉编辑不当。

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

如果有人可以提供帮助,我们将不胜感激。

提前致谢。

【问题讨论】:

  • 您好 Jayz,dataTable 中没有插件可以将数据导出为 PDF、CSV 或 Excel。如果你想导出数据,那么你需要为这些构建外部函数。数据选择查询与数据表数据选择查询相同。以 CSV、PDF 或 excel 格式导出数据,在线提供各种插件。

标签: jquery datatables datatables-1.10


【解决方案1】:

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

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

这里是sn-p。

$(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>

【讨论】:

  • 谢谢!这有很大帮助!但是我可以再问一件事关于数据表吗?
  • 看起来你的 sn-p 中有一些重复的脚本标签。
  • 有什么办法可以排除某些列吗?是否也包括隐藏列?
  • @masud_moni 您可以使用column-selector 来选择要导出的列,反之亦然。欲了解更多信息,请参阅example
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-22
  • 1970-01-01
  • 2023-04-04
  • 2019-09-12
  • 1970-01-01
  • 2010-12-12
  • 2017-02-02
相关资源
最近更新 更多