Calculate subTotal and Total in a dynamic html and Javascript table

0

I hope you can help me, I have created a table where I register the item, quantity, unit price and I need you to calculate unit price and assign it to subtotal and then the sum of the subtotals to an input of TOTAL, what I do is search a purchase order already loaded and I stretch it from the purchase order, then all the items in the purchase order are loaded in the purchase order and that's where I need you to calculate the subtotals and the total.

function RecuperarSolicitud() {
  // alert("Llegamos...");
  // 1. Instantiate XHR - Start 
  var xhr;
  if (window.XMLHttpRequest) //verifica que el navegador tenga soporte
    xhr = new XMLHttpRequest();
  else if (window.ActiveXObject)
    xhr = new ActiveXObject("Msxml2.XMLHTTP");
  else
    throw new Error("Ajax is not supported by your browser");

  xhr.onreadystatechange = function() {
    // alert(xhr.responseText);
    if (xhr.readyState === 4) {
      if (xhr.status === 200 && xhr.status < 300) {
        var json = JSON.parse(xhr.responseText); //reponseText returns the entire JSON file and we assign it to a javascript variable "json".
        var x;
        var d;
        var tindex;

        for (x in json) {
          document.getElementById('codigo_solicitud').value = json[x].nro_solicitud;
          document.getElementById('fecha_solicitud').value = json[x].fecha;
          document.getElementById('codigo_empleado').value = json[x].cod_empleado;
          document.getElementById('empleado').value = json[x].empleado_descripcion;
          document.getElementById('codigo_departamento').value = json[x].cod_departamento;
          document.getElementById('departamento').value = json[x].departamento_descripcion;
          document.getElementById('codigo_deposito').value = json[x].cod_deposito;
          document.getElementById('deposito').value = json[x].deposito_descripcion;
          document.getElementById('sucursal').value = json[x].sucursal_descripcion;
          document.getElementById('menuPrioridad').value = json[x].prioridad;
          document.getElementById('observacion_solicitud').value = json[x].descripcion;

          for (d in json[x].lista_mercaderia) {
            //                        alert(json[x].lista_mercaderia[d].cod_mercaderia);
            //                        alert(json[x].lista_mercaderia[d].descripcion);
            //                        alert(json[x].lista_mercaderia[d].descripcion_marca);
            //                        alert(json[x].lista_mercaderia[d].cantidad);
            $('#detalleTablaMercaderia').append("<tr id=\'prod" + tindex + "\'>\n\
                                <td for='cod' id='cod' style=' width: 10%;'>" + json[x].lista_mercaderia[d].cod_mercaderia + "</td>\n\
                                <td id='des' style=' width: 10%;'>" + json[x].lista_mercaderia[d].descripcion_medida + "</td>\n\
                                <td id='med' style=' width: 40%;'>" + json[x].lista_mercaderia[d].articulo + "</td>\n\
                                <td id='mar' style=' width: 20%;'>" + json[x].lista_mercaderia[d].descripcion_marca + "</td>\n\
                                <td id='cant' style=' width: 10%;'>" + json[x].lista_mercaderia[d].cantidad + "</td>\n\
                                <td style=' width: 5%;'><img class='update' onclick=\"$(\'#prod" + tindex + "\')\" src='../Recursos/Img/update.png'/></td>\n\
                                <td style=' width: 5%;'><img class='delete' onclick=\"$(\'#prod" + tindex + "\')\" src='../Recursos/Img/delete.png'/></td>\n\
                          </tr>");
          }
        }
      }
    }
  };
  xhr.open('POST', '/JavaWeb_Compras/Solicitud_CompraCTR');
  xhr.send(JSON.stringify(datos = {
    bandera: 2,
    nro_solicitud: $('#codigo_solicitud').val()
  }));
  $('#codigo_solicitud').disabled;
  // 3. Specify your action, location and Send to the server - End
}
<html>

<head>
  <title>TODO supply a title</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
  <script type="text/javascript" src="../Recursos/JS/jquery-1.7.1.min.js"></script>
  <script type="text/javascript" src="../Recursos/JS/Solicitud_CompraJS.js"></script>
  <script type="text/javascript" src="../Recursos/JS/all.js"></script>
  <script type="text/javascript" src="../Recursos/JS/moment.js"></script>
  <script type="text/javascript" src="../Recursos/JS/jquery-2.1.1.min.js"></script>
  <script type="text/javascript" src="../Recursos/JS/jquery-1.10.2.min.js"></script>
</head>

<body>
  <div class="w3-row w3-section">
    <div>
      <table class="w3-table-all">
        <thead>
          <tr>
            <th style=" width: 10%;">CODIGO</th>
            <th style=" width: 10%;">MEDIDA</th>
            <th style=" width: 40%;">DESCRIPCION</th>
            <th style=" width: 20%;">MARCA</th>
            <th style=" width: 10%;">CANTIDAD</th>
            <th style=" width: 5%;"><img class="update" id="update" src="../Recursos/Img/update.png" /></th>
            <th style=" width: 5%;"><img id="delete" src="../Recursos/Img/delete.png" /></th>
            <!-- 
                                            <th style=" width: 5%;"><img id="update" src="../Recursos/Img/update.png"/></th>
                                            <th style=" width: 5%;"><img id="delete" src="../Recursos/Img/delete.png"/></th>
                                            -->
          </tr>
        </thead>
        <tbody id="detalleTablaMercaderia">
        </tbody>
      </table>
    </div>
  </div>
</body>

</html>

    
asked by Edgar Alegre 11.12.2018 в 12:55
source

0 answers