I have a basic table with values that I am trying to control the formatting. I have achieved this in this example http://jsfiddle.net/hVJ4b/ for KPI2 of Service 1.
$('#work_table td.r2c1').each(function(){
var test = $(this).html(); // this works
var target = $('#work_table td.r2c5').html()
var target2 = $('#work_table td.r2c5').html() - 5;
//alert("test: " + test);
//alert("target: " + target);
//alert("target2: " + target2);
if ($(this).html() >= target) { //if >= to target then green
//alert($(this).value())
$(this).css('background-color',green);
} else if ($(this).html() < target && $(this).html() >= target2 ) { //if < to target then orange
$(this).css('background-color',orange);
} else { //if < to target then red
$(this).css('background-color',red);
}
});
$('#work_table td.r2c2').each(function(){
var test = $(this).html(); // this works
var target = $('#work_table td.r2c5').html()
var target2 = $('#work_table td.r2c5').html() - 5;
//alert("test: " + test);
//alert("target: " + target);
//alert("target2: " + target2);
if ($(this).html() >= target) { //if >= to target then green
//alert($(this).value())
$(this).css('background-color',green);
} else if ($(this).html() < target && $(this).html() >= target2 ) { //if < to target then orange
$(this).css('background-color',orange);
} else { //if < to target then red
$(this).css('background-color',red);
}
});
$('#work_table td.r2c3').each(function(){
var test = $(this).html(); // this works
var target = $('#work_table td.r2c5').html()
var target2 = $('#work_table td.r2c5').html() - 5;
//alert("test: " + test);
//alert("target: " + target);
//alert("target2: " + target2);
if ($(this).html() >= target) { //if >= to target then green
//alert($(this).value())
$(this).css('background-color',green);
} else if ($(this).html() < target && $(this).html() >= target2 ) { //if < to target then orange
$(this).css('background-color',orange);
} else { //if < to target then red
$(this).css('background-color',red);
}
});
$('#work_table td.r2c4').each(function(){
var test = $(this).html(); // this works
var target = $('#work_table td.r2c5').html()
var target2 = $('#work_table td.r2c5').html() - 5;
//alert("test: " + test);
//alert("target: " + target);
//alert("target2: " + target2);
if ($(this).html() >= target) { //if >= to target then green
//alert($(this).value())
$(this).css('background-color',green);
} else if ($(this).html() < target && $(this).html() >= target2 ) { //if < to target then orange
$(this).css('background-color',orange);
} else { //if < to target then red
$(this).css('background-color',red);
}
});
Now I can just easily copy and paste my JQuery to apply it to the other rows/KPIs. But i am looking for a better way of doing this without copying and pasting, as I don't think this is good practice.
Can anyone offer advise on this please?
Note: My initial approach to this could be completely wrong. I also want it to be flexible for any changes/additions to the table at a later date...
