How to use `cell().index()` to get header text from selected column

How to use `cell().index()` to get header text from selected column

rdmrdm Posts: 194Questions: 55Answers: 4
edited September 2018 in DataTables 1.10

I'm looking at cell().index() and while I am able to get the correct row and column numbers when clicking a cell in the table, I was wondering if I could instead get the column header text of the selected row.

So if I have this:

var table = $(".histogram").DataTable();
            $(".histogram tbody").on("click", "td", function () {
                console.log('column: ' + table.cell(this).index().column);
                console.log('row: ' + table.cell(this).index().row);
            });

how would I need to tweak the code, so that instead of

   column: 2
   row: 1

i get the text-equivalents

   column: Program
   row(the column-0 value of that row) : 17-18

This would be useful so that I could filter another table on the page based on what a person clicks in this table, using parameters that make sense to the database function.

Answers

  • rdmrdm Posts: 194Questions: 55Answers: 4

    I figured out the "column text" portion of the code.

    var col = table.cell(this).index().column+1;
    var colText = $(".histogram thead th:nth-child("+col+")").text();
    
This discussion has been closed.