[autoPrint: false] -- add print button to exported data page

[autoPrint: false] -- add print button to exported data page

rldean1rldean1 Posts: 141Questions: 66Answers: 1

I was able to get DT and the 'print' & 'button' extensions to generate a printout. I turned off autoPrint.

Is there a way for me to customize the output, and add a print button that invokes the browser's print functionality? Like, is this a built-in option I could toggle somewhere in the docs? If not, how to I customize the HTML/JS in the output?

                        $("#tblJobSpecs").DataTable({
                            dom: 'Bftipr',
                            responsive: true,
                            pageLength: 25,
                            lengthChange: false,
                            data: jspecs.ary,
                            columns: [
                                { data: 'PosTitle', title: 'Position Title' },
                                { data: 'PosCode', title: 'Code' },
                                { data: 'Grade', title: 'Grade' },
                                { data: 'NewGrade', title: 'FY 19 Grade' },
                                { data: 'NewSchedule', title: 'FY 19 Schedule' }
                            ],
                            buttons: [
                                {
                                    extend: 'print',
                                    className: 'btn-primary',
                                    text: 'View/print full listing',
                                    autoPrint: false,
                                    title: 'Unified Schedule FY 19 Conversion',
                                    init: function(api, node, config) {
                                        $(node).removeClass('btn-secondary')
                                     }
                                }
                            ],

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    The customize callback that is described in the print documentation can be used to customise the document to print. If you then want to auto call the browser's print functionality, just remove the autoPrint: false line.

    Allan

  • rldean1rldean1 Posts: 141Questions: 66Answers: 1

    @allan So I was able to add a button on the output; however, I can't figure out how to get window.print() to work from within the output... In my example, if I click the PRINT button, it'll print the main datatable, not the exported table.

    How do I attache a click listener to the exported document?

                                        customize: function (win) {
                                           
                                            $(win.document.body).prepend('<button id="print" type="button" class="btn btn-secondary">PRINT</button>');
                                            $(win.document.body).on('click', '#print', function (){
                                                console.log('registered click');
                                                window.print();
                                            }); 
    
  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin
    Answer ✓

    Oh I see - you want to trigger printing from a button inside the print view - I hadn't understood that, sorry.

    You need to use win.print() since window is the host window. win is the context for the print view window.

    Allan

This discussion has been closed.