Button Print cancel print on empty datatable?

Button Print cancel print on empty datatable?

jvcunhajvcunha Posts: 81Questions: 10Answers: 1

Hi,
it's possible cancel printing if empty datatable?
Thanks

Replies

  • allanallan Posts: 63,819Questions: 1Answers: 10,517 Site admin

    Do you mean as the end user? If so, just press esc when the print dialogue shows up and you then decide you don't want to print it.

    Allan

  • jvcunhajvcunha Posts: 81Questions: 10Answers: 1

    Sorry Allan, did not explain right.
    When the datatable is empty, you should not print or export to Excel.
    It is to disable buttons or intercepting the click and abort if it is empty?

  • allanallan Posts: 63,819Questions: 1Answers: 10,517 Site admin

    Yes, what you could do is use the draw event in combination with the page.info() method to see if the table has any rows or not, then use buttons().disable() / buttons().enable() to disable or enable the buttons as required.

    Allan

  • jvcunhajvcunha Posts: 81Questions: 10Answers: 1

    Thanks Allan, resolved in initComplete:

    var info = table.page.info();
    tot = info.recordsTotal;
    
    if (tot > 0) {
        table.buttons().enable();
    } else {
        table.buttons().disable();
    }
    
  • allanallan Posts: 63,819Questions: 1Answers: 10,517 Site admin

    The only thing I would say about using initComplete is that it only runs once in the table's life cycle. If you were to use the API in any way to add new rows, then it wouldn't see the updates and enable the buttons again. Likewise, if there were records and then your user filtered them down to zero, that also wouldn't reflect in the buttons.

    The draw event I mentioned would address that.

    Allan

This discussion has been closed.