append com jquery

Asked

Viewed 122 times

1

I have a table that opens in a pop up for the material search of a material request registration, on the page (without being the pop up) has another table that when I add a material that was selected in the pop up it copies the information of the material selected in the pop up table to the page table. The problem is that when I add a material for example: steel, it copies the steel data everything ok, but if I do a new search, example: concrete, when I add the concrete, instead of making the append it overwrites the steel, if I add two materials from the same research, ai makes the append properly.

//Pesquisa os materiais
    function Pesquisar() {
        $('.corpoTbl').remove();
        $.ajax({
            url: "/Sip/RCM/ListarMateriais",
            type: "POST",
            data: { nome: $('#Pesquisar').val() },
            datatype: 'json',
            success: function (data) {
                $.each(data, function (I, item) {
                    $('#tbl').append("<tr class=\"corpoTbl\"> <td class=\"ids\">" + item.ID + "</td><td id=\"nome\">" + item.Nome + "</td><td id=\"unidade\"><center>" + item.Unidade +
                        "</center></td><td> <center><input class=\"qtda\" type=\"text\" value=\"0\" style=\"width: 50px;\" /></center> </td><td> <center><img class=\"icon-plus\" src=\"/Sip/Content/Imagens/add.png\" /></center> </td></tr>")
                })
            }
        });
    }

    //adiciona o item na tabela de listagem
    $('#telaMaterial').on('click', '.icon-plus', function (event) {
        var $botao = $(event.target);
        var $tr = $botao.closest('tr');
        var $qtda = $tr.find('.qtda').val();
        var $id = $tr.find('.ids').text();
        if ($qtda > 0 && $qtda != "") {
            $.ajax({
                url: "/Sip/RCM/AddCarrinho",
                type: "POST",
                data: { "qtda": $qtda, "id": $id },
                success: function (data) {
                    alert("Material Adicionado!");
                    $('#tabelaMaterial').append("<tr class=\"corpoTbl\"> <td class=\"idMat\">" + $id + "</td><td>" + $tr.find('#nome').text() + "</td><td><center>" + $tr.find('#unidade').text() +
                    "</center></td><td class=\"qtd\"><center>" + $qtda + "<center></td><td id=\"excluir\"> <center><img class=\"icon-remove\" src=\"/Sip/Content/Imagens/cancel.png\" /></center> </td></tr>")
                }
            })
        }
    })

1 answer

1


When you call Function Pesquisar() the first line is

$('.corpoTbl').remove();

Who ends up removing them all <tr class=\"corpoTbl\"> that you added earlier, I believe removing this line will work properly. :)

  • I put the ID of the wrong table in this remove and I didn’t even notice, vlw.

Browser other questions tagged

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