arrays - Storing hierarchy of tree levels in recursion using javascript -


function formselectedtree(totaldata, selectedids) {     if (!array.isarray(totaldata)) return;     (var item of totaldata) {         if ((!item.children || item.children.length === 0)) {             selectedids.map(function(id, index) {                 if (parseint(item.id) === parseint(id)) {                     item.isselected = true                     item.open = true                 }             })         } else {             this.formselectedtree(item.children, selectedids);         }     }     return totaldata } 

parameters:

  • totaldata-array of objects(find below json)
  • selectedids:array of string(id's of leaf nodes)["1749","1747"]

the above recursive function iterate tree json object. getting id's of leafnodes backend, based on these id's , should select leaf node , expand tree structure parent level.

the selection handled using 'isselected' boolean property expanding of tree nodes handled using 'open' boolean property.

i able set these properties leaf node. facing difficulty set open=true parent/sub-parent levels.

please find below sample json object

{     "data": [{         "id": 1745,         "parentid": null,         "isselected": false,         "open": false,         "children": [{             "id": 1746,             "parentid": 1745,             "isselected": false,             "open": false,             "children": [{                     "id": 1747,                     "parentid": 1746,                     "isselected": false,                     "open": false                 },                 {                     "id": 1748,                     "parentid": 1746,                     "isselected": false,                     "open": false,                     "children": [{                         "id": 1749,                         "parentid": 1748,                         "isselected": false,                         "open": false                     }]                 }             ]         }]     }] } 

can me in achieving difficulty.

thanks

this example adds parentnode property each node encountered, , uses set "open" property of ancestors given selected leaf node, once has been located.

function formselectedtree(totaldata, selectedids, parentnode) {    if (!parentnode) parentnode = false;    if (!array.isarray(totaldata)) return;    (var item of totaldata) {      // set parentnode property each item traverse      item.parentnode = parentnode;      if ((!item.children || item.children.length === 0)) {          selectedids.map(function(id, index) {              if (parseint(item.id) === parseint(id)) {                  item.isselected = true                  item.open = true                                    // since leaf item selected/open, ancestors should open                  var p = item.parentnode;                  while (p) {                    p.open = true;                    // travel through hierarchy until parentnode doesn't exist (top level)                    p = p.parentnode;                  }              }          })      } else {          this.formselectedtree(item.children, selectedids, item);      }    }    return totaldata;  }


Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -