How to call Datatable csv button from custom button

前端 未结 4 1958
攒了一身酷
攒了一身酷 2021-01-02 13:34

Need to call csv button from my custom button.

相关标签:
4条回答
  • 2021-01-02 14:12

    If you are trying to trigger the click event of another button and you are using jQuery (according to your tags), you can use

    <button onclick="$('#ExportReporttoExcel').click()">Click me</button>
    

    This selects the element with the id "ExportReporttoExcel" and triggers a click with using jQuery.

    0 讨论(0)
  • 2021-01-02 14:17

    At last i found the solution.

    In Datatable configuration , i added click event for the button to be triggered.

    buttons: [
            { 
                extend: 'csv',
            }
        ]
    
    $("#ExportReporttoExcel").on("click", function() {
        table.button( '.buttons-csv' ).trigger();
    });
    

    This works fine for me thanks for the comments and answers

    0 讨论(0)
  • 2021-01-02 14:27

    dataTables export buttons is by default enriched with signature classes like .buttons-excel, .buttons-pdf, .buttons-csv and so on. Take advantage of that :

    $('#ExportReporttoExcel').on('click', function() {
      $('.buttons-excel').click()
    });
    
    0 讨论(0)
  • 2021-01-02 14:33

    Say you have your own button

    <a href="javascript:;" style="some button style" class="button_export_excel">Export Excel</a>

    And you have your table that you are using the below code for;

    $(document).ready(function () {
        var table = $('#example').DataTable({
            "paging": false,
            "info": false,
            searching: false,
            dom: 'Bfrtip',
            buttons: [
                {
                    extend: 'excelHtml5'
                }
            ]
        });
    });
    

    Then all you need to do is to use this code below:

    $('#example').DataTable({
            "paging": false,
            "info": false,
            buttons: [
                {
                    extend: 'excel'
                },
                {
                    extend: 'csv'
                },
            ]
        });
    $('.button_export_excel').click(() => {
        $('#example').DataTable().buttons(0,0).trigger()
    })
    

    The 0,0 points to excel and if you want to point to csv you do 0,1.

    0 讨论(0)
提交回复
热议问题