Look at the image below;
Thishappenswhenloadingthecomboofcategoriestoloadthecomboofsubcategories.
Whatisthegoal?
WhenselectingthecategorycomboitwillautomaticallyloadthesubcategorycombobecauseofaJavascriptfile,howeveritisgeneratinganerrorintheJavascriptfileonline55.
Line55ofmyJavaScriptfileisbelow;
ThisprojectisaboutSpringBoot,Iunderstandabitofspringboot,butIknowverylittleaboutJavaScript,Ikindofgotacodereadyandtriedtoadaptit.
HereisthecompleteJavaScriptcode;
varArm=Arm||{};Arm.ComboCategoria=(function(){functionComboCategoria(){this.combo=$('#categoria');this.emitter=$({});this.on=this.emitter.on.bind(this.emitter);}ComboCategoria.prototype.iniciar=function(){this.combo.on('change',onCategoriaAlterada.bind(this));}functiononCategoriaAlterada(){this.emitter.trigger('alterado',this.combo.val());}returnComboCategoria;}());Arm.ComboSubCategoria=(function(){functionComboSubCategoria(comboCategoria){this.comboCategoria=comboCategoria;this.combo=$('#subcategoria');this.imgloading=$('.js-img-loading');}ComboSubCategoria.prototype.iniciar=function(){reset.call(this);this.comboCategoria.on('alterado',onCategoriaAlterada.bind(this));}functiononCategoriaAlterada(evento,codigoCategoria){//console.log('codigodacategória',codigoCategoria);if(codigoCategoria){varresposta=$.ajax({url:this.combo.data('url'),method:'GET',contentType:'application/json',data:{'categoria':codigoCategoria},beforeSend:iniciarRequisicao.bind(this),complete:finalizarRequisicao.bind(this)});resposta.done(onBuscarSubCategoriasFinalizado.bind(this));}else{reset.call(this);}}functiononBuscarSubCategoriasFinalizado(subcategorias){varoptions=[];subcategorias.forEach(function(subcategoria){options.push('<optionvalue"' + subcategoria.codigo + '">' + subcategoria.nome + '</option>');
});
this.combo.html(options.join(''));
this.combo.removeAttr('disabled');
var codigoSubCategoriaSelecionada = this.inputHiddenSubCategoriaSelecionada.val();
if (codigoSubCategoriaSelecionada) {
this.combo.val(codigoSubCategoriaSelecionada);
}
}
function reset() {
this.combo.html('<option value="">Selecione a SubCategoria</option>');
this.combo.val('');
this.combo.attr('disabled', 'disabled');
}
function iniciarRequisicao() {
reset.call(this);
this.imgloading.show();
}
function finalizarRequisicao() {
this.imgloading.hide();
}
return ComboSubCategoria;
}());
$(function(){
var comboCategoria = new Arm.ComboCategoria();
comboCategoria.iniciar();
var comboSubCategoria = new Arm.ComboSubCategoria(comboCategoria);
comboSubCategoria.iniciar();
});
I accept all the suggestions and beg for help.
Any questions left my repository down for review.
This is my repository CLICK HERE