How can I use the excel button to export filtered and unfiltered data? No AJAX in use.

How can I use the excel button to export filtered and unfiltered data? No AJAX in use.

kjtocoolkjtocool Posts: 4Questions: 1Answers: 0

High Level: I have a table that I am filtering, I want export buttons to include ALL data in the table, including what's filtered, including hidden columns.

It feels like all the articles I find are people leveraging AJAX and asking how they export data currently loaded + data that's unloaded. That is NOT my issue, I'm not using AJAX and the data is all encapsulated on the page. My initialization is fairly simple:

            var table = $('#table').DataTable({
                dom: 'Bfrtip',
                "aoColumnDefs": [
                    {
                        "aTargets": [0,1,2],
                        "bVisible": true,
                        "bSearchable": true
                    },
                    {
                        "aTargets": [3],
                        "bVisible": false,
                        "bSearchable": true
                    },
                    {
                        "aTargets": ['_all'],
                        "bVisible": false,
                        "bSearchable": false
                    }
                ],
                buttons: [
                    'csv', 'excelHtml5', 'pdf', 'print'
                ]
            });

My reading suggests TableTools would do this, but I don't have much interest in the deprecated and replaced tool. Can this be accomplished with Buttons? Hidden columns are included just fine, it's the currently filtered data I can't find a way to include.

Thanks!

This question has an accepted answers - jump to answer

Answers

  • kjtocoolkjtocool Posts: 4Questions: 1Answers: 0

    Still haven't been able to find an answer to this, appreciate any thoughts or pointers.

  • kthorngrenkthorngren Posts: 20,143Questions: 26Answers: 4,736
    Answer ✓

    You can use exportOptions to control what is exported.
    https://datatables.net/extensions/buttons/examples/print/columns

    Specifically you can use this export option to export all rows regardless of filtering:

                                exportOptions: {
                                  rows: {search: 'none'},
    

    Kevin

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    What Kevin says. Absolutely spot on :smile:.

    Allan

  • kjtocoolkjtocool Posts: 4Questions: 1Answers: 0

    Works like a charm, thank you! Still not sure where it's documented what all the options related to exportOptions are, maybe it's just the structure of documentation that's throwing me, but even now knowing the answer I'm not able to find it.

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    Structure of the documentation. The export options are basically defined by buttons.exportData(). That object is passed in from the exportOptions parameter of the excelHtml5 button type.

    Each part if technically correct, but they need to overlap more.

    Allan

This discussion has been closed.