How to select cells by class in export formatting

How to select cells by class in export formatting

mtiltonmtilton Posts: 5Questions: 2Answers: 0

I'm trying to format certain cells during export depending on if they have certain class, e.g., <td class="link">. This page https://datatables.net/extensions/buttons/examples/html5/outputFormat-function.html shows a good example of how to format export data based on column number. I can do this, and it works, but because columns may be rearranged it risks breaking in the future. Is there anything similar that can be done by class rather than column index?
Thanks for any help.
P.S., Allan, can you delete the post I sent about ten minutes ago? I was trying to ask this question, but it sent before I'd typed in more than a few words.

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,143Questions: 1Answers: 2,586
    Answer ✓

    You've got the column and row index passed into the body function, so you can use cell().node() to get the cell, and from there you can check for the classes. That column index would be consistent should it be moved.

    Colin

    p.s. I removed that other thread, so all done.

  • mtiltonmtilton Posts: 5Questions: 2Answers: 0

    Thanks for the fast reply. I didn't get to this today but will do so next week, and I'll mark this as the answer accordingly. Appreciate your help on both counts.

  • mtiltonmtilton Posts: 5Questions: 2Answers: 0

    In case anyone else is interested in this, the syntax is as follows:

    ($(myTable.cell(row, col).node()).hasClass('myClass'))
    

    Just FYI, this may not work as planned if you are hiding some columns. One issue I ran into happened because myTable filters columns for export and excludes those with another class, "noExport":

    exportOptions :
    {
    // export all visible columns except those of class noExport
    columns: ':not(.noExport):visible',
    rows: ':visible',

    As a result, the "hasClass" selector above actually returns the index of a different column than the one that is set to "myClass." E.g., say column 13 has class "myClass" in the original table, but Column 13 is only Column 11 in the export of visible columns. Any attempt to modify data based on myClass will grab column 13 in the export and will modify the wrong data. I'm not sure if there's a way around this.

    It's still good to know how to select data using cell(row, col).node(), though.

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

    If you could create a test case, we're happy to take a look. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • kthorngrenkthorngren Posts: 20,302Questions: 26Answers: 4,769

    Maybe you need to use column().index() and pass the string 'visible' to get the visible index.

    Kevin

This discussion has been closed.