user8012596
user8012596

Reputation: 854

Datatable pdf customize not picking dynamic value into title

I have the following datatable which I send some dynamic value into its title. But it doesnt seem to be picking them. Also the title cause my file name to change too. How to enable the dynamic value are passed correctly into the pdf. Here is how I am doing it title: 'Vehicle ID:'+$("#vehicleID").val()+'\nDate Start:', But it just appear as Vehicle IDDate Start at the top. How to also add footer and page numbering?

var oTable = $('#dashboardGrid').dataTable({
                    "order": [[ 1, "asc" ]],
                    "aoColumnDefs": [ { "bSortable": false, "aTargets": [ 0,1 ] }],
                        "processing": true,
                    "serverSide": true,
              "ajax": {
                        "url": "getData.php",
                        "type": "POST",
                        "data": function(d) {
                            d.vID = $("#vehicleID").val()
                            d.startDateTime = startDateTimeFinal
                            d.endDateTime = endDateTimeFinal
                         }                

                    },
                    dom: 'Bfrtip',
                    lengthMenu: [
            [ 10, 25, 50, -1 ],
            [ '10 rows', '25 rows', '50 rows', 'Show all' ]
        ],
        buttons: [
           'pageLength', 'copy', 'csv', 'excel', 'pdf', 'print',
           {
           extend: 'pdf',
                        title: 'Vehicle ID:'+$("#vehicleID").val()+'\nDate Start:',
                        text: 'Pdf',
                        orientation: 'landscape',
                        pageSize: 'A4',
                        exportOptions: {
                        columns: [ 0, 1, 2, 3, 5 ]
                        },
                        customize: function ( doc ) {
                        doc.content[1].table.widths = [
                        '5%',
                        '30%',
                        '30%',
                        '10%',
                        '10%'

                        ]
                        }
           }

Here is my full list of .js included.

   <script src="dt/datatables.net/js/jquery.dataTables.min.js"></script>
    <script src="dt/datatables.net-bs/js/dataTables.bootstrap.min.js"></script>
    <script src="dt/datatables.net-buttons/js/dataTables.buttons.min.js"></script>
    <script src="dt/datatables.net-buttons-bs/js/buttons.bootstrap.min.js"></script>
    <script src="dt/datatables.net-buttons/js/buttons.flash.min.js"></script>
    <script src="dt/datatables.net-buttons/js/buttons.html5.min.js"></script>
    <script src="dt/datatables.net-buttons/js/buttons.print.min.js"></script>
    <script src="dt/datatables.net-fixedheader/js/dataTables.fixedHeader.min.js"></script>
    <script src="dt/datatables.net-keytable/js/dataTables.keyTable.min.js"></script>
    <script src="dt/datatables.net-responsive/js/dataTables.responsive.min.js"></script>
    <script src="dt/datatables.net-responsive-bs/js/responsive.bootstrap.js"></script>
    <script src="dt/datatables.net-scroller/js/dataTables.scroller.min.js"></script>
    <script src="dt/datatable-plugin/fnReloadAjax.js"></script>
    <script src="dt/jszip/dist/jszip.min.js"></script>
    <script src="dt/pdfmake/build/pdfmake.min.js"></script>
    <script src="dt/pdfmake/build/vfs_fonts.js"></script>

Here is my temporary link http:///testReport1.php

Upvotes: 1

Views: 4674

Answers (1)

davidkonrad
davidkonrad

Reputation: 85528

Buttons seem now to recognize function attributes. At least with version 1.3.1 as I refer to below. Once a little hack was needed in order to set the filename or title dynamically. This works with 1.10.15 and buttons 1.3.1 :

$('#example').DataTable( {
  dom: 'Bfrtip',
  buttons: [{
    extend: 'pdfHtml5',
    filename: function() {
      return $('#filename').val()      
    },
    title: function() {
      return $('#message').val()
    }
  }]
});

demo -> https://jsfiddle.net/2nwqa2jk/

If you just provide title: 'Vehicle ID:'+$("#vehicleID").val()+'\nDate Start:' then this expression is evaluated once upon initialisation. If you pass a function this is executed when the PDF is generated.

Upvotes: 3

Related Questions