Function to exclude columns from print

Function to exclude columns from print

darrenmdarrenm Posts: 29Questions: 11Answers: 0

The client wants printed output that only includes the currently visible columns, which I can do with ':visible'. However they also want to exclude certain columns, irrespective of their visibility. So I need a way to exclude the columns they don't want, whilst selecting visible columns that aren't in the exclude list.

I think the only way I can do this is with a function as a column-selector. However I can't get it to work, just testing with the following:

                    {
                        extend: "print",
                        text: "Print - Results",
                        exportOptions: {
                            function(idx, data, node) {
                                return false;
                            }
                        }
                    }

As far as I can tell, the function doesn't execute when I click the print button, and all columns appear in the output.

I've tried to do a test case of this but I get a script error when I include the buttons plugin:

http://live.datatables.net/lekoduwu/1/edit?html,js,console,output

Can you either tell me what I'm doing wrong in the demo, or let me know if what I'm trying to do with the Print button is possible?

Cheers.

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,237Questions: 1Answers: 2,598
    Answer ✓

    You could use the columns property of the exportOptions - see example here. columns is column-selector, which can be a function.

    Your demo isn't working as you haven't included all the necessary source files.

    Colin

  • darrenmdarrenm Posts: 29Questions: 11Answers: 0

    I've not been able to get this demo working. I could really use some help. It will display the print button, but when I click it I get "Script error. (line 0)".

    http://live.datatables.net/rokohove/1/

    I used your download builder to create the script tags. I can't think what else would be missing. It is using the most basic implementation of the print button.

    Re. my original question, now that I have corrected my misunderstanding on how to implement the column selector function, I have been able to get it working, however when I use the function I loose all CSS formatting in the printed output. If I switch back to columns: ":visible" the CSS styles are reflected in the printed output.

    This works, it excludes the columns I don't want, but the CSS is gone:

                        {
                            extend: "print",
                            text: "Print - Results",
                            exportOptions: {
                                columns: function (idx, data, node) {
                                    if (node.innerHTML == "Extras")
                                        return false;
                                    return true;
                                }
                        },
    

    This shows all visible columns and the CSS styling is as required:

                        {
                            extend: "print",
                            text: "Print - Results",
                            exportOptions: {
                                columns: ":visible"
                             }
                        },
    

    I'd be happy to try and recreate it in a test case if you can help me out in getting that simple print button to work.

  • darrenmdarrenm Posts: 29Questions: 11Answers: 0

    Hmm OK, so if I follow my share link, the demo works, but in edit mode it does not. Tricky....

  • darrenmdarrenm Posts: 29Questions: 11Answers: 0
  • darrenmdarrenm Posts: 29Questions: 11Answers: 0

    Ok I got it working by toggling between the edit window to make changes and the direct link I originally posted to view them. And of course it works fine, the <th> background colour is coming through in the print version. So back to square one...

  • darrenmdarrenm Posts: 29Questions: 11Answers: 0

    Just ignore this. I don't know what's going on with it, maybe I have a browser issue. It's working fine now and I haven't changed anything.

This discussion has been closed.