arrays - To Traverse from child to parent in tree like json object in javascript -


i have tree json object selection of parent selects sub-parent , leaf levels.

but de-selection of sub-parent or leaf should de-select parent also.

{ "name": "locations", "id": 1, "children": [{     "name": "ap",     "children": [{             "name": "hyd",             "children": [{                     "name": "hitech",                     "children": [],                     "parentid": "167",                     "isselected": true,                     "id": "0loc_0loc_0"                 },                 {                     "name": "madhapur",                     "children": [],                     "parentid": "167",                     "isselected": true,                     "id": "1loc_0loc_0"                 }             ],             "isselected": false,             "id": "0loc_1",             "parentid": "25",             "itemid": "167"         },         {             "name": "wng",             "children": [{                 "name": "nit",                 "children": [],                 "parentid": "165",                 "isselected": false,                 "id": "0loc_0loc_1"             }],             "isselected": false,             "id": "0loc_1",             "parentid": "25",             "itemid": "165"         }     ],     "isselected": false,     "id": "-100000_loc",     "itemid": "25" }] 

}

when node selected , making child nodes selected isselected=true function called when location selected/de-selected tree

checkboxgroupcheck(selectedlocatinsobject) {     let selectedvaluearr = [];     let updatedtotallocationslist = totallocations.tojs();   //the total locations json     var flagid = selectedlocatinsobject.get("id");     var parentid=selectedlocatinsobject.get("parentid")     var flag = !selectedlocatinsobject.get("isselected");     selectedvaluearr.push({       id: flagid,       selectstatus: flag     });     this.setselectedvaluesarr(selectedlocatinsobject.tojs(), selectedvaluearr);     selectedvaluearr.foreach(selectedvalue => {         this.updatelocationslist(updatedtotallocationslist, selectedvalue.id, flag,parentid);      })      console.log(updatedtotallocationslist)  //returns updated total locations list   } 

to select sub-parent or child of selected node (if has children)

      setselectedvaluesarr(node, selectedvaluearr) {     if (node.children) {       node.children.foreach(child => {         selectedvaluearr.push({           id: child.id,           selectstatus: !child.isselected         })         this.setselectedvaluesarr(child, selectedvaluearr);       })     }   } 

to update total locations list

      updatelocationslist(node,id,flag,parentid) {     if (node.children) {       node.children.foreach(child => {         if (child.id == id ) {           child.isselected = flag           return false         }         this.updatelocationslist(child,id,flag,parentid);       })     }   } 

so when leaf node de-selected, sub-parent/parent nodes not getting de-selected.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -