Ver Mensaje Individual
  #12 (permalink)  
Antiguo 23/07/2015, 19:35
pandora09
 
Fecha de Ingreso: julio-2015
Mensajes: 16
Antigüedad: 8 años, 9 meses
Puntos: 1
Respuesta: Al ejecutar RemoveChild restar valor al total

Alexis88 gracias por responder y estar pendiente,

Si he tu codigo pero no me arroja nada, es mas ni siquiera me agrega las filas. No se si te comente anteriormente pero creo las filas de manera dinamica con js. Este es todo mi codigo js para agregar filas, calcular subtotal, total y eliminar filas lo que me falta es que reste el valor de la fila eliminada al campo del total esto solo lo hace bien cuando tengo un solo producto agregado pero si tengo dos o mas me devuelve NaN.

Este es el codigo js.

Código Javascript:
Ver original
  1. var campo1 = document.createElement("select");
  2.         campo1.type = "select";
  3.         campo1.name = "servicio[]";
  4.         for (i=0; i<28; i++) {
  5.         opt = document.createElement('option');
  6.         opt.value = myarray[i];
  7.         opt.innerHTML = myarray[i];
  8.         campo1.appendChild(opt);
  9.         }
  10.         campo1.size = "1";
  11.  
  12.     var miarray=new Array(3)
  13.     miarray[0] = "0"
  14.     miarray[1] = "1"
  15.     miarray[2] = "2"
  16.     miarray[3] = "3"
  17.     miarray[4] = "4"
  18.     miarray[5] = "5"
  19.  
  20.     var campo2 = document.createElement("select");
  21.         campo2.type = "select";
  22.         campo2.name = "cantidad[]";
  23.         campo2.id = "cantidad";
  24.         for (i=0; i<6; i++) {
  25.         opt = document.createElement('option');
  26.         opt.value = miarray[i];
  27.         opt.innerHTML = miarray[i];
  28.         campo2.appendChild(opt);
  29.         }
  30.         campo2.size = "1";
  31.         campo2.style.width = "50px";
  32.  
  33.     var campo3 = document.createElement("input");
  34.         campo3.type = "text";
  35.         campo3.name = "preciounit[]";
  36.         campo3.id = "preciounit";
  37.         campo3.placeholder = "0,00";
  38.         campo3.style.textAlign = "right";
  39.         campo3.setAttribute("onChange","Calcular(this);");
  40.    
  41.     var campo4 = document.createElement("input");
  42.         campo4.type = "text";
  43.         campo4.name = "totalitem[]";
  44.         campo4.id = "totalitem";
  45.         campo4.class = "costo";
  46.         campo4.placeholder = "0,00";
  47.         campo4.readOnly = "true";
  48.         campo4.style.textAlign = "right";
  49.    
  50.     var campo5 = document.createElement("input");
  51.         campo5.type = "image";
  52.         campo5.src = '14.png';
  53.         campo5.style.background = "#143D55";
  54.         campo5.style.border = "1px solid #320C83";
  55.         campo5.style.borderRadius = "100px";
  56.         campo5.style.marginLeft = "60px";
  57.         campo5.onclick = function() {
  58.        
  59.             var fila = this.parentNode.parentNode;
  60.             var tbody = tabla.getElementsByTagName("tbody")[0];
  61.             var total = document.getElementById("total");
  62.             if (total.innerHTML == 'NaN') {
  63.             total.innerHTML = 0;
  64.             }
  65.             total.innerHTML = (Number(total.innerHTML)-Number(totalitem.value)).toFixed(2);
  66.  
  67.             tbody.removeChild(fila);
  68.  
  69.             }
  70.  
  71.    
  72.     celda1.appendChild(campo1);
  73.     celda2.appendChild(campo2);
  74.     celda3.appendChild(campo3);
  75.     celda4.appendChild(campo4);
  76.     celda5.appendChild(campo5);
  77.  
  78. }
  79.  
  80. function vaciar_campo(input) {
  81.    
  82.     input.value = "";
  83.    
  84. }
  85.  
  86. function Calcular(ele) {
  87. var cantidad = 0, precunit = 0, totalitem = 0;
  88. var tr = ele.parentNode.parentNode;
  89. var nodes = tr.childNodes;
  90. for (var x = 0; x<nodes.length;x++) {
  91. if (nodes[x].firstChild.name == 'cantidad[]') {
  92. cantidad = parseFloat(nodes[x].firstChild.value,10);
  93. }
  94. if (nodes[x].firstChild.name == 'preciounit[]') {
  95. preciounit = parseFloat(nodes[x].firstChild.value,10);
  96. }
  97. if (nodes[x].firstChild.name == 'totalitem[]') {
  98. totalitem = parseFloat(preciounit*cantidad).toFixed(2);
  99. nodes[x].firstChild.value = totalitem;
  100. }
  101. }
  102. var total = document.getElementById("total");
  103. if (total.innerHTML == 'NaN') {
  104. total.innerHTML = 0;
  105. }
  106. total.innerHTML = (Number(total.innerHTML)+Number(totalitem)).toFixed(2);
  107. }

Aun no he podido dar con el error no se porque me devuelve NaN cuando tengo dos productos o mas agregados y procedo a eliminarlos.