【问题标题】:Is it possible to export multiple datatables at once?是否可以一次导出多个数据表?
【发布时间】:2017-12-19 12:07:04
【问题描述】:

我需要在一个页面中导出多个数据表,并且页面上的所有表都需要一个导出按钮?

任何建议的链接。 谢谢!

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    是的,你可以。您需要获取所有表的对象并在所有对象上触发导出功能。示例代码如下:

    function exportAllDatatables() {
    
        var table = $('#myTable').DataTable();
        var table2 = $('#myTable2').DataTable();
    
        //Trigger the button at index 2-1 (Wherever your export button in each table):
        table.button( '2-1' ).trigger();
        table2.button( '2-1' ).trigger();
    }
    

    此处引用导出按钮的触发功能: https://datatables.net/reference/api/button%28%29.trigger%28%29

    【讨论】:

    • 如果你能提供一个小提琴就太好了
    • 如何合并这两个表?以及如何在 PDF 中显示这些数据?
    • 请做一些研究,自己尝试一些代码,在问题中添加一些代码,并更新您的问题,了解究竟什么不起作用以及您尝试了什么。我给了你trigger函数的代码示例,这对你的情况很有用。
    【解决方案2】:

    是的,即使有按钮集合也是可能的。 这是高级演示https://jsfiddle.net/4kpvba23/4/(可以同时导出两个或多个表的csv、excel、pdf)

    正如 Kavish Mittai 所指出的,您也可以在集合中触发单个按钮或多个按钮。

    按钮 API (https://datatables.net/reference/api/button())

    按钮 API (https://datatables.net/reference/api/buttons())

    按钮触发 API (https://datatables.net/reference/api/buttons().trigger())

    注意:记得点击全部导出会提示“允许多次下载?” 在浏览器中以避免有害下载,这取决于用户 交互你不能禁用它。

    演示:

    $(document).ready(function() {
    
        var settings = {
            "dom": "<'row'<'col-sm-3 col-left'l><'col-xs-6 col-right'B><'col-xs-3 col-right'f>r>t<'row'<'col-xs-3 col-left'i><'col-xs-9 col-right'p>>",
            "buttons": [
                'colvis',
                {
                    extend: 'collection',
                    text: 'Select',
                    buttons: [
                        'selectAll',
                        'selectNone'
                    ]
                },
                {
                    extend: 'collection',
                    text: 'Copy',
                    buttons: [{
                        extend: 'copy',
                        text: 'Copy Selected',
                        exportOptions: {
                            stripHtml: true,
                            modifier: {
                                selected: true
                            }
                        }
                    }, {
                        extend: 'copy',
                        text: 'Copy Current Page',
                        exportOptions: {
                            stripHtml: true,
                            modifier: {
                                selected: false,
                                page: 'current',
                                search: 'applied'
                            }
                        }
                    }]
                },
                {
                    extend: 'collection',
                    text: 'CSV',
                    buttons: [{
                        extend: 'csvHtml5',
                        text: 'Export to CSV Selected Rows',
                        exportOptions: {
                            columns: ':visible',
                            stripHtml: true,
                            modifier: {
                                selected: true
                            }
                        }
                    }, {
                        extend: 'csvHtml5',
                        text: 'Export to CSV Current Page',
                        exportOptions: {
                            columns: ':visible',
                            stripHtml: true,
                            modifier: {
                                selected: false,
                                page: 'current',
                                search: 'applied'
                            }
                        }
                    }]
                },
                {
                    extend: 'collection',
                    text: 'Excel',
                    buttons: [{
                        extend: 'excelHtml5',
                        text: 'Export to Excel Selected Rows',
                        exportOptions: {
                            columns: ':visible',
                            stripHtml: true,
                            modifier: {
                                selected: true
                            }
                        }
                    }, {
                        extend: 'excelHtml5',
                        text: 'Export to Excel Current Page',
                        exportOptions: {
                            columns: ':visible',
                            stripHtml: true,
                            modifier: {
                                selected: false,
                                page: 'current',
                                search: 'applied'
                            }
                        }
                    }]
                },
                {
                    extend: 'collection',
                    text: 'PDF',
                    buttons: [{
                        extend: 'pdfHtml5',
                        orientation: 'landscape',
                        pageSize: 'A4',
                        text: 'Export to PDF Selected Rows',
                        exportOptions: {
                            columns: ':visible',
                            stripHtml: true,
                            modifier: {
                                selected: true
                            }
                        }
                    }, {
    
                        extend: 'pdfHtml5',
                        orientation: 'landscape',
                        pageSize: 'A4',
                        text: 'Export to PDF Current Page',
                        exportOptions: {
                            columns: ':visible',
                            stripHtml: true,
                            modifier: {
                                selected: false,
                                page: 'current',
                                search: 'applied'
                            }
                        }
                    }]
                }, {
                    extend: 'collection',
                    text: 'Print',
                    buttons: [{
                        extend: 'print',
                        text: 'Print Selected Rows',
                        orientation: 'landscape',
                        exportOptions: {
                            columns: ':visible',
                            stripHtml: false,
                            modifier: {
                                selected: true
                            }
                        }
                    }, {
                        extend: 'print',
                        text: 'Print Current Page',
                        orientation: 'landscape',
                        pageSize: 'A4',
                        exportOptions: {
                            columns: ':visible',
                            stripHtml: false,
                            modifier: {
                                selected: false,
                                page: 'current',
                                search: 'applied'
                            }
                        }
                    }]
                }
            ]
        };
    
        var table = $('#example').DataTable(settings);
        var table2 = $('#example2').DataTable(settings);
    
        $(document).on('click', '#exportAll', function() {
    
            /*
             * For Table 1
             * ===========
             */
    
            //Index of "csv" button is 3 and in it "export to csv current  page" is at index 1 so it'll be 3-1
            //Index of "excel" button is 4 and in it "export to excel current  page" is at index 1 so it'll be 4-1
            //Index of "pdf" button is 5 and in it "export to pdf current  page" is at index 1 so it'll be 5-1
            table.buttons(['3-1', '4-1', '5-1']).trigger();
    
            /*
             * For Table 2
             * ===========
             */
    
            //Index of "csv" button is 3 and in it "export to csv current  page" is at index 1 so it'll be 3-1
            //Index of "excel" button is 4 and in it "export to excel current  page" is at index 1 so it'll be 4-1
            //Index of "pdf" button is 5 and in it "export to pdf current  page" is at index 1 so it'll be 5-1
            table2.buttons(['3-1', '4-1', '5-1']).trigger();
        });
    });
    #example_wrapper{
    	margin-bottom: 100px !important;
    }
    <link href="https://cdn.datatables.net/v/dt/jszip-2.5.0/dt-1.10.16/af-2.2.2/b-1.5.0/b-colvis-1.5.0/b-flash-1.5.0/b-html5-1.5.0/b-print-1.5.0/cr-1.4.1/fc-3.2.4/fh-3.1.3/kt-2.3.2/r-2.2.1/rg-1.0.2/rr-1.2.3/sc-1.4.3/sl-1.2.4/datatables.min.css" rel="stylesheet"
    />
    
    <button id="exportAll">Export All</button>
    <table id="example" class="display" 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>Herrod Chandler</td>
          <td>Sales Assistant</td>
          <td>San Francisco</td>
          <td>59</td>
          <td>2012/08/06</td>
          <td>$137,500</td>
        </tr>
        <tr>
          <td>Rhona Davidson</td>
          <td>Integration Specialist</td>
          <td>Tokyo</td>
          <td>55</td>
          <td>2010/10/14</td>
          <td>$327,900</td>
        </tr>
        <tr>
          <td>Colleen Hurst</td>
          <td>Javascript Developer</td>
          <td>San Francisco</td>
          <td>39</td>
          <td>2009/09/15</td>
          <td>$205,500</td>
        </tr>
        <tr>
          <td>Sonya Frost</td>
          <td>Software Engineer</td>
          <td>Edinburgh</td>
          <td>23</td>
          <td>2008/12/13</td>
          <td>$103,600</td>
        </tr>
        <tr>
          <td>Jena Gaines</td>
          <td>Office Manager</td>
          <td>London</td>
          <td>30</td>
          <td>2008/12/19</td>
          <td>$90,560</td>
        </tr>
        <tr>
          <td>Quinn Flynn</td>
          <td>Support Lead</td>
          <td>Edinburgh</td>
          <td>22</td>
          <td>2013/03/03</td>
          <td>$342,000</td>
        </tr>
        <tr>
          <td>Charde Marshall</td>
          <td>Regional Director</td>
          <td>San Francisco</td>
          <td>36</td>
          <td>2008/10/16</td>
          <td>$470,600</td>
        </tr>
        <tr>
          <td>Haley Kennedy</td>
          <td>Senior Marketing Designer</td>
          <td>London</td>
          <td>43</td>
          <td>2012/12/18</td>
          <td>$313,500</td>
        </tr>
        <tr>
          <td>Tatyana Fitzpatrick</td>
          <td>Regional Director</td>
          <td>London</td>
          <td>19</td>
          <td>2010/03/17</td>
          <td>$385,750</td>
        </tr>
        <tr>
          <td>Michael Silva</td>
          <td>Marketing Designer</td>
          <td>London</td>
          <td>66</td>
          <td>2012/11/27</td>
          <td>$198,500</td>
        </tr>
        <tr>
          <td>Paul Byrd</td>
          <td>Chief Financial Officer (CFO)</td>
          <td>New York</td>
          <td>64</td>
          <td>2010/06/09</td>
          <td>$725,000</td>
        </tr>
        <tr>
          <td>Gloria Little</td>
          <td>Systems Administrator</td>
          <td>New York</td>
          <td>59</td>
          <td>2009/04/10</td>
          <td>$237,500</td>
        </tr>
      </tbody>
    </table>
    <table id="example2" class="display" 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>Suki Burks</td>
          <td>Developer</td>
          <td>London</td>
          <td>53</td>
          <td>2009/10/22</td>
          <td>$114,500</td>
        </tr>
        <tr>
          <td>Prescott Bartlett</td>
          <td>Technical Author</td>
          <td>London</td>
          <td>27</td>
          <td>2011/05/07</td>
          <td>$145,000</td>
        </tr>
        <tr>
          <td>Gavin Cortez</td>
          <td>Team Leader</td>
          <td>San Francisco</td>
          <td>22</td>
          <td>2008/10/26</td>
          <td>$235,500</td>
        </tr>
        <tr>
          <td>Martena Mccray</td>
          <td>Post-Sales support</td>
          <td>Edinburgh</td>
          <td>46</td>
          <td>2011/03/09</td>
          <td>$324,050</td>
        </tr>
        <tr>
          <td>Unity Butler</td>
          <td>Marketing Designer</td>
          <td>San Francisco</td>
          <td>47</td>
          <td>2009/12/09</td>
          <td>$85,675</td>
        </tr>
        <tr>
          <td>Howard Hatfield</td>
          <td>Office Manager</td>
          <td>San Francisco</td>
          <td>51</td>
          <td>2008/12/16</td>
          <td>$164,500</td>
        </tr>
        <tr>
          <td>Hope Fuentes</td>
          <td>Secretary</td>
          <td>San Francisco</td>
          <td>41</td>
          <td>2010/02/12</td>
          <td>$109,850</td>
        </tr>
        <tr>
          <td>Vivian Harrell</td>
          <td>Financial Controller</td>
          <td>San Francisco</td>
          <td>62</td>
          <td>2009/02/14</td>
          <td>$452,500</td>
        </tr>
        <tr>
          <td>Timothy Mooney</td>
          <td>Office Manager</td>
          <td>London</td>
          <td>37</td>
          <td>2008/12/11</td>
          <td>$136,200</td>
        </tr>
        <tr>
          <td>Jackson Bradshaw</td>
          <td>Director</td>
          <td>New York</td>
          <td>65</td>
          <td>2008/09/26</td>
          <td>$645,750</td>
        </tr>
        <tr>
          <td>Olivia Liang</td>
          <td>Support Engineer</td>
          <td>Singapore</td>
          <td>64</td>
          <td>2011/02/03</td>
          <td>$234,500</td>
        </tr>
        <tr>
          <td>Bruno Nash</td>
          <td>Software Engineer</td>
          <td>London</td>
          <td>38</td>
          <td>2011/05/03</td>
          <td>$163,500</td>
        </tr>
        <tr>
          <td>Sakura Yamamoto</td>
          <td>Support Engineer</td>
          <td>Tokyo</td>
          <td>37</td>
          <td>2009/08/19</td>
          <td>$139,575</td>
        </tr>
        <tr>
          <td>Thor Walton</td>
          <td>Developer</td>
          <td>New York</td>
          <td>61</td>
          <td>2013/08/11</td>
          <td>$98,540</td>
        </tr>
        <tr>
          <td>Finn Camacho</td>
          <td>Support Engineer</td>
          <td>San Francisco</td>
          <td>47</td>
          <td>2009/07/07</td>
          <td>$87,500</td>
        </tr>
        <tr>
          <td>Zenaida Frank</td>
          <td>Software Engineer</td>
          <td>New York</td>
          <td>63</td>
          <td>2010/01/04</td>
          <td>$125,250</td>
        </tr>
        <tr>
          <td>Jennifer Acosta</td>
          <td>Junior Javascript Developer</td>
          <td>Edinburgh</td>
          <td>43</td>
          <td>2013/02/01</td>
          <td>$75,650</td>
        </tr>
    
      </tbody>
    </table>
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.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/v/dt/jszip-2.5.0/dt-1.10.16/af-2.2.2/b-1.5.0/b-colvis-1.5.0/b-flash-1.5.0/b-html5-1.5.0/b-print-1.5.0/cr-1.4.1/fc-3.2.4/fh-3.1.3/kt-2.3.2/r-2.2.1/rg-1.0.2/rr-1.2.3/sc-1.4.3/sl-1.2.4/datatables.min.js"></script>

    【讨论】:

    • 是的,它适用于多个数据表和多个 PDF、Csv 等,但我需要一个 PDF 中的所有表意味着在单个 PDF 文件中导出两个或多个数据表。这可能吗?
    猜你喜欢
    • 2014-04-09
    • 1970-01-01
    • 1970-01-01
    • 2017-02-28
    • 2010-12-04
    • 1970-01-01
    • 1970-01-01
    • 2019-06-16
    • 2023-01-19
    相关资源
    最近更新 更多