View Full Version : How to restructure data returned by server to treestore data?

20 Sep 2012, 5:50 PM
My server returns array data like below

{id: '1', parent: '', name: 'AB'},
{id: '2', parent: '1', name: 'A'},
{id: '3', parent: '1', name: 'B'},
{id: '4', parent: '', name: 'CD'},
{id: '5', parent: '4', name: 'C'},
{id: '6', parent: '4', name: 'D'},
{id: '7', parent: '', name: 'E'}

But Ext.data.TreeStore expects it to be

{'root': 'theroot', 'children': [
{id: '1', name: 'AB',
children: [
{id: '2', name: 'A'},
{id: '3', name: 'B'}
{id: '4', name: 'CD',
children: [
{id: '5', name: 'C'},
{id: '6', name: 'D'}
{id: '7', name: 'E'}

I can write an algorithm to transform it but I do not know where to place it. I tried inside the load listener but the records passed to the function is not raw (it is array of Model). And suppose I know how to iterate and transform it, I do not know have idea on how I will set the records with the newly structured data.

Anyone there who had this same requirement before and managed to come up with a solution? Thank you

20 Sep 2012, 9:14 PM
The easiest way to do it is to just make a regular ajax request, create the correct object structure and then load the store.

url : 'data.json',
success : processresponse

function processresponse(response,options){
var data = Ext.decode(response.responseText);

//create the new structure
var tree = createTree(data);



20 Sep 2012, 9:39 PM
OMG!!! You saved my day! Thank you...