views:

73

answers:

2

Hello, I'm almost new to jQuery... I found some tutorials/posts explaining 1. how to clone a table row (useful for inserting invoice details): http://forum.jquery.com/topic/validate-will-not-submit-to-server 2. how to use the jQuery UI autocomplete with PHP and MySQL (useful for retrieving products from a db table and avoid typing everything): http://www.jensbits.com/2010/03/29/jquery-ui-autocomplete-widget-with-php-and-mysql/

Well, 'til now I can clone table rows and I can get autocmplete work on the first row, but problems arise when I add another row and I expect the autocomplete work there, too I found a thread here in StackOverflow (http://stackoverflow.com/questions/1492198/jquery-auto-complete-for-dynamically-generated-textboxes), but had no luck of applying to my case...

And now, the code:

The HTML part

<table border="0" cellspacing="0" cellpadding="4" class="grid" id="details">
 <thead>
  <tr>
   <th scope="col">Codice</th>
   <th scope="col">Nome</th>
   <th scope="col">Quantità</th>
   <th scope="col">Unità di misura</th>
   <th scope="col">Costo U.</th>
   <th scope="col">Totale</th>
  </tr>
 </thead>
 <tbody>
  <tr id="row_0" class="iterable">
   <td><input type="text" name="det_sku_0" id="det_sku_0" class="sku required" /></td>
   <td><input type="text" name="det_name_0" id="det_name_0" class="name required" /></td>
   <td><input name="det_quantity_0" id="det_quantity_0" type="text" class="required" /></td>
   <td>&nbsp;</td>
   <td><input name="det_price_0" id="det_price_0" type="text" class="required" />€</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>
<ul>
 <li><a href="javascript:void(0);" id="remove" class="icons icon-0">Rimuovi ultima riga</a></li>
 <li><a href="javascript:void(0);" id="add" class="icons icon-new">Aggiungi riga</a></li>
 <li><input type="submit" name="button" id="button" value="Salva tutto" /></li>
</ul>

The jQuery part

    function addrow(destination) {
     rowcount = parseInt(parent_row.attr('id').replace('row_',''))+1;
     clonecopy = destination.clone(true);
     clonecopy.attr("class","iterable");
     // update numerical suffixes
     clonecopy.attr("id","row_"+rowcount);
     clonecopy.find('.sku, .name').val('');
     clonecopy.find("input[name^='det_sku']").attr({
      "name": "det_sku_"+rowcount,
      "id": "det_sku_"+rowcount
     });
     clonecopy.find("input[name^='det_name']").attr({
      "name": "det_name_"+rowcount,
      "id": "det_name_"+rowcount
     });
     clonecopy.find("select[name^='det_quantity']").attr({
      "name": "det_quantity_"+rowcount,
      "id": "det_quantity_"+rowcount
     });
     clonecopy.find("select[name^='det_price']").attr({
      "name": "det_price"+rowcount,
      "id": "det_price"+rowcount
     });
     clonecopy.insertAfter(destination);
     $('#det_arrayitems').val(rowcount);
    }

$("#add").click(function() {
        parent_row = $('#details tbody>tr:last');
        addrow(parent_row);
    });    

$('input.sku').autocomplete({
     source: "../json/products.php",
     minLength: 2,
     select: function(event, ui) {
      $(this).parent().siblings().children('input.name').val(ui.item.name);
     }
    })

I also tried this (and a couple of other ideas), but no luck

$("#add").live("click", function() {
 parent_row = $('#details tbody>tr:last');
 addrow(parent_row);
 $('input.sku').autocomplete({
  source: "../json/products.php",
  minLength: 2,
  select: function(event, ui) {
   $('input.sku').parent().siblings().children('input.name').val(ui.item.name);
  }
 })
});

Please, can you help me? Thanks...

A: 

I do something similar where I have a div that holds all the rows, and eact input field in a row has a rel tag with the base part of it's name and ID and then on the click to add a row I call the following:

$("#addChain").click(function() {
var index = $("#chainHolder").children().length + 1;
$("#chainHolder").children(":first").clone().each(function() {
$(this).find(":input").each(function() {
$(this).attr("id", $(this).attr("rel")+"["+index+"]");
$(this).attr("name", $(this).attr("rel")+"["+index+"]");
});
$(this).children(":first").val(index);
}).appendTo("#chainHolder");
});

FatherStorm
Uhm... I miss the Autocomplete part here... I have no difficulties in adding rows to a table with jQuery, but in making the Autocomplete work for every created row
Ivan
A: 

Found the solution, I had to put the autocomplete inside the addrow function and remove the word "true" from the .clone(true) according (and thanks) to this: http://stackoverflow.com/questions/632212/jquery-how-to-clone-autocomplete-fields

function addrow(destination) {
 rowcount = parseInt(parent_row.attr('id').replace('row_',''))+1;
 clonecopy = destination.clone();
 clonecopy.attr("class","iterable");
 // update numerical suffixes
 clonecopy.attr("id","row_"+rowcount);
 clonecopy.find('.sku, .name').val('');
 clonecopy.find("input[name^='det_sku']").attr({
  "name": "det_sku_"+rowcount,
  "id": "det_sku_"+rowcount
 }).autocomplete({       
  source: "../json/products.php",       
  minLength: 2,
  select: function(event, ui) {
   $(this).parent().siblings().children('input.name').val(ui.item.name);       
  }
 });
 clonecopy.find("input[name^='det_name']").attr({
  "name": "det_name_"+rowcount,
  "id": "det_name_"+rowcount
 });

 clonecopy.find("select[name^='det_quantity']").attr({
  "name": "det_quantity_"+rowcount,
  "id": "det_quantity_"+rowcount
 });
 clonecopy.find("select[name^='det_price']").attr({
  "name": "det_price"+rowcount,
  "id": "det_price"+rowcount
 });
 clonecopy.insertAfter(destination);
 $('#det_arrayitems').val(rowcount);

}
Ivan