0
Look at the image below;
This happens when loading the category combo to load the subcategory combo.
What is the goal?
When selecting the category combo it will load the subcategory combo automatically because of a Java script file, but is generating an error in the Javascript file on line 55.
The line 55 of my Javascript file is this below;
This project is about Spring Boot, I understand a little about spring boot, but I know very little about Javascript, I kind of took a code ready and tried to adapt it.
Here is the complete Javascript code;
var Arm = Arm || {};
Arm.ComboCategoria = (function () {
function ComboCategoria(){
this.combo = $('#categoria');
this.emitter = $({});
this.on = this.emitter.on.bind(this.emitter);
}
ComboCategoria.prototype.iniciar = function () {
this.combo.on('change', onCategoriaAlterada.bind(this));
}
function onCategoriaAlterada() {
this.emitter.trigger('alterado', this.combo.val());
}
return ComboCategoria;
}());
Arm.ComboSubCategoria = (function() {
function ComboSubCategoria(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));
}
function onCategoriaAlterada(evento, codigoCategoria) {
// console.log('codigo da categória ', codigoCategoria);
if(codigoCategoria){
var resposta = $.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);
}
}
function onBuscarSubCategoriasFinalizado(subcategorias) {
var options = [];
subcategorias.forEach(function(subcategoria){
options.push('<option value"' + 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 suggestions and beg for help.
any doubt I left my repository down to analyze.
This is my repository CLICK HERE
you don’t need to know much about Spring Boot to solve this problem, which is really necessary and understand Javascript, even with your example could not help much, but still thank you
– wladyband
What problem you found, my example is in pure js, I have used in various contexts, always successfully. 'Study' my example, redo it in codepen (it’s free) or jsfidle, if you prefer, with your data and I’m sure you will be able.
– Sidon
The solution came from where you couldn’t even imagine! I made a small change in my project and it solved my problem, as I had mentioned before I did the project with Spring Boot and in it I use to show the information contained in the bank the Thymeleaf, what exactly I did was update Thymeleaf to the new version and include the Thymeleaf Extra artifact, and staying with the same Javascript code it worked. weird right?
– wladyband