1
I have the following functions:
Grab server data by ajax and save to an object array:
function carregaPesquisaTabela(){
controlLoading();
$.ajax({
url: "header.php",
type: "POST",
dataType:"json",
data: {
'procuraPesquisa' : null
}
}).done(function(data){
pesquisaObj = data;
controlLoading();
});
}
Grabs the Object Array and mounts a table. (I won’t put all the code because it’s not necessary)
function montaTabela(){
$("#tablePesquisas").empty();
var linha = "";
for(i = 0; i<pesquisaObj.length; i++){
//Monta dados e insere em uma tabela
$("#tablePesquisas").html(linha);
}
}
I call them both as follows:
$("#viewPesquisa").submit(function(e){
e.preventDefault();
carregaPesquisaTabela();
montaTabela();
});
The problem is that the action montaTable(); is happening before the function loadPesquisaTable(); is finished, and so the result is not being real.
I saw something about callback but I couldn’t implement it. What to do?
So what I happen is that I wanted my function to chartPesquisaTable(), could be called to fetch the data and another function to treat them, for example, put them in a table or in an option of a select. And avoid having to duplicate the server search code.
– Lucas Junior
Okay, try the fix I made.
– user130117
Great! Exactly what I needed. Working perfectly! Thank you!
– Lucas Junior