Print Button, row restriction before print

Print Button, row restriction before print

spiderkznspiderkzn Posts: 44Questions: 12Answers: 0

Hi,

Please can you help with guidance.

As I have print button on datatables which when clicked it will go to new screen to direct to print option.

So, can it possible that I would like restrict it up to 1000 rows to allow print otherwise it will have message that row need to be less or equal to 1000 to order to print?

As there is action: function (e, dt, node, config) {

Help with if statement.
}

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    You are already 90% of the way there. Combine what you know (needing an if statement in the action) with the second example from the buttons.buttons.action documentation, and a call to alert() for the warning:

    action: function (e, dt, node, config, cb) {
      if (dt.rows({selected: true}).count() <= 1000) {
        DataTable.ext.buttons.csvHtml5.print.call(this, e, dt, node, config, cb);
      }
      else {
        alert('...');
      }
    }
    

    Assuming you wish to base it on the rows selected of course. You might want something else. You will probably also need to add a check to make sure at least one row is selected.

    Allan

  • spiderkznspiderkzn Posts: 44Questions: 12Answers: 0

    Thank you for it. It work. But other thing for

    DataTable.ext.buttons.csvHtml5.print.call(this, e, dt, node, config, cb);

    It mention DataTable is not found. as I'm using

    $("#tablename").DataTable({....

    How do I change DataTable?

  • spiderkznspiderkzn Posts: 44Questions: 12Answers: 0

    by the way the error is:

    TypeError: Cannot read properties of undefined (reading 'call')

  • spiderkznspiderkzn Posts: 44Questions: 12Answers: 0

    Look like I've made it to work.

    So the printing. How do I make it to print in new tab and printer option instead of csv/pdf etc?

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Sorry - it should have been:

    DataTable.ext.buttons.print.call(
    

    The print view should open in a new tab by default.

    Allan

  • spiderkznspiderkzn Posts: 44Questions: 12Answers: 0

    Hi Allan,

    So it didn't work. But I've make other way

    $.fn.dataTable.ext.buttons.print.call(this, e, dt, config, cb,);

    So there is error:

    TypeError: $.fn.dataTable.ext.buttons.print.call is not a function

    By the way I'm using TypeScript not Javascript.

    How do I resolve this

  • spiderkznspiderkzn Posts: 44Questions: 12Answers: 0

    When I try this:

    $.fn.dataTable.ext.buttons.print.action.call(this, e, dt, config, cb,);

    Error:

    TypeError: Cannot read properties of undefined (reading 'columns')

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    https://live.datatables.net/zafifuna/1/edit

    Needs the action function that is called. It should also extend from print so that it inherits the properties needed for that action.

    Allan

  • spiderkznspiderkzn Posts: 44Questions: 12Answers: 0

    Hi allan,

    I've working and use this one https://datatables.net/forums/discussion/33209/run-script-before-exporting-begins-buttons-plugin

    So I can able to make it work but there is error:

    Exception Unhandled

    TypeError: r is not a function

    When I click continue. it does print fine. as is there is way to prevent this error?

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Can you update my test case to show the issue please? Or link to the actual page you are working on so I can debug it.

    Allan

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    The error might relate to the missing fifth parameter in the example you linked to, but you will see it in the example I provided above. The fifth (cb) parameter was added in newer versions of Buttons.

    Allan

  • spiderkznspiderkzn Posts: 44Questions: 12Answers: 0
    edited August 30

    action: function (
    e,
    dt,
    button,
    config,
    ) {
    if (dt.rows().count() <= 1000) {
    $.fn.dataTable.ext.buttons.print.action.call(
    this,
    e,
    dt,
    button,
    config,
    );
    } else {
    alert("The record contain more than 1000. Please use the dropdown boxes to reduce the number of display records before printing");
    }
    },

    Ok let me try add cb

  • spiderkznspiderkzn Posts: 44Questions: 12Answers: 0

    the cb has solved the problem. thank you allan!

    action: function (
    e,
    dt,
    button,
    config,
    cb
    ) {
    if (dt.rows().count() <= 1000) {
    $.fn.dataTable.ext.buttons.print.action.call(
    this,
    e,
    dt,
    button,
    config,
    cb
    );
    } else {
    alert("The record contain more than 1000. Please use the dropdown boxes to reduce the number of display records before printing");
    }
    },

  • spiderkznspiderkzn Posts: 44Questions: 12Answers: 0

    other help please.

    When the else appear with alert.

    After ok. the print button showing the loading icon.

    How to make loading icon disappear from print button?

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    Check the browser's console for errors.

    Update Allan's test case to chow the issue so we can help debug:
    https://live.datatables.net/zafifuna/1/edit

    Kevin

  • spiderkznspiderkzn Posts: 44Questions: 12Answers: 0

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    I copied your code into this test case:
    https://live.datatables.net/zafifuna/2/edit

    Its easier for us to help you if you provide running test cases replicating your issues.

    You are using buttons.buttons.extend to make the button a print button. This doesn't work and causes the issue you are seeing because now its both a print button and custom button. The Custom button required parameters docs state that buttons.buttons.text and buttons.buttons.action are required. I changed extend to text in the updated test case and now it works.

    Kevin

  • spiderkznspiderkzn Posts: 44Questions: 12Answers: 0

    Kevin,

    I cannot find a solution but I've found a way to remove spinner icon on print.

    ...
    ...
    ...
    else {
    alert("The record contain more than 1000. Please use the dropdown boxes to reduce the number of display records before printing");

    $("[class^='btn btn-secondary buttons-print']").removeClass("processing");

    }
    ...

    Maybe you may need add that for next update?

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    If you can provide me with a link to a test case showing the issue, as Kevin asked for, and it shows an issue that needs a fix for the next release, I'll make that change.

    Allan

  • spiderkznspiderkzn Posts: 44Questions: 12Answers: 0

    Hi allan.

    See here https://live.datatables.net/zafifuna/3/edit

    If you click print the spinner icon will running.

Sign In or Register to comment.