代码之家  ›  专栏  ›  技术社区  ›  zzMzz

DataTables导出按钮问题-按钮未显示

  •  5
  • zzMzz  · 技术社区  · 7 年前

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    
    <link rel="stylesheet" type="text/css" href="DataTables/datatables.css"/>
    
    <script type="text/javascript" src="DataTables/datatables.js"></script>
    
    
    <script type="text/javascript" charset="utf-8">
    $(document).ready(function(){
      $('#example').DataTable( {
          dom: 'Bfrtip',
          buttons: [
          {
            extend: 'collection',
            text: 'Export',
            buttons: [
              'copy',
              'excel',
              'csv',
              'pdf',
              'print'
            ]
          }         
        ]
      });
    } );
    </script>
    

    虽然我使用了DataTables下载生成器,但我想知道我是否只是缺少了一个依赖项。

    2 回复  |  直到 7 年前
        1
  •  14
  •   Caique Romero    5 年前

    https://datatables.net/extensions/buttons/examples/initialisation/simple.html

    外部代码:

    https://cdn.datatables.net/1.10.15/css/jquery.dataTables.min.css
    https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js
    https://cdn.datatables.net/buttons/1.4.0/js/dataTables.buttons.min.js
    https://cdn.datatables.net/buttons/1.4.0/js/buttons.flash.min.js
    https://cdn.datatables.net/buttons/1.4.0/js/buttons.html5.min.js
    https://cdn.datatables.net/buttons/1.4.0/js/buttons.print.min.js
    https://cdn.datatables.net/buttons/1.4.0/css/buttons.dataTables.min.css
    https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js
    https://cdn.rawgit.com/bpampuch/pdfmake/0.1.27/build/pdfmake.min.js
    https://cdn.rawgit.com/bpampuch/pdfmake/0.1.27/build/vfs_fonts.js
    

    $(document).ready(function() {
        $('#example').DataTable( {
            dom: 'Bfrtip',
            buttons: [
                'copy', 'csv', 'excel', 'pdf', 'print'
            ]
        } );
    } );
    <link rel="stylesheet" type="text/css"  href="https://cdn.datatables.net/1.10.15/css/jquery.dataTables.min.css" />
    <link rel="stylesheet" type="text/css"  href="https://cdn.datatables.net/buttons/1.4.0/css/buttons.dataTables.min.css" />
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
    <script src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.27/build/pdfmake.min.js"></script>
    <script src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.27/build/vfs_fonts.js"></script>
    <script src="https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.4.0/js/dataTables.buttons.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.4.0/js/buttons.flash.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.4.0/js/buttons.html5.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.4.0/js/buttons.print.min.js"></script>
    
    
    <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>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>
        2
  •  0
  •   Gufran Hasan    3 年前

    您还可以使用以下技巧显示导出按钮。

    var table = $('#example').DataTable({
                buttons: [
                    'copy', 'csv', 'excel', 'pdf', 'print'
                ]
            });
            $('#btn-place').html(table.buttons().container()); 
    

    现在创建一个div,在其中添加这些按钮。

    <div id="btn-place"></div>
    

    Reference link