I have a table with the following structure, which is populated with json data that I get from my controller.
<div class="table-responsive table-condensed table-sm table-bordered tabla">
<table class="table table-hover content-tabla" id="Datos" cellpadding="0" cellspacing="0">
<thead>
<tr class="table-success">
<td>Código</td>
<td>Nombre</td>
<td>Pronosticado</td>
<td></td>
<td>Pronosticado Acumulado</td>
<td>Entregado</td>
<td>Diferencia</td>
<td></td>
<td>Pronosticado Actual</td>
<td>Entregado</td>
<td>Diferencia</td>
<td>Activar</td>
</tr>
</thead>
<tbody>
<tr></tr>
</tbody>
</table>
</div>
Now what I need is to apply a background with jquery to the columns called Difference but according to the values obtained.
This is just an example of what I want to achieve:
if(micolumna=='100%')
{
background-color=black;
color:red;
}
So I fill my table
<script src="~/js/Jquery/jquery-3.1.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
datos();
});
function datos() {
$.getJSON("/MyController/Index", function (data) {
$.each(data, function (idx, opt) {
$('#Datos').append('<tr><td>' + opt.Codigo + '</td><td>' + opt.Nombre + '</td><td>' + opt.Pronostico + '</td><td>' + '' + '</td><td>' + opt.PronosticoAA + '</td><td>' + opt.Entregado + '</td><td>' + opt.DiferenciaAA + '</td><td>' + '' + '</td><td>' + opt.PronosticoSA + '</td><td>' + opt.EntregadoSA + '</td><td>' + opt.DiferenciaSA + '</td><td><input type="checkbox" name="cod_A" id="cod_A" value="A" /></td></tr>');
});
}, 'json');
}
</script>
You can ask for the value of "Difference" before creating the column and then concatenate it to the HTML, something like this:
Luck!
What you can do is when you are creating each row, check for the value of the difference variable and apply a class if it meets your condition. Something like:
I suggest several ways to accomplish the task.
If you don't want to change the way you generate the table, you can make the following call to change the style afterwards:
Another way to do it is through fields
data-*
, adding the data to an attribute (for exampledata-pronostico
) and not to its content:Now we can configure the styles with pure CSS without the need for javascript: