导出到excel多次下载

时间:2017-06-25 03:18:43

标签: javascript ajax datatables

我有一个jquery数据表,当用户单击加载按钮时,该数据表由来自DB的ajax调用填充。根据用户在日期选择器中提供的日期填充数据。我还有一个导出按钮,用于将加载的数据表导出到excel。导出与页面加载附带的数据表一起正常工作。但是,当再次填充数据表时,会下载多个文件。

这是html



$(document).ready(function() {

  //export to excel Districts Remittance list
  $('#btnExcelDistrictsRemittance').click(function() {
    $('<table>').append($("#remittanceDistrict").DataTable().$('tr').clone()).table2excel({
      exclude: ".noXls",
      name: "Districts Remittance",
      filename: 'Districts Remittance-' + date //do not include extension
   
  });
}


})
&#13;
<script src="js/dataTables/jquery.table2excel.min.js"></script>
<div id="month-group" class="input-group">
  <i class="fa fa-calendar">&nbsp</i>
  <asp:TextBox ID="selecttMonth" ClientIDMode="Static" runat="server"></asp:TextBox><i class="fa fa-refresh" id="btnGo"></i>
</div>
<button type="button" id="btnExcelDistrictsRemittance" class="export-to-excel" title="Export to Excel"><i class="fa fa-files-o fa-2x"></i></button>
<table id="remittanceDistrict" class="table  table-bordered table-hover table-responsive table-striped" style="margin-top: 10px">
  <thead class="arrear-table">
    <tr>
      <th>#</th>
      <th>District</th>
      <th>Amount</th>
    </tr>
  </thead>
</table>
&#13;
&#13;
&#13;

对于导出我正在使用table2excel.js插件。注意: - 我发现$('#btnExcelDistrictsRemittance').click()多次触发。任何帮助表示赞赏。请原谅,如果问题太广泛,我是社区的新手

2 个答案:

答案 0 :(得分:3)

如前所述,问题在于$'(#btnExcelDistrictsRemittance').click()多次触发。

我自己通过unbinding元素中的所有events解决了这个问题。 更新了以下代码:

$'(#btnExcelDistrictsRemittance').unbind().click() {
    // whatever
}

<强>更新

