Tabletools - print only selected rows

Tabletools - print only selected rows

MWillisMWillis Posts: 10Questions: 3Answers: 0

I'd like to be able to print only the currently selected rows, when clicking the tabletools print button. I see this has been asked before and there was some discussion about implementing it - https://www.datatables.net/forums/discussion/3755/tabletools-multirow-select-print-only-selected-rows - but I looked through the docs and couldn't find a way.

Anyone know if this has been added and I just missed it? If it hasn't been added, maybe there's a way I can run "before print" and "after print" functions that will handle the hide and show for me?

Answers

  • MWillisMWillis Posts: 10Questions: 3Answers: 0

    If anyone reads this in the future, I came up with a somewhat convoluted way to do it. I start with a hidden column that acts as a flag, noting whether the entire row should be hidden on print.

    I also hide the normal print button using CSS, and add one of my own with class ".printRows".

    When that button is clicked, I set the flag for all rows that aren't selected, and then trigger a click on the normal print button.

    Finally, I listen for escape keypress so I can unhide the rows when print is complete.

    jQuery.fn.dataTable.ext.search.push(
        function( settings, data, dataIndex ) {
            if ( data[window.flagColumnIndex] == "1" ) {
                return false;
            } else {
                return true;
            }
        }
    );
    
    window.dTable = jQuery(".datatable").DataTable({
        "columnDefs": [
            {
                "targets": [window.flagColumnIndex],
                "visible": false
            }
        ]
    });
    
    jQuery(document).on("click",".printRows",function(event){
        /* hide all rows that aren't selected, by setting the "hidden" flag column to "1"
        * our custom search filter knows that when this flag is "1", the row should be hidden
        * so re-drawing the table after setting the flag will hide those rows
        */
        window.dTable.rows(":not('.selected')").every( function() {
            window.dTable.cell( this.index(), window.flagColumnIndex ).data( "1" );
        });
        jQuery(".datatable tbody tr").removeClass("selected"); // remove the highlight
        window.dTable.draw();
        
        jQuery(".DTTT_button_print").click();
        
        jQuery(document).on("keyup.DTPrintComplete",function( event ){
            if ( event.which == 27 ) {
                jQuery(document).off("keyup.DTPrintComplete");
                // set the flag to "" and re-draw the table
                window.dTable.rows().every( function() {
                    window.dTable.cell( this.index(), window.flagColumnIndex ).data( "" );
                });
                window.dTable.draw();
            }
        });
    });
    
  • alanngalanng Posts: 2Questions: 0Answers: 0
    edited November 2015

    There is apparently new functionality to address this situation:

    https://datatables.net/extensions/buttons/examples/print/select.html

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75
    edited November 2015

    I wanted something similar, I wanted it to print the selected rows if any were selected, and if none were, print all rows, but only have one button.

    I ended up doing the following:

    $(document).ready(function() {
        $('#example').DataTable( {
            dom: 'Bfrtip',
            buttons: [
                {
                    extend: 'print',
                    autoPrint: false,
                    text: 'Print',
                    exportOptions: {
                      rows: function ( idx, data, node ) {
                        var dt = new $.fn.dataTable.Api( '#example' );
                        var selected = dt.rows( { selected: true } ).indexes().toArray();
                       
                        if( selected.length === 0 || $.inArray(idx, selected) !== -1)
                          return true;
    
                        return false;
                    }
                  }
                }
            ],
            select: true
        } );
    } );
    

    Demo here

    P.S. For some reason, I couldn't a grip on the API instance within the rows closure, I tried this, this.api, and a few other things, but it either threw an error saying that it didnt exist, or I couldnt call rows on null, so I ended up just executing new $.fn.dataTable.Api( '#example' );, which sucks, because I hate having to re-reference the CSS selector for the table. I tried new $.fn.dataTable.Api( this );, and while it didn't throw any errors, it didn't work (It would never see any rows as selected)

    Maybe @allan can tell me what I was doing wrong

This discussion has been closed.