Paint rows in the table

2

I want to paint some rows of the table, is it possible to do this?

I mean to put a condition for it to be painted:

if(Object.values(totalDatos[j])[i].tipoUser=='verdadero'

    html += "<td if(Object.values(totalDatos[j])[i].tipoUser=='verdadero')
{style.background-color=yellow} >" + Object.values(totalDatos[j])[i] + "</td>";

example:

let condicion = true;


var html = "<table border=2 id='examples'>";
html += "<thead>";
html += "<tr><th colspan='3'>Validacion </th></tr>";
html += "<tr>";

html += "<th style='background-color=yellow;'> intento </th>";
html += "<th > Usuario </th>";
html += "<th > Resultado </th></tr> </thead>";

document.getElementById("tablaVariablesSVM").innerHTML = html;
<div id="tablaVariablesSVM"></div>
    
asked by hubman 22.11.2018 в 15:19
source

3 answers

3

Based on your if condition, rearrange the way you assign the information to the variable html .

Try this:

if(Object.values(totalDatos[j])[i].tipoUser=='verdadero')
{
    html += "<td style='background-color:yellow;'>" + Object.values(totalDatos[j])[i] + "</td>";
}

If you notice the condition can not go inside the html, so you have to take it up and when it is met, then arms the html. I suppose you're using javascript.

Updating

Keep this in mind:

html += "<td style='background-color:yellow;'>" + Object.values(totalDatos[j])[i].xxx + "</td>";

Where xxx is the property you want to show in the cell.

    
answered by 22.11.2018 / 15:32
source
1

If you use the bootstrap tables you can change the color by manipulating the classes:

if(Object.values(totalDatos[j])[i].tipoUser=='verdadero')
{
   $('#id_del_tr').addClass('table-warning');
}

You can also do it with th or td.

    
answered by 22.11.2018 в 17:40
1
let condicion = true;


var html = "<table class="table" border=2 id='examples'>";
html += "<thead>";
html += "<tr><th colspan='3'>Validacion </th></tr>";
html += "<tr>";
if(condicion){
   html += "<th class='table-warning'> intento </th>";
}else{
    html += "<th> intento </th>";
}

html += "<th > Usuario </th>";
html += "<th > Resultado </th></tr> </thead>";

document.getElementById("tablaVariablesSVM").innerHTML = html;

<div id="tablaVariablesSVM"></div>

Only if you use the bootstrap table.     

    
answered by 22.11.2018 в 17:55