How do I get elements of a column given a column name?

How do I get elements of a column given a column name?

taltal Posts: 5Questions: 3Answers: 0

I have multiple tables on one page, all of which use DataTables.
To each one, I want to add a callback function that says:

If the table has a column named "Values", go through each <td> in that column
If the value is below 10, make it red.
If the value is between 10 and 20, make it yellow.
If the value is between 20 and 30, make it green.

Changing colors is easy enough in css, as long as I can make the javascript add a class to the td.

Adding a callback function using drawCallback is pretty simple, and getting a reference to my table within the callback function is also simple, but that's where I get stuck.

How do I search for a column named "Values", and return a list of the <td>'s in that column, so I can add a class to each one based on the value?

Note: The column names are NOT defined using DataTables({}). The names of the columns are just what's in the <th> in <thead> of the table.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,743Questions: 1Answers: 10,111 Site admin
    Answer ✓
    let columnIndex = table
      .columns()
      .header()
      .map(c => $(c).text())
      .indexOf('Values');
    

    Is probably the easiest way to get the column index for given header text.

    Allan

  • taltal Posts: 5Questions: 3Answers: 0

    Perfect - thank you!

  • sichtschutzsichtschutz Posts: 1Questions: 0Answers: 0

    @allan

    Thank you for your help. :)

Sign In or Register to comment.