Skip to content

Instantly share code, notes, and snippets.

@jorgesanabria
Created November 20, 2016 23:51
Show Gist options
  • Select an option

  • Save jorgesanabria/4bb9feb7e3b3b4e6947cc6d0e8f3f46e to your computer and use it in GitHub Desktop.

Select an option

Save jorgesanabria/4bb9feb7e3b3b4e6947cc6d0e8f3f46e to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<title>Arbol</title>
</head>
<body>
<div id="arbol">
<ul></ul>
</div>
<script type="text/javascript" src="jquery-3.1.1.js"></script>
<script type="text/javascript">
var arbol = [
{"nombre": "asasa",
"hijos": []
},
{"nombre": "asasa",
"hijos": [
{"nombre": "asasas",
"hijos": [
{"nombre": "asasaa",
"hijos": []
}
]
},
{"nombre": "asasa",
"hijos": []
},
{"nombre": "asasag",
"hijos": [
{"nombre": "asasah",
"hijos": [
{"nombre": "asasahd",
"hijos": [
{"nombre": "asasahaaaaaaaaaaaaaaaaaaaaa",
"hijos": []
}]
}]
}
]
}
]
},
{"nombre": "asasa",
"hijos": [
{"nombre": "asasab",
"hijos": []
}
]
},
{"nombre": "asasa",
"hijos": []
},
{"nombre": "asasa",
"hijos": [{"nombre": "asasahaaaaaaaaaaaaaaaaaaaaa",
"hijos": []
}]
}
];
function crearArbol (data, $contenedor)
{
$.each(data, function (i, nodo) {
var $li = $('<li>');
var $div = $('<div>', {"html":nodo.nombre});
$li.append($div);
if (nodo.hijos.length > 0) {
var $ul = $('<ul>');
crearArbol(nodo.hijos, $ul);
$li.append($ul);
}
$contenedor.append($li);
});
}
crearArbol(arbol, $("#arbol > ul"));
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment