The following code creates a datatable with the data that identifies users and the companies they have worked for.
for (var number = 0; number < users.length; number++) {
var one_row = new Array();
one_row.push("<br>" + userData);
rows_builder.push(one_row);
getUserCompaniesNames(id, number);
}
function getUserCompaniesNames(id, number) {
getUserCompanies(id).done(function(userCompanies) {
var result = JSON.parse(userCompanies);
if (result != null) {
var text = "";
for (var i = 0; i < result.length; i++) {
text += "<u>" + result[i].name_company + "</u>";
}
$('.myclass' + number).append(text);
}
}).fail(function(err) {
console.log(err);
});
}
It works fine except for this: when I use pagination and go from the first page of 10 results to the next, I lose the information generated by the getUserCompaniesNames function.
I think the problem is here:
$('.myclass' + number).append(text);
And the only thing I can think of is to create a global array and add just below:
$('.myclass' + number).append(text);
This:
myGlobalArray.push(text); // declared var myGlobalArray = new Array(); in the first line of my code
But when I check myGlobalArray outside the function, it comes up empty. I understand that the function is taking more time to execute but I don't know how to fix all this and have the information from getUserCompaniesNames in the datatable when using pagination.
Thanks in advance!