Excel export does not show all rows and select amount of register disappeared

Excel export does not show all rows and select amount of register disappeared

vbalsamellovbalsamello Posts: 2Questions: 1Answers: 0

I am using a ajax method to fill the datatable. When try to include buttons to export to excel file the option fro selecting the amount of record shown in table disappeared,. And the excel option does no download all rows, just the rows that the user is seeing.

table = $('#myTable').DataTable({
                "serverSide": true,
                "ajax": "myMethod",
                "lengthMenu": [[10 , 25, 100, -1], [10,25, 100, "all"]],
                "pageLength": 10,
                "dom": 'Bfrtip',
                "buttons": [{
                    extend: 'excel',
                    text: '<span class="fa fa-file-excel-o"></span> Excel',
                    exportOptions: {
                        modifier: {
                            order: 'current',
                            page: 'all',
                            selected: false
                        }
                    }
                }],
                "pagingType": "full_numbers",
                "createdRow": function (row, data, dataIndex) {
                    console.log(data);
                    agregarColorSegunEstado(data, row);
                    agregarAbrirDetalle(data, row);
                },
                "columnDefs": [
                    {"width": "1%", "targets": 0}
                ],
                "bFilter": true
            });
        });

Answers

  • colincolin Posts: 15,112Questions: 1Answers: 2,583

    Hi @vbalsamello ,

    We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • kthorngrenkthorngren Posts: 20,142Questions: 26Answers: 4,736

    Please see this FAQ:
    https://datatables.net/faqs/index#buttons

    Kevin

  • vbalsamellovbalsamello Posts: 2Questions: 1Answers: 0
    edited May 2019

    Thanks it's resolve with the following

    var oldExportAction = function (self, e, dt, button, config) {
                    if (button[0].className.indexOf('buttons-excel') >= 0) {
                        if ($.fn.dataTable.ext.buttons.excelHtml5.available(dt, config)) {
                            $.fn.dataTable.ext.buttons.excelHtml5.action.call(self, e, dt, button, config);
                        }
                        else {
                            $.fn.dataTable.ext.buttons.excelFlash.action.call(self, e, dt, button, config);
                        }
                    } else if (button[0].className.indexOf('buttons-print') >= 0) {
                        $.fn.dataTable.ext.buttons.print.action(e, dt, button, config);
                    }
                };
    
                var newExportAction = function (e, dt, button, config) {
                    var self = this;
                    var oldStart = dt.settings()[0]._iDisplayStart;
    
                    dt.one('preXhr', function (e, s, data) {
                        // Just this once, load all data from the server...
                        data.start = 0;
                        data.length = 2147483647;
    
                        dt.one('preDraw', function (e, settings) {
                            // Call the original action function
                            oldExportAction(self, e, dt, button, config);
    
                            dt.one('preXhr', function (e, s, data) {
                                // DataTables thinks the first item displayed is index 0, but we're not drawing that.
                                // Set the property to what it was before exporting.
                                settings._iDisplayStart = oldStart;
                                data.start = oldStart;
                            });
    
                            // Reload the grid with the original page. Otherwise, API functions like table.cell(this) don't work properly.
                            setTimeout(dt.ajax.reload, 0);
    
                            // Prevent rendering of the full data to the DOM
                            return false;
                        });
                    });
    
                    // Requery the server with the new one-time export settings
                    dt.ajax.reload();
                };
    
    

    And then adding to our dataTable configuraion the ACTION

    "buttons": [{
                        extend: 'excel',
                        text: 'Exportar a Excel',
                        action: newExportAction
                    }],
    
    
This discussion has been closed.