1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| function convertToHierarchy(arry)
{
var item, path;
var children = {};
var hasParent = {};
//Liste les utilisateurs
for (var i = 0; i < arry.length; i++)
{
var path = arry[i];
var parent = null;
var manager = path[0];
var account = path[1];
var title = path[2];
if (!children[manager]) {
if (manager != "")
{
children[manager] = {};
}
}
/* if (parent) {
children[parent][manager] = true;
hasParent[manager] = true;
}*/
parent = manager;
/* if (account != "")
{
children[account] = {};
children[account].title = title;
}*/
//children[account] = {title: title};
if (parent) {
children[parent][account] = true;
hasParent[account] = true;
//children[account] = {};
children[parent][account].title = title;
}
}
/* sResults = JSON.stringify(children, null, 2);
alert(sResults);*/
//Construction de la hierarchie
var result = {id:"", title:"", items:[]};
for (item in children) {
if (!hasParent[item]) {
result.id = buildNodeRecursive(item, children, item.title).id;
result.title = buildNodeRecursive(item, children, item.title).title;
result.items.push(buildNodeRecursive(item, children, item.title));
}
}
return result;
}
function buildNodeRecursive(item, children, title)
{
var node = {id:item, title:title, items:[]};
for (var child in children[item]) {
node.items.push(buildNodeRecursive(child, children, title));
}
return node;
}
sResults = JSON.stringify(convertToHierarchy([["C.HOGAN","H.VIRON", "Henry VIRON"], ["K.TOIF", "G.DURANT", "Gerard DURANT"], ["", "C.HOGAN", "Charles HOGAN"], ["C.HOGAN","K.TOIF", "Karim TOIF"], ["G.DURANT","T.MENES", "Thierry MENES"]]), null, 2);
alert(sResults); |
Partager