1
Hello! I’m doing a simple program for managing my beers and sales.
In the order view, I was able to add a new line to include a new item for the order.
Fields: id_item(select) | id_lote_item(select) | quantity (text) | unit value(text) | subtotal(text)
jquery code adding fields:
var campos_max = 10; //max de 10 campos
var x = 1; // campos iniciais
$('#add_field').click(function (Event) {
Event.preventDefault(); //prevenir novos clicks
if (x < campos_max) {
$('#listas').append('\
<div id="linha">\n\
<div class="col-sm-3">\n\
<div class="form-group">\n\
<label for="exampleInputEmail1">Cerveja</label>\n\
<select name="id_item_estoque_pedido[]" id="id_item" class="select2 form-control">\n\
<option>Selecione</option>\n\
</select>\n\
</div>\n\
</div>\n\
<div class="col-sm-3">\n\
<div class="form-group">\n\
<label for="exampleInputEmail1">Lote</label>\n\
<select name="id_lote_pedido[]" id="id_lote_item" class="select2 form-control">\n\
<option>Selecione o lote</option>\n\
</select>\n\
</div>\n\
</div>\n\
<div class="col-sm-2">\n\
<div class="form-group">\n\
<label for="exampleInputEmail1">Qtd. Pedido</label>\n\
<input type="text" name="quantidade_item_pedido[]" id="quantidade_item_pedido" class="form-control">\n\
</div>\n\
</div>\n\
<div class="col-sm-2">\n\
<div class="form-group">\n\
<label for="exampleInputEmail1">valor Unitario</label>\n\
<input type="text" name="valor_unitatio_item_pedido[]" id="valor_unitatio_item_pedido" class="form-control">\n\
</div>\n\
</div>\n\
<div class="col-sm-2"><div class="form-group">\n\
<label for="exampleInputEmail1">Subtotal</label>\n\
<input type="text" id="subtotal_item_pedido" class="form-control" readonly="true">\n\
</div>\n\
</div>'
);
x++;
}
});
So far so good... my doubt comes now... The id_lote_item field is changed to display batches of the selected item.
jquery code:
$('#id_item').change(function (e) {
var item = $('#id_item').val();
var base_url = '<?php echo base_url(); ?>';
$.getJSON(base_url + 'pedido/GetLoteByIdItem/' + item, function (dados) {
if (dados.length > 0) {
var option = '<option>Selecione o lote</option>';
$.each(dados, function (i, obj) {
option += '<option value="' + obj.id_lote + '">' + obj.nome_lote + ' - ' + 'Qtd: (' + obj.quantidade_garrafas_lote + ')</option>';
})
} else {
option = '<option>Selecione o lote</option>';
}
$('#id_lote_item').html(option).show();
});
});
I managed to make for only one line. Now with this dynamic lines scheme I couldn’t even stick to.
Thanks for the help!
In the HTML you are generating there are more than 1
#id_item
? I mean, you’re adding multiple times the HTML you have in the question is that? and it only works for the right first?– Sergio
@Sergio, when I call the view for the first time, fields are not created, only when I click a button (#add_field). In this case there would be several lines with the same fields, understood?
– FabricioCruzCasarini
Okay, and
$('#id_item').change(function (e) {
works for 1 field, but not for the right next?– Sergio
not.... I was able to search for lots of the item just by writing the line where it would be. Now, when I added the function to add lines with jquery I couldn’t even do it for the primaire
– FabricioCruzCasarini