Ver Mensaje Individual
  #6 (permalink)  
Antiguo 06/02/2008, 04:37
infolan
 
Fecha de Ingreso: noviembre-2005
Mensajes: 405
Antigüedad: 18 años, 7 meses
Puntos: 0
Re: Añadir campos dinámicamente

Ya tengo resuelta la otra parte, lo único que me gustaría es poder sumar todas las tarifas y ponerlo en textbox que hay al lado del enlace "Añadir...".

Tiniendo encuenta, que si se añade un articulo se suma la cantidad o si se elimina, se resta.

Os paso el codigo :

Ventana padre

<html>
<head>
<title>Test</title>
</head>
<script>
var number ="100";

function eliminarAll(){
var tbody = document.getElementById("table1").getElementsByTag Name("tbody")[0];
while (tbody.hasChildNodes()) {
tbody.removeChild(tbody.firstChild);
}
}

function eliminar(idRow)
{
nodoTabla = document.getElementById(idRow);
nodoTabla.parentNode.removeChild(nodoTabla);
}

function addTextBox()
{
number++;
var tbody = document.getElementById("table1").getElementsByTag Name("tbody")[0];
var row = document.createElement("tr");

var cell1 = document.createElement("td");

cell1.style.border="0px";
cell1.style.cellspacing="0px";
cell1.style.cellpadding="0px";

var papelera = document.createElement("a");
var img = document.createElement("IMG");
img.src="../tools/img/papelera.gif";
img.border=0;
img.alt="Eliminar";

papelera.appendChild(img);
papelera.href="javascript:eliminar('"+number+"');" ;
cell1.appendChild(papelera);

var espacio = document.createElement("IMG");
espacio.border=1
espacio.src="transparent.gif";
espacio.width="15px";
cell1.appendChild(espacio);

var inputTextCodigo = document.createElement("INPUT");
inputTextCodigo.type = "text";
inputTextCodigo.name = "textCodigo[" + number + "]";
inputTextCodigo.id = "textCodigo[" + number + "]";
inputTextCodigo.size = 15;
//inputTextCodigo.value = "textCodigo[" + number + "]";
cell1.appendChild(inputTextCodigo);

var espacio = document.createElement("IMG");
espacio.border=1
espacio.src="transparent.gif";
espacio.width="15px";
cell1.appendChild(espacio);

var inputTextDescr = document.createElement("INPUT");
inputTextDescr.type = "text";
inputTextDescr.name = "textDescripcion[" + number + "]";
inputTextDescr.id = "textDescripcion[" + number + "]";
inputTextDescr.size = 70;
//inputTextDescr.value = "textDescripcion[" + number + "]";
cell1.appendChild(inputTextDescr);

var espacio = document.createElement("IMG");
espacio.border=1
espacio.src="transparent.gif";
espacio.width="15px";
cell1.appendChild(espacio);

var inputTextCantidad = document.createElement("INPUT");
inputTextCantidad.type = "text";
inputTextCantidad.name = "textTextCantidad[" + number + "]";
inputTextCantidad.id = "textTextCantidad[" + number + "]";
inputTextCantidad.size = 20;
//inputTextCantidad.value = "textTextCantidad[" + number + "]";
cell1.appendChild(inputTextCantidad);

var newLink=document.createElement('a');
newLink.setAttribute('name','buscar[' + number + ']');
newLink.setAttribute('href','buscar.html?opcion=' + number);
newLink.setAttribute('target','_blank');
newLink.className='contrast';
var linkText=document.createTextNode('buscar');
newLink.appendChild(linkText);

cell1.appendChild(newLink);


row.appendChild(cell1);
row.id = number;
tbody.appendChild(row);
}

</script>
<body>
<table border="1" width="100%">
<tr>
<td width="16%">Código</td>
<td width="47%">Descripción</td>
<td width="">Cantidad</td>
</tr>
<tr>
<td colspan="6">
<table id="table1" style="border:1px;cellspacing:0px;cellpadding:0px; " border="0">
<tbody></tbody>
</table>
</td>
</tr>
<tr>
<td colspan="5">
<input type="text" size="10" maxlength="10">
<a href="javascript:addTextBox();">Añadir Articulos</a>
<img src="transparent.gif" width="10%" border="0" height="1">
<a href="javascript:eliminarAll();">Eliminar todos</a>
</td>
</tr>
</table>
</body>
</html>


Ventana hija "buscar" :


<head>
<title>Buscar</title>
</head>

<script language="javascript">

var url = location.href;
var index = url.indexOf("?");
var parameter = "opcion";

index = url.indexOf(parameter,index) + parameter.length;
if (url.charAt(index) == "=")
{
var result = url.indexOf("&",index);
if (result == -1){result=url.length;};
codSeleccion = url.substring(index + 1,result);
}

//var codSeleccion = "102";

function aceptar()
{
var textCodigoParent = "textCodigo[" + codSeleccion + "]";
var textDescParent = "textDescripcion[" + codSeleccion + "]";
var textTarifaParent = "textTextCantidad[" + codSeleccion + "]";

var ventanaPadre = window.opener;

var txtCodigo = document.getElementById("codigo");
var txtDesc = document.getElementById("desc");
var txtTarifa = document.getElementById("tarifa");

ventanaPadre.document.getElementById(textCodigoPar ent).value = txtCodigo.value;
ventanaPadre.document.getElementById(textDescParen t).value = txtDesc.value;
ventanaPadre.document.getElementById(textTarifaPar ent).value = txtTarifa.value;
window.close();
}

function calcularSumar()
{

}

</script>
<body>
Buscar

<table>
<tr><td>Prueba con un código manual.</td></tr>
</table>
<br>

<table>
<tr>
<td>Codigo</td>
<td><input type="text" size="20" name="codigo" id="codigo"></td>
</tr>
<tr>
<td>Descripcion</td>
<td><input type="text" size="70" name="desc" id="desc"></td>
</tr>
<tr>
<td>Tarifa</td>
<td><input type="text" size="20" name="tarifa" id="tarifa"></td>
</tr>
<tr><td><input type="button" name="" value="Aceptar" onClick="javascript:aceptar();"></td></tr>
</table>
</body>
</html>

A ver si entre todos podemos terminar esto, está ya el final