Buenas! , no soy muy ducho en javascript (recien lo vengo aprendiendo ) asi que bueno esto es lo logre armar, es para agregar inputs dinamicos a una tabla detalle .
Código:
num = 0;
function addfila(id_tabla){
num++;
var tabla = document.getElementById(id_tabla);
var txtcodigo = document.getElementById("txtcodigo");
var txtnombre = document.getElementById("txtnombre");
var txtcantidad = document.getElementById("txtcantidad");
var txtprecio = document.getElementById("txtprecio");
var txtimporte = document.getElementById("txtimporte");
var contarFilas = (tabla.rows.length);//obtenemos el numero de filas - 1
var filas = tabla.insertRow(contarFilas);//insertamos una nueva fila en la posicion indicada.
var chk = filas.insertCell(0); //Inserta una nueva columna en la posición indicada dentro del array de columnas de la fila.
var e1 = document.createElement("input");//creamos un elemento input
e1.type = "checkbox"; //tipo
e1.name = "chk" + num; //nombre
//Con este método añadiremos el elemento “e1" despues de “chk”.
//permite insertar un elemento al final del otro
chk.appendChild(e1);
var codproducto = filas.insertCell(1);
var e2 = document.createElement("input");
e2.text = txtcodigo.value;
e2.value = txtcodigo.value;
e2.type = "text";
e2.name = "codproducto" + num;
e2.readOnly = true;
e2.size = "10";
codproducto.appendChild(e2);
//nombre del Producto
var nombrepro = filas.insertCell(2);
var e3 = document.createElement("input");
e3.text = txtnombre.value;
e3.value = txtnombre.value;
e3.type = "text";
e3.name = "nombrepro" + num;
e3.readOnly = true;
e3.size = "30";
nombrepro.appendChild(e3);
//cantidad
var cantidad = filas.insertCell(3);
var e4 = document.createElement("input");
e4.text = txtcantidad.value;
e4.value = txtcantidad.value;
e4.type = "text";
e4.name = "cantidad" + num;
e4.readOnly = true;
e4.size = "4";
cantidad.appendChild(e4);
//precio
var precio = filas.insertCell(4);
var e5 = document.createElement("input");
e5.text = txtprecio.value;
e5.value = txtprecio.value;
e5.type = "text";
e5.name = "precio" + num;
e5.size = "4";
e5.readOnly = true;
precio.appendChild(e5);
//importe
var importe = filas.insertCell(5);
var e6 = document.createElement("input");
e6.text = txtimporte.value;
e6.value = txtimporte.value;
e6.type = "text";
e6.name = "importe" + num;
e6.size = "10";
importe.appendChild(e6);
//limpiar_cajas();
//txtimporte.focus();
var boton = filas.insertCell(6);
var e7 = document.createElement("input");
e7.type = "button";
e7.text = "Borrar Elemento";
e7.value = 'Borrar';
e7.name = "btnborrar";
e7.id = num;
e7.onclick = function(){
borrar()
} // 9
boton.appendChild(e7);
}
function sumar(sum){
var sum = 0;
var tabla = document.getElementById("detalle")
var contarFila = tabla.rows.length - 1;
for (var i = 0; i <= contarFila; i++) {
var importe = tabla.rows[i].cells[5].childNodes[0].value;
sum = parseFloat(sum, 2) + parseFloat(importe, 2);
}
var subtotal = sum;
var igv = parseFloat(parseFloat(subtotal, 2) * 0.19, 2);
var total = parseFloat(subtotal + igv);
document.frmfactura.subtotal.value = subtotal;
document.frmfactura.igv.value = igv;
document.frmfactura.total.value = total;
}
function borrar(){
var tabla = document.getElementById("detalle");
var contarFila = tabla.rows.length;
for (var i = 0; i < contarFila; i++) {
var fila = tabla.rows[i];
var p = fila.cells[5].childNodes[0].value;
if (p == "") {
p = 0;
}
var chkbox = fila.cells[0].childNodes[0];
if (null != chkbox && true == chkbox.checked) {
var subtotal = parseFloat(document.frmfactura.subtotal.value) - parseFloat(p);
var igv = parseFloat(parseFloat(subtotal, 2) * 0.19, 2);
var total = parseFloat(subtotal + igv);
document.frmfactura.subtotal.value = subtotal;
document.frmfactura.igv.value = igv;
document.frmfactura.total.value = total;
tabla.deleteRow(i);
contarFila--;
i--;
}
}
}
</script>
Con este codigo agrega inputs dinamicos a una tabla detalle , estos valores los recupero desde un popup. Me gustaria saber como podria validar si este producto repetido y ya ha sido agregado al detalle de la factura.
Un saludo.