How to make validations in datatables?

1

I have found out on the official page of Data Tables how to do validations, but it seems very complex to me as for what I am doing.
The validation that I require is a bit similar to that done with PHP but in this case it is in a for cycle in JS handling the Data Tables .
THE CODE:

 var table_show = $('#table_students').DataTable({
            "language": {                
                            "sProcessing":     "Procesando...",
                            "sLengthMenu":     "Mostrar _MENU_ registros",
                            "sZeroRecords":    "",
                            "sEmptyTable":     "",
                            "sInfo":           "Mostrando registros del _START_ al _END_ de un total de _TOTAL_ registros",
                            "sInfoEmpty":      "Mostrando registros del 0 al 0 de un total de 0 registros",
                            "sInfoFiltered":   "(filtrado de un total de _MAX_ registros)",
                            "sInfoPostFix":    "",
                            "sSearch":         "Consult Students:",
                            "sUrl":            "",
                            "sInfoThousands":  ",",
                            "sLoadingRecords": "Loading..",
                            "oPaginate":
                             {
                                "sFirst":    "Primero",
                                "sLast":     "Último",
                                "sNext":     "Siguiente",
                                "sPrevious": "Anterior"
                            },
                            "oAria":
                             {
                                "sSortAscending":  ": Activar para ordenar la columna de manera ascendente",
                                "sSortDescending": ": Activar para ordenar la columna de manera descendente"

                            }
                        },
                    }); 

    list_data();
        function list_data()
        {       
    $.ajax({
                        url: 'calificaciones_estudiantes',
                        type: 'POST'
                       })
                .done(function(result)
                {
                    var answer= $.parseJSON(result);

                    if(answer.estado == "vacio")
                    {
                        var response = '<div class="alert alert-warning alert-dismissable">'
                        + '<strong>¡Error!</strong>' + ' ' +answer.mensaje + '</div>';
                        $('#alert').html(response);
                    }

                    if(answer)
                    {
                        for (var i = answer.length - 1; i >= 0; i--) 
                        {
                            var rowNode = table_show
                           .row.add([                               
                                        answer[i].nombre_estudiante,
                                        answer[i].fecha_calificacion,
                                        answer[i].area,
                                        answer[i].calificacion
                                    ])
                           .draw()
                           .node();
                        }
                    }
                })
                .fail(function() {
                    console.log("error");
                    });  
            }

WHAT IS REQUIRED:
A student may have different grades, so to "not lose" because they are many grades, I would like to validate if answer [i] .qualification="good" then show me the letters green for example, if it's bad to show me red ...
The problem is that I would not know how to do that validation in that case. I thank you in advance if you can help me.

    
asked by JDavid 01.02.2017 в 14:54
source

1 answer

1

In your cycle for within the request ajax would add a if linear leaving this way:

for (var i = answer.length - 1; i >= 0; i--) 
{
  var rowNode = table_show
  .row.add([                               
    answer[i].nombre_estudiante,
    answer[i].fecha_calificacion,
    answer[i].area,
    (answer[i].calificacion == "bueno" ? 
        "<label style='color:green;'>bueno</label>" : 
        "<label style='color:red;'>malo</label>")
  ])
  .draw()
  .node();
}

In the case that you have 3 more data that you want to validate, you just have to nest the if example:

(answer[i].calificacion == "bueno" ? 
  "<label style='color:green;'>bueno</label>" : 
  //segunda validación 
  (answer[i].calificacion == "regular" ? 
    "<label style='color:yellow;'>regular</label>" : 
    "<label style='color:red;'>malo</label>"
  )
)
    
answered by 01.02.2017 / 16:08
source