I'm generating a TreeView with a Json, the library I'm using is bootstrap-treeview.js.
The structure of the Json that I receive is the following:
data = [{
"Nivel": 0,
"NombrePuesto": "Coordinador"
}, {
"Nivel": 1,
"NombrePuesto": "Encargado"
}, {
"Nivel": 1,
"NombrePuesto": "Jefe"
}, {
"Nivel": 2,
"NombrePuesto": "Analista 1"
}, {
"Nivel": 2,
"NombrePuesto": "Analista 2"
}, {
"Nivel": 1,
"NombrePuesto": "Jefe de Controles"
}, {
"Nivel": 2,
"NombrePuesto": "Encargado 1"
}, {
"Nivel": 1,
"NombrePuesto": "Jefe de Supervision"
}, {
"Nivel": 2,
"NombrePuesto": "Tecnico 1"
}, {
"Nivel": 3,
"NombrePuesto": "Tecnico 2"
}, {
"Nivel": 4,
"NombrePuesto": "Tecnico 3"
}];
And I must convert the json, based on the level of the position to:
var tree = [{
text: "Coordinador",
nodes: [{
text: "Encargado",
},
{
text: "Jefe",
nodes: [{
text: "Analista 1"
},
{
text: "Analista 2"
}
]
},
{
text: "Jefe de Controles",
nodes: [{
text: "Encargado 1"
}]
},
{
text: "Jefe de Supervision",
nodes: [{
text: "Tecnico 1"
},
{
text: "Tecnico 2"
},
{
text: "Tecnico 3"
}
]
}
]
}];
I tried to run the Json with a $ .each but I can not generate the correct structure since I always have symbols of more or less. I read that it was possible to go through the Json and push an array depending on the type of level, but I can not do it.
The query generated by the json has the following structure:
If someone can help me thank you very much
This is the example of how it should look, but fill it by hand not with code.
data = [{
"Nivel": 0,
"NombrePuesto": "Coordinador"
}, {
"Nivel": 1,
"NombrePuesto": "Encargado"
}, {
"Nivel": 1,
"NombrePuesto": "Jefe"
}, {
"Nivel": 2,
"NombrePuesto": "Analista 1"
}, {
"Nivel": 2,
"NombrePuesto": "Analista 2"
}, {
"Nivel": 1,
"NombrePuesto": "Jefe de Controles"
}, {
"Nivel": 2,
"NombrePuesto": "Encargado 1"
}, {
"Nivel": 1,
"NombrePuesto": "Jefe de Supervision"
}, {
"Nivel": 2,
"NombrePuesto": "Tecnico 1"
}, {
"Nivel": 3,
"NombrePuesto": "Tecnico 2"
}, {
"Nivel": 4,
"NombrePuesto": "Tecnico 3"
}];
var tree = [{
text: "Coordinador",
nodes: [{
text: "Encargado",
},
{
text: "Jefe",
nodes: [{
text: "Analista 1"
},
{
text: "Analista 2"
}
]
},
{
text: "Jefe de Controles",
nodes: [{
text: "Encargado 1"
}]
},
{
text: "Jefe de Supervision",
nodes: [{
text: "Tecnico 1"
},
{
text: "Tecnico 2"
},
{
text: "Tecnico 3"
}
]
}
]
}];
/*var tree = '';
$.each(data, function(i, item) {
//console.log(item.Nivel, item.NombrePuesto);
if (item.Nivel == 0) {
tree += '[{text: "' + item.NombrePuesto + '"';
}
if (item.Nivel == 1) {
tree += ', nodes: [{text: "' + item.NombrePuesto + '"}';
}
if (item.Nivel >= 2) {
tree += ',{text: "' + item.NombrePuesto + '"}';
}
});
*/
function cargarTreeView() {
$.each(data, function(i, item) {
});
var $TreeV = $('#treeview').treeview({
data: tree
});
}
$(document).ready(function() {
cargarTreeView();
});
<script src="https://code.jquery.com/jquery-1.9.1.js"></script>
<script src="https://rawgit.com/jonmiles/bootstrap-treeview/master/public/js/bootstrap-treeview.js" type="text/javascript"></script>
<link rel="stylesheet" href="https://rawgit.com/jonmiles/bootstrap-treeview/master/public/css/bootstrap-treeview.css" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<link rel="stylesheet" href="https://rawgit.com/jonmiles/bootstrap-treeview/master/public/css/bootstrap-treeview.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" type="text/javascript"></script>
<div class="col-sm-4">
<div id="treeview" class="treeview">
</div>
</div>