This is a portion of my table:
index.php
<tr>
<th>data_uwr</th>
<td>-1.413\-5.113</td>
<td>-3.314\-3.650</td>
<td>-0.001\-0.010</td>
<td>-1.052\-1.052</td>
<td>-1.052\-1.052</td>
</tr>
As you can see, each of the cells has 2 numbers separated by a backslash. Can I somehow apply different colors to each of these 2 numbers? For example, in first <td>
, -1.413 would be green and -5.113 would be red.
This is what I tried to do but it did not work:
index.js
// This is how I get the row. What this code does is it colors the entire cell - everything in the cells becomes green.
mainTable.rows[rowNumber].cells[numOfRunsToCompute].style.color = 'green';
// Then I tried splitting cell content into an array (of strings), and coloring each string - each number became a string
let test = mainTable.rows[rowNumber].cells[numOfRunsToCompute].firstChild.textContent.split('\\');
test[0].fontcolor('green');
I used this fontcolor() method but it did not work. Since String is not a DOM element, I am not sure how I can color it.
Edit: My question is not duplicate of this. I do not intend to use RegExp mainly because it is very hard to define a pattern in my case. There are thousands of numbers that I cannot predict. The colorization is done based on formulas (which I did not include here for simplicity).
Edit 2:
This is a snippet of code from my function (you may think that some variables are not declared etc., but note that this a cut-out snippet for simplicity purposes):
for (let numOfRunsToCompute = 2; numOfRunsToCompute < runs; numOfRunsToCompute++) {
let baseline = mainTable.rows[rowNumber].cells[1].firstChild.textContent;
let newCellValue = mainTable.rows[rowNumber].cells[numOfRunsToCompute].firstChild.textContent;
let splitBaseline = baseline.split('\\');
let splitNewCellValue = newCellValue.split('\\');
for (let i = 0; i < splitBaseline.length; i++) {
let numBaseline = Math.abs(splitBaseline[i]);
let numNewCellValue = Math.abs(splitNewCellValue[i]);
if (numBaseline >= flagNum) {
let greenCell = numBaseline * greenNum;
let orangeCell = numBaseline * orangeNum;
if (numNewCellValue >= 0 && numNewCellValue < greenCell) {
// For example, this is where I need to apply those colors
mainTable.rows[rowNumber].cells[numOfRunsToCompute].style.color = 'green';
} else if (numNewCellValue >= greenCell && numNewCellValue <= orangeCell) {
mainTable.rows[rowNumber].cells[numOfRunsToCompute].style.color = 'orange';
} else {
mainTable.rows[rowNumber].cells[numOfRunsToCompute].style.color = 'red';
}
} else {
let greenCell = numBaseline + greenOrangeWorse;
let orangeCell = greenCell + greenOrangeWorse;
if (numNewCellValue >= 0 && numNewCellValue < greenCell) {
mainTable.rows[rowNumber].cells[numOfRunsToCompute].style.color = 'green';
} else if (numNewCellValue >= greenCell && numNewCellValue < orangeCell) {
mainTable.rows[rowNumber].cells[numOfRunsToCompute].style.color = 'orange';
} else {
mainTable.rows[rowNumber].cells[numOfRunsToCompute].style.color = 'red';
}
}
}
}
To put it simply: what this code does is basically some pre-formatting and essentially determining which color to give a particular cell. But in this specific case, I need to color a particular piece of information in the cell, not entire cell.