I'm having problems with the data I'm fetching from the database, when I change it in my table.
HTML
<table class="table table-striped table-bordered" id="vendaTabela" width="100%">
<thead>
<tr>
<th data-class="expand">Cód.</th>
<th data-hide="phone">Designação</th>
<th data-hide="phone">Qnt.</th>
<th data-hide="phone,tablet">Uni.</th>
<th>Preço</th>
<th>Desconto</th>
<th>IVA</th>
<th>Sub-Total</th>
</tr>
</thead>
<tbody>
<tr id="tr0">
<td class="codigo" contenteditable="true" onblur="codArtigo ( )"></td>
<td id="ac" class="designacao autoC" contenteditable="true" onblur="nomeArtigo()"></td>
<td class="quantidade" contenteditable="true" onblur="subtotal ( )"></td>
<td class="unidade" contenteditable="false"></td>
<td class="preco" contenteditable="true"></td>
<td class="desconto" contenteditable="true"></td>
<td class="iva" contenteditable="false"></td>
<td class="total" contenteditable="false"></td>
</tr>
</tbody>
</table>
JQUERY
//PREENCHER TABELA COM OS DADOS DO ARTIGO A PARTIR DO SEU CÓDIGO
codArtigo = function () {
var $linhas = $("#vendaTabela tbody > tr");
var val = $('#entidade').val();
var xyz = $('#list option').filter(function () {
return this.value == val;
}).data('xyz');
var idEntidade = xyz ? '' + xyz : 'No Match';
$linhas.each(function () {
var designacao = $(".designacao", this);
var codigo = $(".codigo", this);
var unidade = $(".unidade", this);
var iva = $(".iva", this);
var qtd = $(".quantidade", this);
var preco = $(".preco", this);
var precoArt = $(".preco", this);
var desconto = $(".desconto", this);
var subTotal = $(".total", this);
myJSRoutes.controllers.ArtigoController.getArtigobyEntidadeId(idEntidade, codigo.html()).ajax({
success: function (data) {
if (data.length > 0) {
myJSRoutes.controllers.ArtigoController.getArtigobyId(codigo.html()).ajax({
success: function (data) {
if (data.length > 0) {
$.each(data, function (key, value) {
designacao.html(value.nome);
unidade.html(value.unidade.simbolo);
iva.html(value.iva.valor + " %");
});
}
}
})
$.each(data, function (key, value) {
preco.html(value.preco.toFixed(2) + " €");
});
} else {
myJSRoutes.controllers.ArtigoController.getArtigobyId(codigo.html()).ajax({
success: function (data) {
if (data.length > 0) {
$.each(data, function (key, value) {
designacao.html(value.nome);
unidade.html(value.unidade.simbolo);
iva.html(value.iva.valor + " %");
precoArt.html(value.preco.toFixed(2) + " €");
});
} else {
alert("Artigo Desconhecido");
codigo.text("");
designacao.text("");
qtd.text("");
preco.text("");
precoArt.text("");
unidade.text("");
iva.text("");
subTotal.text("");
desconto.text("");
}
}
})
}
}
})
})
}
What happens is the following:
-I'll get an article for DB (code: 1, price: 12); -After inserting it in the first line and changing its price that came from DB (12), to 15 for example, when I insert another line and re-insert the same article, what comes from DB is (price: 12, and well ), but it changes the value of the first line from 15 to 12.
Why do I change the rest of the lines in the same article to the original DB value if I am referencing the line? var preco = $(".preco",this)
?
PS: I put an identical question and it was problem in the declaration of variables, as the question was already extensive I decided to put another.