PDF Button - Error on filter all (zero records)

PDF Button - Error on filter all (zero records)

rodriformigarodriformiga Posts: 38Questions: 8Answers: 0

Hello @allan

The pdf have a error on click and the datasource is empty.
In my case, I filtered the records on search field (wrote something to return zero records) and click in the button to export.
You can see this in the example: https://datatables.net/extensions/buttons/examples/initialisation/export.html

This question has an accepted answers - jump to answer

Answers

  • rodriformigarodriformiga Posts: 38Questions: 8Answers: 0

    someone?

  • tangerinetangerine Posts: 3,348Questions: 36Answers: 394

    This seems to apply to all the "export" Buttons; they don't recognize that there is no data.
    I suppose there may be circumstances in which someone might want to print an empty table, for example, to have a copy of the table structure. But is this intentional?

  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    I'd say that was a bug - I'll get it raised and linked to this topic.

    Cheers,

    Colin

  • kthorngrenkthorngren Posts: 20,276Questions: 26Answers: 4,765
    edited April 2018

    If you want to protect against exporting 0 records you can use something like drawCallback to check for the number of records using page.info() to determine the number of records. Then use button().disable() and button().enable() as desired.

    Kevin

  • allanallan Posts: 61,665Questions: 1Answers: 10,096 Site admin
    Answer ✓

    Thanks all! Fixed here and will be in the next release of Buttons which I think we should push out fair soon - its been a while!

    Allan

  • CharleyCharley Posts: 66Questions: 17Answers: 0
    edited August 2018

    Hmm... my comment is missing. Maybe I deleted it?

    I had someone who didn't want the buttons enabled at all if there was no data in the view (even though it didn't error out due to the bug fix above). I enable/disable the export buttons with the code below.

    var datatablesConfig = {
        "drawCallback": function( settings ) {
            var api = new $.fn.dataTable.Api( settings );
            var info = api.page.info();
            var showExportButtons = false;
            try{
                if(info.recordsDisplay > 0){
                    showExportButtons = true;
                }
            } catch (err){}
            if(showExportButtons){
                api.buttons( '.buttons-html5' ).enable()
            } else {
                api.buttons( '.buttons-html5' ).disable()
            }
        }
    
    };
    
    var table = $('#example').DataTable(datatablesConfig);
    
  • allanallan Posts: 61,665Questions: 1Answers: 10,096 Site admin

    Perfect - thanks for your reply. That looks like it could be a useful addition for others. Actually, I'm wondering if it should go into the core library...

    Allan

This discussion has been closed.