1
I am developing a quotation form and in it I add and remove lines to register products and in each line there are two autocomplete fields, where search the product by code or by name. The first line is always fixed to start the filling and in it the autocomplete works, whereas the others that are added as many times as the user wants is not working the autocomplete. They don’t 'capture the autocomplete event', they don’t seem to "see in the DOM".
Addition and removal lines code
(function($) {
RemoveTableRow = function(handler) {
var tr = $(handler).closest('tr');
tr.fadeOut(400, function(){
tr.remove();
});
return false;
};
AddTableRow = function() {
var newRow = $("<tr>");
var cols = "";
cols += '<td width="13%"> <div class="form-group"> <div class="input-group"> <div class="input-group-addon"><i class="ti-search"></i></div><input type="text" class="form-control input-sm" name="form_code_product[]" id="form_code_product" placeholder="0002"> </div></div></td>';
cols += '<td width="30%"> <div class="form-group"> <div class="input-group"> <div class="input-group-addon"><i class="ti-search"></i></div><input type="text" class="form-control input-sm" name="form_name_product[]" id="form_name_product" placeholder="Mesa"> </div></div> </td>';
cols += '<td><input type="text" class="form-control input-sm" name="form_price_product" id="form_price_product"> </td>';
cols += '<td><input type="text" class="form-control input-sm" name="form_quantity_product" id="form_quantity_product"> </td>';
cols += '<td><input type="text" class="form-control input-sm" name="form_total_product" id="form_total_product"> </td>';
cols += '<td><input type="text" class="form-control input-sm" name="form_obs_product"> </td>';
cols += '<td class="actions">';
cols += '<button class="btn btn-large btn-danger" onclick="RemoveTableRow(this)" type="button"> <i class="fa fa-close"></i></button>';
cols += '</td>';
newRow.append(cols);
$("#products-table").append(newRow);
return false;
};
})(jQuery);
Code of the autocomplete
$(document).ready(function() {
$("#form_code_product, #form_name_product").autocomplete({
width: 260,
matchContains: true,
selectFirst: false,
appentTo: '#form_register_budget',
source: function(request, response){
$.ajax({
url: "filter/product_code",
type: 'get',
dataType: 'html',
data:{'term' : request.term }
}).done(function( products ){
if( products.length > 0 ){
products = products.split( ',' );
response( $.each( products, function( key, item ){
return({
label: item
});
}));
}
});
}
});
});
thank you very much, it worked perfectly. I had really forgotten the issue of ID so being unique. And thank you for the optimization of the code as well.
– ThiMoreira
I had another problem, because then, for example I type the code and when clicking on the options it fills some fields of this line with the information related to it. Then I need to differentiate each class from the list, because otherwise it will fill in all the same. If I put a counter , incrementing a class will it work? Example: var Count = 1; <class="my-class '+Count+'"> But how would you also reference in the code? I also need to add the subtotals of each line and give total after
– ThiMoreira
would look like this: $('table tbody'). Closest('tr'). find('. form_name_product'). val( product.product_name ); ?
– ThiMoreira
Not so. By clicking on the autocomplete option, you should take this input where the autocomplete value will be inserted. Wait I’ll take a look here.
– Sam
is because I use SELECT so that when clicked on the item it triggers this event: https://pastebin.com/q2Agb1bi
– ThiMoreira
You have to add an event in the Autocomplete options.... I’ll add it in the reply...
– Sam
At the beginning of
ac_opts
you addselect: function(event){
 $(event.target).closest("tr").find(".form_name_product").val("teste");
 },

– Sam
Let’s go continue this discussion in chat.
– Sam
I texted you on chat
– ThiMoreira