{
"layer": [
{
"number": "1",
"name": "Layer Name One",
"type": "Core",
"thickGauge": "89",
"layerPerc": "100.00",
"componentTotal": "100.00",
"componenDens": "Infinity",
"componentCost": "",
"component": [
{
"name": "K",
"percent": "100"
}
]
},
{
"number": "2",
"name": "Layer Name Two",
"type": "Core",
"thickGauge": "89",
"layerPerc": "100.00",
"componentTotal": "100.00",
"componenDens": "Infinity",
"componentCost": "",
"component": [
{
"name": "K",
"percent": "100"
}
]
},
{
"number": "3",
"name": "Layer Name Three",
"type": "Core",
"thickGauge": "89",
"layerPerc": "100.00",
"componentTotal": "100.00",
"componenDens": "Infinity",
"componentCost": "",
"component": [
{
"name": "K",
"percent": "100"
}
]
}
]
}
<!doctype html>
<html lang="en">
<head>
<title>JStree</title>
<meta charset="utf-8" />
<!-- JavaScript functions will go here -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css" />
</head>
<body>
<div id="jstree">
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js"></script>
<script>
var data={
"layer": [
{
"state" : {
"opened" : true,
"selected" : true},
"text":"This is data one",
"number": "1",
"name": "Layer Name One",
"type": "Core",
"thickGauge": "89",
"layerPerc": "100.00",
"componentTotal": "100.00",
"componenDens": "Infinity",
"componentCost": "",
"component": [
{
"name": "K",
"percent": "100"
}
]
},
{
"state" : {
"opened" : true,
"selected" : true},
"text":"This is data two",
"number": "2",
"name": "Layer Name Two",
"type": "Core",
"thickGauge": "89",
"layerPerc": "100.00",
"componentTotal": "100.00",
"componenDens": "Infinity",
"componentCost": "",
"component": [
{
"name": "K",
"percent": "100"
}
]
},
{
"state" : {
"opened" : true,
"selected" : true},
"text":"This is data three",
"number": "3",
"name": "Layer Name Three",
"type": "Core",
"thickGauge": "89",
"layerPerc": "100.00",
"componentTotal": "100.00",
"componenDens": "Infinity",
"componentCost": "",
"component": [
{
"name": "K",
"percent": "100"
}
]
}
]
};
$(function () {
$('#jstree').jstree({'core':{
'data':data.layer
}});
});
</script>
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<title>JStree</title>
<meta charset="utf-8" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!-- JavaScript functions will go here -->
<link href="jquery.json-viewer-master/json-viewer/jquery.json-viewer.css" rel="stylesheet" type="text/css"/>
<script src="jquery.json-viewer-master/json-viewer/jquery.json-viewer.js" type="text/javascript"></script>
</head>
<body>
<pre id="json-renderer"></pre>
<script>
var data={
"layer": [
{
"number": "1",
"name": "Layer Name One",
"type": "Core",
"thickGauge": "89",
"layerPerc": "100.00",
"componentTotal": "100.00",
"componenDens": "Infinity",
"componentCost": "",
"component": [
{
"name": "K",
"percent": "100"
}
]
},
{
"number": "2",
"name": "Layer Name Two",
"type": "Core",
"thickGauge": "89",
"layerPerc": "100.00",
"componentTotal": "100.00",
"componenDens": "Infinity",
"componentCost": "",
"component": [
{
"name": "K",
"percent": "100"
}
]
},
{
"number": "3",
"name": "Layer Name Three",
"type": "Core",
"thickGauge": "89",
"layerPerc": "100.00",
"componentTotal": "100.00",
"componenDens": "Infinity",
"componentCost": "",
"component": [
{
"name": "K",
"percent": "100"
}
]
}
]
};
$('#json-renderer').jsonViewer(data);
</script>
</body>
</html>
var newTemplate = {
"Id": "",
"parent": "",
"text": ""
};
function BuildJSONStructure (jObjS) {
var NewJSObj = newTemplate;
var retObj = [];
var alpha= ['a','b','c','d','e','f','g', 'h', 'i', 'j', 'k', 'l', 'm', 'n'];
var alphaPtr;
jObj = JSON.parse(jObjS.layer);
for (var lPtr=0; lPtr<jObj.layer.length; lPtr++)
{
NewJSObj = newTemplate;
alphaPtr = 0;
//Layer Name
NewJSObj.Id="ajson"+ String(lPtr+1);
NewJSObj.parent = "ajson0";
NewJSObj.text = "Layer" + String(lPtr+1);
retObj.push(NewJSObj);
//Layer Type
NewJSObj = newTemplate;
NewJSObj.Id="ajson" + String(lPtr+1) + alpha[alphaPtr];
alphaPtr=alphaPtr + 1;
NewJSObj.parent = "ajson" + String(lPtr);
NewJSObj.text = jObj.layer[lPtr].type;
retObj.push(NewJSObj);
//Layer Thickess
NewJSObj = newTemplate;
NewJSObj.Id="ajson" + String(lPtr+1) + alpha[alphaPtr];
alphaPtr=alphaPtr + 1;
NewJSObj.parent = "ajson" + String(lPtr);
NewJSObj.text = jObj.layer[lPtr].thickGauge;
retObj.push(NewJSObj);
//layerPerc
NewJSObj = newTemplate;
NewJSObj.Id="ajson" + String(lPtr+1) + alpha[alphaPtr];
alphaPtr=alphaPtr + 1;
NewJSObj.parent = "ajson" + String(lPtr);
NewJSObj.text = jObj.layer[lPtr].layerPerc;
retObj.push(NewJSObj);
var alphaPtr2 = 0;
for (var cPtr=cPtr=0; cPtr<jObj.layer[cPtr].component.length; cPtr++) {
//Component Name
NewJSObj = newTemplate;
NewJSObj.Id="ajson" + String(lPtr+1) + alpha[alphaPtr] + alpha[alphaPtr2] + String(cPtr + 1);
alphaPtr2=alphaPtr2 + 1;
NewJSObj.parent = "ajson" + String(lPtr+1) + alpha[alphaPtr];
NewJSObj.text = jObj.layer[lPtr].component[cPtr].name;
retObj.push(NewJSObj);
//Component Percentage
NewJSObj = newTemplate;
NewJSObj.Id="ajson" + String(lPtr+1) + alpha[alphaPtr] + alpha[alphaPtr2] + String(cPtr + 1);
alphaPtr2=alphaPtr2 + 1;
NewJSObj.parent = "ajson" + String(lPtr+1) + alpha[alphaPtr];
NewJSObj.text = jObj.layer[lPtr].component[cPtr].percent;
retObj.push(NewJSObj);
}
}
return JSON.stringify(retObj);
}
<!doctype html>
<html lang="en">
<head>
<title>JStree</title>
<meta charset="utf-8" />
<!-- JavaScript functions will go here -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css" />
</head>
<body>
<div id="jstree_1"></div>
<div id="jstree_2"></div>
<div id="jstree_3"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js"></script>
<script>
var data={
"layer": [
{
"number": "1",
"name": "Layer Name One",
"type": "Core",
"thickGauge": "89",
"layerPerc": "100.00",
"componentTotal": "100.00",
"componenDens": "Infinity",
"componentCost": "",
"component": [
{
"name": "K",
"percent": "100"
}
]
},
{
"number": "2",
"name": "Layer Name Two",
"type": "Core",
"thickGauge": "89",
"layerPerc": "100.00",
"componentTotal": "100.00",
"componenDens": "Infinity",
"componentCost": "",
"component": [
{
"name": "K",
"percent": "100"
}
]
},
{
"number": "3",
"name": "Layer Name Three",
"type": "Core",
"thickGauge": "89",
"layerPerc": "100.00",
"componentTotal": "100.00",
"componenDens": "Infinity",
"componentCost": "",
"component": [
{
"name": "K",
"percent": "100"
}
]
}
]
};
$('#jstree_1').jstree({'core':{
'data':{
text : "Data 1", // node text
icon : "string", // string for custom
state : {
opened : true, // is the node open
disabled : true, // is the node disabled
selected : true, // is the node selected
},
children : [{text:'number: '+data.layer[0].number},{text:'name: '+data.layer[0].name},{text:'type: '+data.layer[0].type},{text:'thickGauge: '+data.layer[0].thickGauge},{text:'layerPerc: '+data.layer[0].layerPerc},
{text:'componentTotal: '+data.layer[0].componentTotal},{text:'componenDens: '+data.layer[0].componenDens},{text:'componentCost: '+data.layer[0].componentCost},{text:'component: '+data.layer[0].component[0].name},{text:'percent: '+data.layer[0].component[0].percent}], // array of strings or objects
li_attr : {}, // attributes for the generated LI node
a_attr : {}, // attributes for the generated A node
}
}});
$('#jstree_2').jstree({'core':{
'data':{
text : "Data 2", // node text
icon : "string", // string for custom
state : {
opened : true, // is the node open
disabled : true, // is the node disabled
selected : true, // is the node selected
},
children : [{text:'number: '+data.layer[1].number},{text:'name: '+data.layer[1].name},{text:'type: '+data.layer[1].type},{text:'thickGauge: '+data.layer[1].thickGauge},{text:'layerPerc: '+data.layer[1].layerPerc},
{text:'componentTotal: '+data.layer[1].componentTotal},{text:'componenDens: '+data.layer[1].componenDens},{text:'componentCost: '+data.layer[1].componentCost},{text:'component: '+data.layer[1].component[0].name},{text:'component: '+data.layer[1].component[0].percent}], // array of strings or objects
li_attr : {}, // attributes for the generated LI node
a_attr : {}, // attributes for the generated A node
}
}});
$('#jstree_3').jstree({'core':{
'data':{
text : "Data 3", // node text
icon : "string", // string for custom
state : {
opened : true, // is the node open
disabled : true, // is the node disabled
selected : true, // is the node selected
},
children : [{text:'number: '+data.layer[2].number},{text:'name: '+data.layer[2].name},{text:'type: '+data.layer[2].type},{text:'thickGauge: '+data.layer[2].thickGauge},{text:'layerPerc: '+data.layer[2].layerPerc},
{text:'componentTotal: '+data.layer[1].componentTotal},{text:'componenDens: '+data.layer[2].componenDens},{text:'componentCost: '+data.layer[2].componentCost},{text:'component: '+data.layer[2].component[0].name},{text:'component: '+data.layer[2].component[0].percent}], // array of strings or objects
li_attr : {}, // attributes for the generated LI node
a_attr : {}, // attributes for the generated A node
}
}});
</script>
</body>
</html>
(function createTree(){
var i=0;
var dataLength=data.layer.length
for(i;i<=dataLength;i++){
$('#jstree_'+[i+1]).jstree({'core':{
'data':{
text : "Data "+[i+1], // node text
icon : "string", // string for custom
state : {
opened : false, // is the node open
disabled : true, // is the node disabled
selected : true, // is the node selected
},
children : [{text:'number: '+data.layer[i].number},{text:'name: '+data.layer[i].name},{text:'type: '+data.layer[i].type},{text:'thickGauge: '+data.layer[i].thickGauge},{text:'layerPerc: '+data.layer[i].layerPerc},
{text:'componentTotal: '+data.layer[i].componentTotal},{text:'componenDens: '+data.layer[i].componenDens},{text:'componentCost: '+data.layer[i].componentCost},{text:'component: '+data.layer[i].component[0].name},{text:'percent: '+data.layer[i].component[0].percent}], // array of strings or objects
li_attr : {}, // attributes for the generated LI node
a_attr : {}, // attributes for the generated A node
}
}});
}
})();
icon     : "string", // string for custom.You an set your icon via directory path eg
icon : "/directory_path/icon.png", // string for custom
(function createTree(){
var i=0;
var dataLength=data.layer.length
for(i;i<=dataLength;i++){
$('#jstree_'+[i+1]).jstree({'core':{
'data':{
text : "Data "+[i+1], // node text
icon : "string", // string for custom
state : {
opened : false, // is the node open
disabled : true, // is the node disabled
selected : true, // is the node selected
},
children : [{text:'number: '+data.layer[i].number},{text:'name: '+data.layer[i].name},{text:'type: '+data.layer[i].type},{text:'thickGauge: '+data.layer[i].thickGauge},{text:'layerPerc: '+data.layer[i].layerPerc},
{text:'componentTotal: '+data.layer[i].componentTotal},{text:'componenDens: '+data.layer[i].componenDens},{text:'componentCost: '+data.layer[i].componentCost},{text:'component: '+data.layer[i].component[0].name},{text:'percent: '+data.layer[i].component[0].percent}], // array of strings or objects
li_attr : {}, // attributes for the generated LI node
a_attr : {}, // attributes for the generated A node
}
}});
}
})();
function CreateTree(data) {
//same code
};
var data={
"layer": [
{
"number": "1",
"name": "Layer Name One",
"type": "Core",
"thickGauge": "323",
"layerPerc": "100.00",
"componentTotal": "100.00",
"componenDens": "Infinity",
"componentCost": "",
"component": [
{
"name": "K",
"percent": "100"
}
]
},
{
"number": "2",
"name": "Layer Name Two",
"type": "Core",
"thickGauge": "3213",
"layerPerc": "100.00",
"componentTotal": "100.00",
"componenDens": "Infinity",
"componentCost": "",
"component": [
{
"name": "L",
"percent": "200"
}
]
},
{
"number": "3",
"name": "Layer Name Three",
"type": "Core",
"thickGauge": "12333",
"layerPerc": "100.00",
"componentTotal": "100.00",
"componenDens": "Infinity",
"componentCost": "",
"component": [
{
"name": "M",
"percent": "300"
}
]
}
]
};
//I create a function that at the end it rerurns an array of objects to insert it into children
//option inside the jsTree function
function childrenInpupt(arg){
//First I create two var an array (keyD) that it returns this function and an object
//The object is going to have this format because children option (in jsTree function)
//has this format {text:some string here to show the data of JSON}
var keyD=[];
var obj={};
//Then I create a dynamic var that takes the lenght of each elements inside the JSON
//See that I put the [arg] (argument).This is going to set values from the IFEE
//function below.It measures the lenght of the JSON.In this example we have 3 elements (containers)
//of data.
var dataElmLenght=Object.keys(data.layer[arg]).length;
//Via for loop I get the keys of each object inside the data.layer
for(var x=0;x<dataElmLenght;x++){
var key=Object.keys(data.layer[arg])[x];
//I check of the each key has value string or object.If it is an object the via if
//condition I get the keys of this second object like component
if((typeof data.layer[arg][key])==='object'){
var newObj=data.layer[arg][key];
var lenghtObj=newObj.length;
//Via for loop again I push the obj inside the keyD array
for(var y=0;y<=lenghtObj;y++){
var newKey=Object.keys(newObj[0]);
obj={text:key+" : "+newKey[y]+" = "+newObj[0][newKey[y]]};
keyD.push(obj);
}
}else{
obj={text:key+" : "+data.layer[arg][key]};
keyD.push(obj);
}
}
//And finally i return the keyD array to use it in the jsTree function
return keyD;
}
(function (){
var i=0;
var dataLength=data.layer.length;
for(i;i<dataLength;i++){
$('#jstree_'+[i+1]).jstree({'core':{
'data':{
text : "Data "+[i+1], // node text
state : {
opened : false, // is the node open
disabled : true, // is the node disabled
selected : true // is the node selected
},
children : childrenInpupt(i), // array of strings or objects
li_attr : {}, // attributes for the generated LI node
a_attr : {} // attributes for the generated A node
}
}});
}
})();
function someFunction(){
//code here
}
someFunction();
function (rowNumber, LayerForRowJSONObj){
var data; //do I need this?
var i=0;
data = LayerForRowJSONObj;
var dataLength=data.layer.length;
for(i;i<dataLength;i++){
$('#jstree_'+ rowNumber).jstree({'core':{
'data':{
text : "Data "+[i+1], // node text
state : {
opened : false, // is the node open
disabled : true, // is the node disabled
selected : true // is the node selected
},
children : childrenInpupt(i), // array of strings or objects
li_attr : {}, // attributes for the generated LI node
a_attr : {} // attributes for the generated A node
}
}});
}
}