Change values from BD in a table

Asked

Viewed 352 times

0

I’m having problems with the data I’m going to retrieve from the database, this 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("");
                            }
                        }
                    })
                }
            }
        })
    })
}

Here’s what’s going on:

-I’ll get an article from BD (code:1, price: 12); -After inserting it in the first row and changing its price that came from the BD (12), for example, when I insert another line and insert again the same article, what comes from the BD is (price:12, and well), but changes the value of the first row from 15 to 12.

Why do I change the rest of the lines of the same article to the original comic book value if I’m referring to the line? var preco = $(".preco",this) ?

PS: I posed an identical question and it was problem in the declaration of the variables, as the question was already extensive I decided to put another.

  • Improve code formatting because it’s hard to understand and post HTML too, please

  • In fact it was badly formatted but so with the formatting puts that scroll horizontal.

  • 1

    I believe that’s why: var $linhas = $("#vendaTabela tbody > tr");, is taking all the rows of the table and going through #.each just below; I don’t know for sure, I can’t test it; If you want to ... you can change your events to: http://api.jquery.com/on/; $(document).on('blur', 'table#vendaTabela > .codigo', function(e) {$linha = $(this).closest('tr'); //RESTO });

  • If you want an example just ask @Hugomachado;

  • Thank you I will test and I see :)

  • That way I’ll get the line, but if you want to change a column of that row as I do ?

  • It is perfectly functional, at least it hasn’t given problems yet. Thanks @Rafaelwithoeft ;)

  • @Hugomachado He was having lunch, he managed to solve his problem in the proposed way?

  • Yes yes, thank you very much @Rafaelwithoeft ;)

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.