I am trying to modify code I found here:
conditional formatting of html table cells
I have two different sets of formatting rules. There will be 37 columns in the final table. Most will be formatted with rule 1, about 10 will be formatted by rule 2.
Here is a jsfiddle with my work so far:
http://jsfiddle.net/8ed89exs/3/
here is the code:
html
<table>
<thead>
<tr>
<th>Name</th>
<th>0.1</th>
<th>0.2</th>
<th>0.3</th>
</tr>
</thead>
<tbody>
<tr>
<td>Allan, Paul</td>
<td>-9</td>
<td>-9</td>
<td>-9</td>
</tr>
<tr>
<td>Bartlett, James</td>
<td>-5</td>
<td>-5</td>
<td>-5</td>
</tr>
<tr>
<td>Callow, Simon</td>
<td>-2</td>
<td>-2</td>
<td>-2</td>
</tr>
<tr>
<td>Dennis, Mark</td>
<td>-1</td>
<td>-1</td>
<td>-1</td>
</tr>
<tr>
<td>Ennals, Simon</td>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>Finnegan, Seamus</td>
<td>2</td>
<td>2</td>
<td>2</td>
</tr>
<tr>
<td>Goldberg, John</td>
<td>9</td>
<td>9</td>
<td>9</td>
</tr>
</tbody>
css
table {
width: 13em;
}
th {
border-bottom: 2px solid #ccc;
padding: 0.1em 0 0.0em 0;
font-size: .9em;
}
td {
border-bottom: 0px solid #ccc;
padding: 0.1em 0 0.1em 0;
text-align: center;
}
.abovePlus {
background-color: #1b7837; color: #fff;
}
.above {
background-color: #7fbf7b; color: #000;
}
.high {
background-color: #d9f0d3; color: #000;
}
.at {
background-color: #ffffff; color: #000;
}
.low {
background-color: #e7d4e8; color: #000;
}
.below {
background-color: #af8dc3; color: #000;
}
.belowPlus {
background-color: #762a83; color: #fff;
}
js
$(function() {
$( "td" ).each(function(index) {
var scale = [['abovePlus', 20, 20], ['above', 5, 6], ['high', 2, 1], ['at', 0, 0], ['low', -1, -2], ['below', -2, -4], ['belowPlus', -9, -9]];
var score = $(this).text();
var column = [[0, 1], [1, 1], [2, 2], [3, 1]];
for (var i = 0; i < scale.length; i++)
{
if (score <= scale[i][1]) {
$(this).addClass(scale[i][0]);
}
}
});
});
As shown above all columns are formatted by rule one. If I change:
if (score <= scale[i][1]) {
to
if (score <= scale[i][2]) {
all columns will be formatted by rule 2.
I need to be able to have some columns formatted by rule 1 others by rule 2. The column array will define which columns will be formatted by which rules (in the final product there will be 37 columns).
I am trying to do this with a variable:
var x = column[i][1];
and then change
if (score <= scale[i][1]) {
to
if (score <= scale[i][x]) {
However when I place the
var x = column[i][1];
into the loop block the code fails to format.
I have limited experience with js so I am wondering if I've made a syntax error.
The code am trying to get to work is:
$(function() {
$( "td" ).each(function(index) {
var scale = [['abovePlus', 20, 20], ['above', 5, 6], ['high', 2, 1], ['at', 0, 0], ['low', -1, -2], ['below', -2, -4], ['belowPlus', -9, -9]];
var score = $(this).text();
var column = [[0, 1], [1, 1], [2, 2], [3, 1]];
for (var i = 0; i < scale.length; i++)
{
var x = column[i][1];
if (score <= scale[i][x]) {
$(this).addClass(scale[i][0]);
}
}
});
});
Thank you for any help.