在较新版本的jquery中,您可以使用off()方法 $'(#btnExcelDistrictsRemittance').Off().click() { // whatever }

答案 1 :(得分:0)

确定使用datatable插件而不是table2excel.js插件。

此解决方案有更多选项,几乎可以完美运行。

仅适用于excel的示例:

$(document).ready(function() {
    $('#table_id').DataTable( {
        dom: 'Bfrtip',
        buttons: [
            'excel'
        ]
    } );
} );
/*
 * Table
 */
table.dataTable {
    margin: 0 auto;
    clear: both;
    width: 100%;
}

table.dataTable thead th {
    padding: 3px 18px 3px 10px;
    border-bottom: 1px solid black;
    font-weight: bold;
    cursor: pointer;
    *cursor: hand;
}

table.dataTable tfoot th {
    padding: 3px 18px 3px 10px;
    border-top: 1px solid black;
    font-weight: bold;
}

table.dataTable td {
    padding: 3px 10px;
}

table.dataTable td.center,
table.dataTable td.dataTables_empty {
    text-align: center;
}

table.dataTable tr.odd { background-color: #E2E4FF; }
table.dataTable tr.even { background-color: white; }

table.dataTable tr.odd td.sorting_1 { background-color: #D3D6FF; }
table.dataTable tr.odd td.sorting_2 { background-color: #DADCFF; }
table.dataTable tr.odd td.sorting_3 { background-color: #E0E2FF; }
table.dataTable tr.even td.sorting_1 { background-color: #EAEBFF; }
table.dataTable tr.even td.sorting_2 { background-color: #F2F3FF; }
table.dataTable tr.even td.sorting_3 { background-color: #F9F9FF; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="<script src = "https://code.jquery.com/jquery-1.12.4.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.3.1/js/dataTables.buttons.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.3.1/js/buttons.flash.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/buttons/1.3.1/js/buttons.html5.min.js"></script>
<script src= "https://cdn.datatables.net/buttons/1.3.1/js/buttons.print.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css"/>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/buttons/1.3.1/css/buttons.dataTables.min.css"/>
<div>
<table id="table_id">   
        <thead>        
            <tr>            
                <th>Name</th>         
                <th>Age</th>               
                <th>Bot</th> 
            </tr>   
        </thead> 
        <tbody>  
            <tr>       
                <td>John</td>
                <td>25</td>   
                <td><button type="button" id="myBtn_1">Select</button></td> 
                
            </tr>
            <tr>
                <td>Ana</td>
                <td>22</td>     
                <td><button type="button" id="myBtn_2">Select</button></td>
           </tr>
           <tr>
                <td>Diana</td>
                <td>23</td>    
                <td><button type="button" id="myBtn_3">Select</button></td> 
           </tr>
           <tr>
                <td>Lino</td>
                <td>32</td>    
                <td><button type="button" id="myBtn_4">Select</button></td> 
           </tr>
        </tbody>
    </table>
</div>

所有选项:

$(document).ready(function() {
    $('#table_id').DataTable( {
        dom: 'Bfrtip',
        buttons: [
            'copy', 'csv', 'excel', 'pdf', 'print'
        ]
    } );
} );
/*
 * Table
 */
table.dataTable {
    margin: 0 auto;
    clear: both;
    width: 100%;
}

table.dataTable thead th {
    padding: 3px 18px 3px 10px;
    border-bottom: 1px solid black;
    font-weight: bold;
    cursor: pointer;
    *cursor: hand;
}

table.dataTable tfoot th {
    padding: 3px 18px 3px 10px;
    border-top: 1px solid black;
    font-weight: bold;
}

table.dataTable td {
    padding: 3px 10px;
}

table.dataTable td.center,
table.dataTable td.dataTables_empty {
    text-align: center;
}

table.dataTable tr.odd { background-color: #E2E4FF; }
table.dataTable tr.even { background-color: white; }

table.dataTable tr.odd td.sorting_1 { background-color: #D3D6FF; }
table.dataTable tr.odd td.sorting_2 { background-color: #DADCFF; }
table.dataTable tr.odd td.sorting_3 { background-color: #E0E2FF; }
table.dataTable tr.even td.sorting_1 { background-color: #EAEBFF; }
table.dataTable tr.even td.sorting_2 { background-color: #F2F3FF; }
table.dataTable tr.even td.sorting_3 { background-color: #F9F9FF; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="<script src = "https://code.jquery.com/jquery-1.12.4.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.3.1/js/dataTables.buttons.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.3.1/js/buttons.flash.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/buttons/1.3.1/js/buttons.html5.min.js"></script>
<script src= "https://cdn.datatables.net/buttons/1.3.1/js/buttons.print.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css"/>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/buttons/1.3.1/css/buttons.dataTables.min.css"/>
<div>
<table id="table_id">   
        <thead>        
            <tr>            
                <th>Name</th>         
                <th>Age</th>               
                <th>Bot</th> 
            </tr>   
        </thead> 
        <tbody>  
            <tr>       
                <td>John</td>
                <td>25</td>   
                <td><button type="button" id="myBtn_1">Select</button></td> 
                
            </tr>
            <tr>
                <td>Ana</td>
                <td>22</td>     
                <td><button type="button" id="myBtn_2">Select</button></td>
           </tr>
           <tr>
                <td>Diana</td>
                <td>23</td>    
                <td><button type="button" id="myBtn_3">Select</button></td> 
           </tr>
           <tr>
                <td>Lino</td>
                <td>32</td>    
                <td><button type="button" id="myBtn_4">Select</button></td> 
           </tr>
        </tbody>
    </table>
</div>

重要说明:

部分功能可能无法在代码段中使用(打印);

初始化是必需的,没有它们代码不起作用。

有关详情,请访问此链接:https://datatables.net/extensions/buttons/examples/initialisation/export.html

有任何疑问,请求我的帮助。