Search code examples
datatables

buttons.html5.min.js:8:23391 Uncaught (in promise) TypeError: l is not a function - Datatable Serverside Excel Export


dom: 'Blfrtip',
    buttons: [
        {
            text: "<img src='./assets/img/excel_new_design.svg' style='width:1.5rem; height:1.5rem;' />",
            action: function (e, dt, button, config){
                dt.one('preXhr', function (e, s, data){
                    data.length = -1;
                }).one('draw', function (e, settings, json, xhr){
                    var excelButtonConfig = $.fn.DataTable.ext.buttons.excelHtml5;
                    var addOptions = { exportOptions: { 'columns': ':not(:last-child)'}, filename: file_name };

                    $.extend(true, excelButtonConfig, addOptions);
                    excelButtonConfig.action(e, dt, button, excelButtonConfig);
                }).draw();
            }
        },
    ]

Above is my code to export all data as excel file. I'm getting the excel correctly but following error occurred in the console.

buttons.html5.min.js:8 Uncaught (in promise) TypeError: l is not a function

I have tried to using button.html5.js instead of min file. If I do that It's says

cb is not a function


Solution

  • According to https://cdn.datatables.net/buttons/3.0.0/js/buttons.html5.js, DataTable.ext.buttons.excelHtml5 has a property action which is defined like
    function (e, dt, button, config, cb) {...}

    You defined var excelButtonConfig = $.fn.DataTable.ext.buttons.excelHtml5; and used the function in the following way:
    excelButtonConfig.action(e, dt, button, excelButtonConfig);.

    You didn't pass anything for the cb, so the cb is undefined and not a function, so cb() will throw an error. To avoid the error, you can pass an empty function e.g. () => {}, like
    excelButtonConfig.action(e, dt, button, excelButtonConfig, () => {});.