2
I have a registration form that has, among others, the following field:
<div id="Aditivo" class="col-xs-12 col-md-2">
@Html.LabelFor(model => model.NumeroAditivo, new { @class = "control-label" })
@Html.TextBoxFor(model => model.NumeroAditivo, new { @class = "form-control no-caps", @ng_readonly = "!m.Editavel" })
</div>
Which should only be displayed when the Template (another field of my Contract form) starts with "Additive".
For this, I created the following javascript function:
function verificarAditivo() {
if ($("#ModeloId_chosen").text().startsWith("Aditivo")) {
$('#Aditivo').show();
$('#Numero').attr('class', 'col-xs-12 col-md-2');
}
else {
$('#Aditivo').hide();
$('#Numero').attr('class', 'col-xs-12 col-md-4');
}
}
The function checks whether the selected Model (Modeloid_chosen) starts with "Additive" to then display or hide the created field.
It turns out that this function should also be called as soon as the page is loaded, so I tried to use:
$(document).ready(verificarAditivo)
and $(window).on("load", function () { verificarAditivo(); }
But both of them didn’t answer because when they called the function, my list of Models...:
<div class="row">
<div class="col-xs-12 col-md-12">
@Html.LabelFor(model => model.ModeloId, new { @class = "control-label" })
@Html.DropDownListFor(model => model.ModeloId, defaultSelect, new
{
@class = "form-control chosen-select",
@data_url = @Url.Action("ListaModelo", "Modelo", new { modeloId = Model.ModeloId }),
@chosen = true,
@ng_options = "o.value as o.label group by o.group for o in options.ModeloId track by o.value",
@onchange = "verificarAditivo(); carregaComplemento('" + @Url.Action("Listar", "Complemento", new { contratoId = Model.ContratoId }) + "?modeloId=' + this.value);",
@ng_readonly = "!m.Editavel",
@ng_model = "v.ModeloId",
@chosen_scope = "m",
@ng_change = "alterarModelo();"
})
</div>
</div>
...It hasn’t been filled out yet. That is, the function is being called too early and there is still no place to check if the Model starts with "Additive".
I could only "solve" the problem using a setTimeout, but I believe this is not the best solution.
Any idea how to call the function I created once the page is ready but with my Templates list already filled?
EDIT1: I also tried with Angular:
$scope.alterarModelo = function () { verificarAditivo(); }
$scope.$on('$viewContentLoaded', function () { verificarAditivo(); });
Hello I suggest you take a look at Promisses this will solve your problem see this video https://www.youtube.com/watch?v=7Bs4-rqbCQc. has this documentation from MDN https://developer.mozilla.org/en-US/Docs/Web/Javascript/Guide/Usando_promises If I had more time I would post the answer, if no one answered, put the night.
– Brendon Iwata
I will take a look yes, if I can not wait for your reply. Thank you.
– Newton Sandey