how to create a switch case within a path in DATATABLES?

2

I'm painting the data brought from the database in datatables , the problem is that I need to validate a data to be able to paint a badge in the GUI , and I look for the way to do with a switch since if I use a if validation is very extensive.

CODE

var rowNode = db_consultar_empleados
            .row.add([
                        resultado[i].emple_id,                          
                        resultado[i].emple_cedula,
                        resultado[i].emple_nombre,
                        resultado[i].emple_apellido,
                        (resultado[i].porcentaje == '80' && resultado[i].resultado_lider <= '25' || resultado[i].porcentaje == '100' && resultado[i].resultado_lider <= '60' ?
                        "<span class='badge' style='background: red'>" + resultado[i].resultado_lider + "%</span>"  :

                            (resultado[i].porcentaje == '80' && resultado[i].resultado_lider >= '26' && resultado[i].resultado_lider <= '45' || resultado[i].porcentaje == '100' && resultado[i].resultado_lider >= '61' && resultado[i].resultado_lider <= '89' ?
                            "<span class='badge' style='background: orange'>" + resultado[i].resultado_lider + "%</span>"  :
                            "<span class='badge' style='background: green'>" + resultado[i].resultado_lider + "%</span>" )),


                        resultado[i].detalle_per_id_area_empleado,
                        resultado[i].deteva_id
                    ])
                .draw()
            .node();

Here it works validating with a IF but the idea is to be able to do within that path say:

What if in the case that the 'variable' resultado[i].area is igual a 1 validate if the porcentaje es 80 y el resultado es <= 25 or if porcentaje es 100 y el resultado <= 60 that paint a badge de color rojo and likewise validate the same but in case resultado[i].area igual a 2 .

    
asked by JDavid 26.04.2017 в 16:21
source

1 answer

1

Because your code that you show is within for and you are rendering information in the table you can not do the switch where you now have the% linear if , it is best to do it out, like this:

for(let i = 0; i < resultado.length; i++){
  var res = "";
  switch (resultado[i].area) {
    case 1:
      if (validacion correspondiente) {
        res = "<span class='badge' style='background: red'>" + resultado[i].resultado_lider + "%</span>"
      }
      break;

    case 2:
      if (validacion correspondiente) {
        //...
      }
      break;
  }

  var rowNode = db_consultar_empleados
    .row.add([
      resultado[i].emple_id,
      resultado[i].emple_cedula,
      resultado[i].emple_nombre,
      resultado[i].emple_apellido,

      res,//aqui no se puede aplicar el switch debido a que se renderiza la informacion y genera errores

      resultado[i].detalle_per_id_area_empleado,
      resultado[i].deteva_id
    ])
    .draw()
    .node();
}
    
answered by 26.04.2017 / 17:47
source