When loading data from a table by going through array, enter a validation

0

Good and I tried to do that when I'm painting the data of a table and the condition is fulfilled instead of painting a data I put a label or a label-success so that when it is green or another color can access the , I have done it with html and php but now I want to do it differently. Mark me error in the if

Code

   for(var i=0; i<resp.length; i++)
    {
      console.log(resp[i]);

  html+= '<tr><td>'+resp[i].fecha_registro+
        '</td><td>'+resp[i].dias_+
        '</td><td>'+resp[i].contacto_nombre+
        '</td><td>'+resp[i].modelo+
        '</td><td class="center"><span class="center' if(resp[i].servicio==0){'label-success'}else{'label-warning'}'"></span>'
    +resp[i].servicio+
        '</td><td>'+resp[i].ventas+
        '</td></tr>';
    }
    
asked by Juan Jose 10.10.2018 в 02:14
source

1 answer

1

The error that I see is a concatenation that you try to make in the if I recommend you change it this way:

 var labelType = (resp[i].servicio == 0) ? 'label-success' : 'label-warning'; 
 html+= '<tr><td>'+resp[i].fecha_registro+
    '</td><td>'+resp[i].dias_+
    '</td><td>'+resp[i].contacto_nombre+
    '</td><td>'+resp[i].modelo+
    '</td><td class="center"><span class="label ' + labelType + '"></span>'
    +resp[i].servicio+
    '</td><td>'+resp[i].ventas+
    '</td></tr>';

It's simpler if you use the ternary operator, which is basically an if compressed into a single line, and then you concatenate it to your text as any string. For more information on the Czech ternary operator How do you use the? : (conditional) operator in JavaScript?

    
answered by 10.10.2018 / 06:32
source