I'm kind of lost here! The following piece of code shows what I'm intending to do, basically I have this function insertChilds() that inserts child elements in the DOM in cascade look like (or that's supposed to do...).
The thing here is that I call $.ajax to get the first element childs and in between the function I have to make other ajax call to get the childs of the childs, these could be "n"-childs.
I having trouble because when I make the second ajax call (calling again the same function inside itself) the ajax calls get left behind and the items are shown disordered.
I thank you for some guidance, I dont quite good on programming yk
Code:
function insertChilds(id, after){
var subtar="";
var level = "";
$.ajax({
url: "get_tasktypes.php",
type: "POST",
data: {
id: id
},
dataType: "json",
success: function(json){
// console.log(json);
if(json.length>0){
if(after!= 0){
var levelParts = after.split("-");
level = levelParts.length;
if(level!=0){
var marginLeft = (level*50)+'px;';
}else{
var marginLeft = '50px';
}
}else{
level = 0;
var marginLeft = '50px';
}
subTar = subTar+'<ul class="t-tar-cont" style="margin-left:'+marginLeft+';" >';
for(var i=0; i<json.length; i++){
var tipoTar = "";
var position = after+"-"+i;
for(var h=0; h<json.length; h++){
if(h == i){
var childs = json[h][4];
tipoTar = tipoTar+'<option selected value="'+json[h][0]+'">'+json[h][1]+'</option>';
if(childs == '1'){
//HERE I CALL THE FUNCTION AGAIN, TELL IF THIS IS WRONG...
insertChilds(json[i][0], position);
}
}else{
tipoTar = tipoTar+'<option value="'+json[h][0]+'">'+json[h][1]+'</option>';
}
}
subTar = subTar+'<li class="gdw-tarea-sub-line-cont" id="gdw-tarea-sub-id'+position+'" style="display:none;">';
subTar = subTar+'<div class="gdw-tarea-sub-line" style="width:100%;"><select style="width:90%;" id="gdw-selected-sub'+i+'" name="act'+i+'">'+tipoTar+'</select></div>';
subTar = subTar+'</li>';
}
subTar = subTar+'</ul>';
var afterDivID = '#gdw-tarea-sub-id'+(after);
$(afterDivID).after(subTar);
$('#gdw-tarea-sub-cont').css("height","auto");
var subs = $('li[class="gdw-tarea-sub-line-cont"]');
subs.first().show("fast", "linear", function showNext() {
$(this).next(subs).show("fast", "linear" , showNext);
});
}
}
});
}
Thanks in advance, any correction will be of value to me.