Hello guys,

I have the following scenario: I have a set of input fields that need
to be cloned as requested in order to add more records of a single
model, this is my view code:

                        <div id="productos0" style="display:block;">
                        <?php echo $form->input("FacturaDetalle.producto_id", 
array('div'
=> '', 'type' => 'hidden', 'name' => "data[FacturaDetalle][0]
[producto_id]")) ?>
                        <?php echo $form->input("FacturaDetalle.cantidad", 
array('div' =>
'', 'label' => 'Cantidad','name' => "data[FacturaDetalle][0]
[cantidad]", 'size' => '3', 'onChange' => "calcularImporte(this.value,
document.getElementById('FacturaDetallePrecioUnitario').value,
'FacturaDetalleImporte')")); ?>
                <?php echo $form->label
('FacturaDetalle.producto_codigo','Clave'); ?>
                <?php echo $ajax->autocomplete('FacturaDetalle.producto_codigo',
'/productos/seleccionar', array('size' => 5, 'afterUpdateElement' =>
'getProductId'));?>
                <?php echo $ajax->ObserveField('FacturaDetalleProductoId', array
('url' => '/facturas/agregarProducto', 'update' => 'productos0',
'frequency' => 1, 'with' => "Form.Element.serialize
('FacturaDetalleProductoId')+'&'+Form.Element.serialize
('FacturaDetalleCantidad')"));?>
                        <?php //echo 
$form->input('FacturaDetalle.producto_codigo', array
('div' => '','label' => 'Clave', 'name' => "data[FacturaDetalle][0]
[producto_codigo]", 'size' => 5, 'afterUpdateElement' =>
'getProductId'));?>
                        <?php echo $form->input("FacturaDetalle.concepto", 
array('div' =>
'', 'label' => 'Concepto','name' => "data[FacturaDetalle][0]
[concepto]", 'size' => '30')); ?>
                        <?php echo 
$form->input("FacturaDetalle.precio_unitario", array
('div' => '', 'label' => 'P.U. <small>(con I.V.A.)</small>','name' =>
"data[FacturaDetalle][0][precio_unitario]", 'size' => '6', 'onChange'
=> "calcularImporte(document.getElementById
('FacturaDetalleCantidad').value, this.value,
'FacturaDetalleImporte')")); ?>
                        <?php echo $form->input("FacturaDetalle.importe", 
array('div' =>
'', 'label' => 'Importe','name' => "data[FacturaDetalle][0][importe]",
'size' => '6')); ?>
                        <?php echo $form->button('+', array('onclick' => 
"addRow()")); ?>
                        <?php echo $form->button('-', array('onclick' =>
'this.parentNode.parentNode.removeChild(this.parentNode);'));?
>
                        </div>
                        <span id="writeroot"></span>

If I click on + button, all the form fields are cloned using
javascript, this is the code of addRow function:

function addRow() {
    counter++;
    var newRows = document.getElementById('productos0').cloneNode
(true);
    newRows.style.display = 'block';
    newRows.id = 'productos' + counter;
    var newRow = newRows.childNodes;
        for(var i=0;i<newRow.length;i++) {
                if(newRow[i].nodeName == 'SCRIPT') {
                        var html = newRow[i].innerHTML;
                        html = html.replace(/FacturaDetalleProductoCodigo/,
"FacturaDetalleProductoCodigo" + counter);
                        html = 
html.replace(/FacturaDetalleProductoCodigo_autoComplete/,
"FacturaDetalleProductoCodigo_autoComplete" + counter);
                        html = html.replace(/FacturaDetalleProductoId/g,
"FacturaDetalleProductoId" + counter);
                        html = html.replace(/FacturaDetalleCantidad/,
"FacturaDetalleCantidad" + counter);
                        html = html.replace(/productos0/g, "productos" + 
counter);
                        newRow[i].innerHTML = html;
                }
                var domid = newRow[i].id;
                var name = newRow[i].name;
                if(domid) {
                        if(domid == "FacturaDetalleCantidad"){
                                newOnChange = 
newRow[i].attributes.onchange.value;
                                newOnChange = 
newOnChange.replace(/FacturaDetalleImporte/g,
"FacturaDetalleImporte" + counter);
                                newRow[i].attributes.onchange.value = 
newOnChange.replace(/
FacturaDetallePrecioUnitario/g, "FacturaDetallePrecioUnitario" +
counter);
//                              alert(newRow[i].attributes.onchange.value);
                        }
                        else if(domid == "FacturaDetallePrecioUnitario"){
                                newOnChange = 
newRow[i].attributes.onchange.value;
                                newOnChange = 
newOnChange.replace(/FacturaDetalleImporte/g,
"FacturaDetalleImporte" + counter);
                                newRow[i].attributes.onchange.value = 
newOnChange.replace(/
FacturaDetalleCantidad/g, "FacturaDetalleCantidad" + counter);
//                              alert(newRow[i].attributes.onchange.value);
                        }
                        newRow[i].id = domid + counter;
                }
                if(name)
                        newRow[i].name = name.replace(/[0]/, counter);
        }
        var insertHere = document.getElementById('writeroot');
        insertHere.parentNode.insertBefore(newRows,insertHere);
}

The code generated by the addRow function is correct, but the
autocomplete doesn't work when is cloned, if I type directly the code
generated in the view the autocomplete works fine, any idea of what
might be happening
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to