How to add a condition in this code

4

I have the following function in javacritps, but I want to be able to add a condition if the request is with yes, the checkbox is disable

  function MuestraDatos(Valor) {
        data = JSON.parse(Valor);
        var indice = 0;
        //alert("entra a mostrar");
        $.each(data, function(index, value) {
            /* Vamos agregando a nuestra tabla las filas necesarias */
            indice = indice + 1;
            $("#TablaCargaOP").append("<tr>" +
                "<td>" + value.OP + "</td>" +
                "<td>" + value.Item + "</td>" +
                "<td>" + value.CodPza + "</td>" +
                "<td>" + value.Desc + "</td>" +
                "<td data-name='Cantidad' class='Cantidad' data-type='text'>" + value.CantPzas + "</td>" +
                "<td>" + value.QueNivel + "</td>" +
                "<td>" + value.Pedido + "</td>" +
                "<td>" + if (value.Pedido == 'SI') {
                  " <div class='form-check'><input type='checkbox' disable class='form-check-input' id='Escoge"+indice+"' name='Escoge'/>" + " <label class='form-check-label' for='Escoge"+indice+"'></label></div></td>"
                } else {
                  " <div class='form-check'><input type='checkbox' class='form-check-input' id='Escoge"+indice+"' name='Escoge'/>" + " <label class='form-check-label' for='Escoge"+indice+"'></label></div></td>"
                }  +
                "</tr>");
        });
        $('#TablaCargaOP').ddTableFilter();
}

I get the following error

SyntaxError: expected expression, got keyword 'if'
    
asked by MoteCL 20.09.2018 в 21:06
source

4 answers

3
  

The problem is that you put code in the concatenation ( + ) of    strings , and does not know what to do since it is not a specific type of data.

Try the following, which is to define outside with the if that td corresponds to assign the string to a variable and then concatenate it.

function MuestraDatos(Valor) {
        data = JSON.parse(Valor);
        var indice = 0;
        //alert("entra a mostrar");
        $.each(data, function(index, value) {
            /* Vamos agregando a nuestra tabla las filas necesarias */
            indice = indice + 1;

            var tdToAdd;
            if (value.Pedido == 'SI') {
                 tdToAdd = "<td> <div class='form-check'><input type='checkbox' disable class='form-check-input' id='Escoge"+indice+"' name='Escoge'/>" + " <label class='form-check-label' for='Escoge"+indice+"'></label></div></td>"
                } 
                else {
                  tdToAdd = "<td> <div class='form-check'><input type='checkbox' class='form-check-input' id='Escoge"+indice+"' name='Escoge'/>" + " <label class='form-check-label' for='Escoge"+indice+"'></label></div></td>"
            }

            $("#TablaCargaOP").append("<tr>" +
                "<td>" + value.OP + "</td>" +
                "<td>" + value.Item + "</td>" +
                "<td>" + value.CodPza + "</td>" +
                "<td>" + value.Desc + "</td>" +
                "<td data-name='Cantidad' class='Cantidad' data-type='text'>" + value.CantPzas + "</td>" +
                "<td>" + value.QueNivel + "</td>" +
                "<td>" + value.Pedido + "</td>" +
                tdToAdd +
                "</tr>");
        });
        $('#TablaCargaOP').ddTableFilter();
}
    
answered by 20.09.2018 / 21:14
source
4

You have a syntax error on the line

"<td>" + if (value.Pedido == 'SI') ...

To solve this you have two alternatives, one is to finish concatenating the string , then evaluate the condition, and then add (or not) what is missing

Otherwise, the other option would be to use a ternary operator (which I assume is closest to what you want to do)

It would be something like that.

"<td>" + (value.Pedido == 'SI' ? "HtmlSiSeCumple" : "HtmlSiNoSeCumple") ...

Greetings

    
answered by 20.09.2018 в 21:16
2

Your problem is given because in a chain summing operation (concatenation) you can not include an if statement, since it only expects an operand that can be another string, a number, a function or a variable. To solve this if you look carefully the only difference between the text you include when the condition is met and when it is not met is that in the first case you add or remove the disabled attribute to the checkbox, so you can do the following, you avoid repeating unnecessary code.

var pedido = "<div class='form-check'><input type='checkbox' "+((value.Pedido == 'SI') ? 'disabled ' : '')+ "class='form-check-input' id='Escoge"+indice+"' name='Escoge'/>" + " <label class='form-check-label' for='Escoge"+indice+"'></label></div></td>" :
$("#TablaCargaOP").append("<tr>" +
    "<td>" + value.OP + "</td>" +
    "<td>" + value.Item + "</td>" +
    "<td>" + value.CodPza + "</td>" +
    "<td>" + value.Desc + "</td>" +
    "<td data-name='Cantidad' class='Cantidad' data-type='text'>" + value.CantPzas + "</td>" +
    "<td>" + value.QueNivel + "</td>" +
    "<td>" + value.Pedido + "</td>" +
    "<td>" + pedido +
    "</tr>");
    
answered by 20.09.2018 в 21:14
2

The problem is that you are trying to concatenate a if with text strings. Your code should be as follows:

function MuestraDatos(Valor) {
  data = JSON.parse(Valor);
  var indice = 0;
  //alert("entra a mostrar");
  $.each(data, function(index, value) {
      /* Vamos agregando a nuestra tabla las filas necesarias */
      indice = indice + 1;
      $("#TablaCargaOP").append("<tr>" +
          "<td>" + value.OP + "</td>" +
          "<td>" + value.Item + "</td>" +
          "<td>" + value.CodPza + "</td>" +
          "<td>" + value.Desc + "</td>" +
          "<td data-name='Cantidad' class='Cantidad' data-type='text'>" + value.CantPzas + "</td>" +
          "<td>" + value.QueNivel + "</td>" +
          "<td>" + value.Pedido + "</td>" +
          "<td>" + " <div class='form-check'><input type='checkbox' " + (value.Pedido == 'SI' ? 'disable' : '') + " class='form-check-input' id='Escoge"+indice+"' name='Escoge'/>" + " <label class='form-check-label' for='Escoge"+indice+"'></label></div></td>" +
          "</tr>");
  });
  $('#TablaCargaOP').ddTableFilter();
}

The expression (value.Pedido == 'SI' ? 'disable' : '') refers to a ternary operator , which is used to evaluate conditionals in a single line (very useful in cases like yours).

Greetings!

    
answered by 20.09.2018 в 21:15