DataTables automatically set export title

DataTables automatically set export title

zs_canadazs_canada Posts: 1Questions: 1Answers: 0

I got the following .js file which is called on each page of my website and automatically transforms tables to DataTables:

$.extend( $.fn.dataTable.defaults, {
    "buttons": [
        $.extend( true, {}, buttonCommon, {
            extend: 'excelHtml5',
            exportOptions: {
                columns: ':visible'
            }
        } ),
        $.extend( true, {}, buttonCommon, {
            extend: 'pdfHtml5',
            orientation: 'landscape',
            exportOptions: {
                columns: ':visible'
            }
        } ),
        $.extend( true, {}, buttonCommon, {
            extend: 'print',
            exportOptions: {
                columns: ':visible'
            },
            orientation: 'landscape'
        } )
    ]
} );

//Render datatables
$(document).ready(function() {
    if ($('.data-table').length !== 0)
    {
        $('.data-table').DataTable();
    }
});

When I export a table (i.e. excelHtml5, pdfHtml5 or print), I want the exported document's title to display the table's data-exporttitle attribute if the table has such attribute, or else display a default value. Currently and by default, the title displays the page header title.

How can the above script be modified in order to achieve this?

If this info is of any help, some pages on my website contain more than one table.

My first reflex was to add the following "title" line of code in each extend function, but it doesn't work:

$.extend( true, {}, buttonCommon, {
    extend: 'pdfHtml5',
    orientation: 'landscape',
    title: $(this).data("export-title")
} ),

Answers

  • allanallan Posts: 61,853Questions: 1Answers: 10,134 Site admin

    Its the context that is causing the issue here. You could change $(this) to $('#myTable') and it would work for that table, but only for that table.

    I don't think there is a good way to have it work independently of the table at the moment looking at the code. title can be executed as a function, but it doesn't have scope set or parameters passed.

    Allan

This discussion has been closed.