# Recursion with JSON

Show 40 post(s) from this thread on one page
Page 2 of 2 First 12
• 09-18-2013, 07:45 AM
val10
Thank you for quick respons and for present a variety of solutions. I use recursion from your code and implemented with ExtJS.
• 09-18-2013, 07:47 AM
val10
hi can you explain to me some part of your code :
Code:

``` obj[a].names= obj.names + (obj[a]["name"] ? "/" + obj[a]["name"]:"");     recurseIt(obj[a]);   }else{     if(a == "name")selPath.add(new Option(obj[a],obj["names"]))```
• 09-18-2013, 07:49 AM
val10
Quote:

Originally Posted by DaveyErwin
here I assume a different
meaning for "path" ...

Code:

```     obj[a].names= obj.names + (obj[a]["name"] ? "/" + obj[a]["name"]:"");     recurseIt(obj[a]);   }else{     if(a == "name")selPath.add(new Option(obj[a],obj["names"]))   }       }```

Can you explain t ome , what is happend here. I dont understand it. and this '?' what it do ?
• 09-18-2013, 08:26 AM
glenngv
You should be the one explaining what it is you really want instead of us assuming and guessing. Posting a sample "path" will help.

Btw, the ? is a ternary operator which is a shorthand for if-else statement. Google for it for more info.
• 09-18-2013, 03:52 PM
val10
http://pastebin.com/XxBKkY81
Here is one of solution that has posted.
Recursion is ok. Just has to be ExtJS comboBox, in 'store' has to be names from tree json, and has to be listeners who looks wich name from combobox is selected and in a text field show us the path to chosen one.
• 09-18-2013, 11:49 PM
glenngv
I don't know ExtJS so I had to take a look at their documentation to know how ExtJS combobox is done.

One of the ways to specify the store array option is this (which is what you need in this case):

Code:

```store: [   [optionValue, optionText],   [optionValue, optionText],   [optionValue, optionText],   //...and so on... ]```
So I tweaked DaveyErwin's code to extract the correct array format from the json data.

Code:

```[     ['/Planning', 'Planning'],     ['/Planning/Investigate', 'Investigate'],     ['/Planning/Assign Resources', 'Assign Resources'],     //...and so on ]```
Here it is:

Code:

```<div id="combobox"></div> <textarea id="path"></textarea>```
Code:

```var data = [{     "name": "Planning",     "id": 1,     "parentId": 994423,     "children": [{         "name": "Investigate",         "id": 11,         "parentId": 1     }, {         "name": "Assign resources",         "id": 12,         "parentId": 1     }, {         "name": "Gather documents",         "id": 13,         "parentId": 1     }, {         "name": "Report to management",         "id": 17,         "parentId": 1,         "children": [{             "name": "Implementation Phase 1",             "id": 4,             "parentId": 17,             "children": [{                 "name": "Preparation work",                 "id": 34,                 "parentId": 4             }, {                 "name": "Evaluate chipsets",                 "id": 14,                 "parentId": 4             }, {                 "name": "Choose technology suite",                 "id": 16,                 "parentId": 4             }, {                 "name": "Build prototype",                 "id": 15,                 "parentId": 4,                 "children": [{                     "name": "Step 1",                     "id": 20,                     "parentId": 15                 }, {                     "name": "Step 2",                     "id": 19,                     "parentId": 15                 }, {                     "name": "Step 3",                     "id": 18,                     "parentId": 15                 }, {                     "name": "Follow up with customer",                     "id": 21,                     "parentId": 15,                     "children": [{                         "name": "Customer approval",                         "id": 5,                         "parentId": 21                     }, {                         "name": "Implementation Phase 2",                         "id": 6,                         "parentId": 21,                         "children": [{                             "name": "Task 3",                             "id": 25,                             "parentId": 6                         }, {                             "name": "Task 2",                             "id": 26,                             "parentId": 6                         }, {                             "name": "Task 1",                             "id": 27,                             "parentId": 6,                             "children": [{                                 "name": "Production phase 1",                                 "id": 8,                                 "parentId": 27,                                 "children": [{                                     "name": "Assemble",                                     "id": 22,                                     "parentId": 8                                 }, {                                     "name": "Load SW",                                     "id": 23,                                     "parentId": 8                                 }, {                                     "name": "Basic testing (inc some test)",                                     "id": 24,                                     "parentId": 8,                                     "children": [{                                         "name": "Customer approval 2",                                         "id": 10,                                         "parentId": 24                                     }]                                 }]                             }]                         }]                     }]                 }]             }]         }]     }] }, {     "name": "Final testing",     "id": 9,     "parentId": 123321 }, {     "name": "Delivery",     "id": 7,     "parentId": 654321 }]; var storeArr = []; data.names = ""; (function recurseIt(obj) {     for (var a in obj) {         if (typeof obj[a] == "object") {             obj[a].names = obj.names + (obj[a]["name"] ? "/" + obj[a]["name"] : "");             recurseIt(obj[a]);         } else {             if (a == "name") {               storeArr.push([obj["names"], obj[a]]);             }         }     } })(data); Ext.create('Ext.form.field.ComboBox', {     fieldLabel: 'Choose One',     store: storeArr,     renderTo: Ext.getDom('combobox'),     listeners: {         change: function(field, newValue, oldValue) {             document.getElementById('path').value = field.value;         }     } });```
Show 40 post(s) from this thread on one page
Page 2 of 2 First 12