Export to Excel not working on table filter

Export to Excel not working on table filter

patfreakpatfreak Posts: 4Questions: 1Answers: 1

My data table is working with its filter and search functionality:

function filterTable(targetTable) {
    let mappedStatus = $('input:radio[id=optFilter]:checked').val();

    if (mappedStatus !== 'undefined') {
        if (mappedStatus !== 'A') {
            targetTable.columns(3).search(mappedStatus).draw();
        } else {
            targetTable.columns(3).search("").draw();
        }
    }
}

And here is datatable definition:

function reloadTable2(data) {
    if ($.fn.DataTable.isDataTable("#tblPayFiles")) {
        $('#tblPayFiles').DataTable().destroy();
    }

    if (data !== null) {
        var table = $('#tblPayFiles').DataTable({
            lengthChange: false,
            buttons: [
                {
                    titleAttr: 'Export to Excel',
                    extend: 'excelHtml5',
                    className: 'btn btn-success d-none',
                    text: 'Export To Excel',
                    title: '',
                    filename: getExcelFileName(),
                    exportOptions: {
                        columns: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
                        messageTop: null
                    }
                }
            ],
            pageLength: 10,
            destroy: true,
            data: data.details,
            deferRender: true
        });

        return table;
    }
}

The export to excel works properly when the table is freshly loaded. however when filters are applied, note that it will redraw (not reload) the table, and the results are correct. the export to excel suddenly doesn't work anymore. even if i remove the filters, the export doesn't work anymore and I am suspecting that the draw() function seem to remove the event listener for the export. any help on maintaining the export to excel even after filter? the example in this link: https://datatables.net/extensions/buttons/examples/initialisation/export.html, the export to excel works on filter because it uses the default filter.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,275Questions: 26Answers: 4,765

    however when filters are applied, note that it will redraw (not reload) the table, and the results are correct. the export to excel suddenly doesn't work anymore. even if i remove the filters, the export doesn't work anymore

    What happens?

    Do you get any errors?

    Are there errors in your browser's console?

    I don't see anything in your code that stands out as a problem. We will need to see the issue happen in order to help. Please post a link to your page or a tet case replicating the issue.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • patfreakpatfreak Posts: 4Questions: 1Answers: 1
    edited October 2019

    There are no errors in the console. For clarity's sake, I forgot to show how I trigger export to excel.

    I appended the buttons next to paginate:

    function reloadTable2(data) {
        if ($.fn.DataTable.isDataTable("#tblPayFiles")) {
            $('#tblPayFiles').DataTable().destroy();
        }
    
        if (data !== null) {
            var table = $('#tblPayFiles').DataTable({
                // same declaration as above 
            });
    
            // moving the button close to paginate
            table.buttons().container()
                .appendTo('#tblPayFiles_paginate');
    
            return table;
        }
    }
    

    But actually, I have a button outside the datatable that triggers the export. it goes like this:

    $('#btnExportExcel').on('click', function () {
            $('.buttons-excel').click();    
    });
    

    From what I see, the references to the button seems to go away after draw()

  • kthorngrenkthorngren Posts: 20,275Questions: 26Answers: 4,765

    Again, please provide a link to a page showing the issue. Nothing stands out as an obvious issue.

    Kevin

  • patfreakpatfreak Posts: 4Questions: 1Answers: 1

    Oh I saw the problem. I made a test here:

    http://live.datatables.net/yababipi/4/

    Note the "export to excel" button at the bottom. as you filter the location, the button goes away on draw(). that could be reason why the click isn't working when the filters are applied.


  • patfreakpatfreak Posts: 4Questions: 1Answers: 1
    Answer ✓

    I solved the issue, I saw it. I had to add

    dom: "Bfrtip",
    

    on the datatable declaration so at re-draw, the table knows where to put the buttons. the declaration above works only IF at reloadTable and not at redraw.

This discussion has been closed.