Site Search:

How to dynamically change nodes for Treant.js in 5 minutes

Back>

Treant.js allows you to create good presentation of tree data structure. It will be more useful if you can dynamically change the tree structure.

Here is a solution that works.
You first need to create the json string (you can iterate an array then create the json string), then change the string to an object.
var obj = JSON.parse(myString);

Finally you attach the new object, thus dynamically changed the tree presentation.
 simple_chart_config.nodeStructure = obj; 

Example


<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/raphael/2.2.7/raphael.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/treant-js/1.0/Treant.js"></script>

<style>
/* required LIB STYLES */
/* .Treant se automatski dodaje na svaki chart conatiner */
.Treant { position: relative; overflow: hidden; padding: 0 !important; }
.Treant > .node,
.Treant > .pseudo { position: absolute; display: block; visibility: hidden; }
.Treant.Treant-loaded .node,
.Treant.Treant-loaded .pseudo { visibility: visible; }
.Treant > .pseudo { width: 0; height: 0; border: none; padding: 0; }
.Treant .collapse-switch { width: 3px; height: 3px; display: block; border: 1px solid black; position: absolute; top: 1px; right: 1px; cursor: pointer; }
.Treant .collapsed .collapse-switch { background-color: #868DEE; }
.Treant > .node img { border: none; float: left; }
</style>

<style>

/* optional Container STYLES */
.chart { height: 159px; width: 332px; margin: 5px; margin: 5px auto; border: 3px solid #DDD; border-radius: 3px; }
.node { color: #9CB5ED; border: 2px solid #C8C8C8; border-radius: 3px; }
.node p { font-size: 20px; line-height: 20px; height: 20px; font-weight: bold; padding: 3px; margin: 0; }
</style>

<br />
<div class="chart" id="OrganiseChart-simple">
</div>
<br/>
<p id="demo"></p>
<button onclick="myFunction()">Click me</button>
<p id="changeNode"></p>

<script>
var simple_chart_config = {
 chart: {
  container: "#OrganiseChart-simple"
 },

 nodeStructure: {
  text: { name: "Parent node" },
  children: [
   {
    text: { name: "First child" }
   }
  ]
 }
};
</script>

<script>
  var my_chart = new Treant( simple_chart_config );
  document.getElementById("demo").innerHTML = simple_chart_config.nodeStructure.text.name;
</script>

<script>
function myFunction() {
  document.getElementById("changeNode").innerHTML = "Hello World";
  //simple_chart_config.nodeStructure.text.name = 'changed';

/*
  simple_chart_config.nodeStructure = {
  text: { name: "Parent node" },
  children: [
   {
    text: { name: "First child" }
   },
   {
    text: { name: "Second child" }
   }
  ]
 };
*/ 
 
  var myString = '{"text": { "name": "Parent nodexxx" },"children": [{"text": { "name": "First childxx" }},{"text": { "name": "Second childxx" }}]}';
 
  var obj = JSON.parse(myString);

  simple_chart_config.nodeStructure = obj; 
  my_chart.destroy()
  my_chart = new Treant(simple_chart_config, null, $);
}
</script>