13

Need to call csv button from my custom button.

<button value="Export report to Excel" class="button-default datatable-csv" type="button" id="ExportReporttoExcel">
                <span>Export report to Excel</span>
            </button>

Instead of calling it from the Datatable button, i want the same functionality from the above button.

Looking for some configuration changes in Datatable so that i can call my custom button to export the table records as csv.

var table=$('#tableId').DataTable( {
    "paging":   true,
    "info":     true,
    "searching": true,      
    ,buttons: true
});


$("#SEARCH").on("keyup search input paste cut", function() {
    table.search(this.value).draw();
});

var buttons = new $.fn.dataTable.Buttons(table, {
     buttons: [
       'csvHtml5'
    ]
}).container().appendTo($('#ExportReporttoExcel'));

Getting output like below but i need only one button.

enter image description here

3
  • 1
    Can't you just add onclick="yourFunction()" attribute? Commented Aug 4, 2017 at 21:15
  • when you say "call the button" you mean pragmatically click it right? Commented Aug 4, 2017 at 22:19
  • Yes Neville Nazerane, need to export the table to csv file Commented Aug 7, 2017 at 12:38

5 Answers 5

19

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

Sign up to request clarification or add additional context in comments.

2 Comments

Its not working for me.Its giving me error that table.button is not a function();
My code is: $("#btnExport").on("click", function() { var table = $('#tableList').DataTable(); table.button( '.buttons-excel' ).trigger(); });
6

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()
});

1 Comment

I no need of the Datatable buttons at all, in other words i need to customizes the button by changing the location and style
2

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.

Comments

0

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.

1 Comment

yeah this answer shows how to trigger the click of the id ExportReporttoExcel. I wasn't sure what your question was but based on your answer it seems you needed to alter the click event of the #ExportReporttoExcel. you can still use the above code if you want the button to do the same on click.
0

I just got to try this and the extend: 'csv' still showed the built-in CSV button for me, also the triggering calls mentioned above didn't work for me either, but the below did.

This is what worked for me.

buttons: ['csv'],

Then hide the built-in CSV button after creating the Datatable, as follows:

$(".buttons-csv").hide();

Then trigger its click event from your special button handler.

$("#myExportBtn").on('click', function (ev) {
      $(".buttons-csv").trigger("click");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.