0
Talk to me, people. I have a problem with item registration, when you list a series of items and I click add opens a new modal with the characteristics of the item that will be added. However when I click no and return for item listing.
I do this 3 times for example, at last when I finally click Yes to add, it calls my Gravaritem function().
$('#tbl-item-cadastrado tbody').on('click', 'input[type=button]', function () {
var table = $('#tbl-item-cadastrado').DataTable();
var data = table.row($(this).parents('tr')).data();
$('#modal-container-complemento-item-cadastrado').modal('show');
//Preencher modal com informações do item
$('#modal-container-complemento-item-cadastrado #recipient-quantidade').val('');
$('#modal-container-complemento-item-cadastrado #recipient-descricao').val('');
$('#modal-container-complemento-item-cadastrado #recipient-desconto').val(data.PercDescPadrao);
$('#modal-container-complemento-item-cadastrado #recipient-name').text(data.CodigoItem);
$('#modal-container-complemento-item-cadastrado #recipient-descricao').val(data.DescricaoCurta);
$('#modal-container-complemento-item-cadastrado #recipient-descricaoLonga').val(data.DescricaoLonga);
$('#modal-container-complemento-item-cadastrado #recipient-modelo').val(data.Modelo);
$('#modal-container-complemento-item-cadastrado #lote-minimo').val(data.LotMinimo);
$("#modal-container-complemento-item-cadastrado #btn-add-item-cadastrado").bind("click", function (event) {
event.preventDefault();
openLoader();
setTimeout(function () { GravarItem(); }, 3000);
});
function GravarItem() {
var codigoItem = '';
var quantidade = 1;
var descricao = '';
var desconto = 0;
var descontoView = $('#modal-container-complemento-item-cadastrado #recipient-desconto').val();
if (descontoView.indexOf('.') != -1) {
descontoView = descontoView.replace('.', '');
}
if (descontoView.indexOf(',') != -1) {
descontoView = descontoView.replace(',', '.');
}
codigoItem = $('#modal-container-complemento-item-cadastrado #recipient-name').text();
quantidade = $('#modal-container-complemento-item-cadastrado #recipient-quantidade').val() > 0 ? $('#modal-container-complemento-item-cadastrado #recipient-quantidade').val() : 1;
descricao = $('#modal-container-complemento-item-cadastrado #recipient-descricao').val();
desconto = parseFloat(descontoView) > 0 ? parseFloat(descontoView) : 0;
//alert(desconto);
descricaoLonga = $('#modal-container-complemento-item-cadastrado #recipient-descricaoLonga').val();
modelo = $('#modal-container-complemento-item-cadastrado #recipient-modelo').val();
LotMinimo = $('#modal-container-complemento-item-cadastrado #lote-minimo').val();
var item = new ItemCotacaoCadastrado(codigoItem, quantidade, desconto, descricao, descricaoLonga, modelo, 0, LotMinimo, false);
item.PercDescFinal = adicionaFuncionalidadeDescontoNegativoPeloInputRadioSelecionado(item);
//Carlos Pamplona 29/01/2019- Inclui o item no itemCotacao
//alert(item.PercDescFinal);
itemCotacao.push(item);
var clientModel = new CriarViewModel();
if (clientModel.Cotacao.CodigoVendedorExternoCotacao == "Indisponível" || clientModel.Cotacao.CodigoVendedorExternoCriacao == "Indisponível") {
ExibirAlerta("Atenção!", "Precisa selecionar um vendedor externo para continuar.");
closeLoader();
return;
}
var obj = { cliente: clientModel.Cliente, cotacao: clientModel.Cotacao, itemCotacao: clientModel.ItemCotacao };
$.ajax({
type: "POST",
url: '../Cotacao/Criar',
contentType: "application/json; charset=utf-8",
data: JSON.stringify(obj),
success: function (response) {
var obj = JSON.parse(response);
if (obj.HttpStatusCode == "200") {
RedirectTelaEditar(obj.ObjResponse);
}
else {
closeLoader();
ExibirAlerta('Atenção', 'Ocorreu um erro ao cadastrar a cotação - Error Codigo: 01.');
}
},
error: function (request, status, error) {
closeLoader();
ExibirAlerta('Atenção', responseText);
}
});
}
});
I even put a Debugger at the beginning of the ajax function, one at the return of the ajax, and one at my controller method, but the problem is that it enters the JS function 1 time, executes my method in the controller 3 times (the same amount of times I visualized the items in the listing), after he finishes falling into the callback of ajax successfully, in that he add 3 times the same item.
Do not use
async: false,
in AJAX. AJAX shall be asynchronous.– Sam
You must also see how you are opening the modal.
– Sam
Updated the question with the stretch that opens the modal.
– Lucas Franson Barros
as you’re passing an object it should ta storing in memory, try to reset everything when closing the button you open the modal - > 'I click add opens a new modal '.
– Danielle Arruda torres