1. #1
    Sencha Premium Member
    Join Date
    Dec 2010
    Posts
    24
    Vote Rating
    4
    demotics2002 is on a distinguished road

      0  

    Default Answered: How to restructure data returned by server to treestore data?

    Answered: How to restructure data returned by server to treestore data?


    My server returns array data like below
    Code:
    [
       {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
    Code:
    {'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

  2. The easiest way to do it is to just make a regular ajax request, create the correct object structure and then load the store.

    Code:
    Ext.Ajax.request({
         url : 'data.json',
         success : processresponse
    });
    
    function processresponse(response,options){
         var data = Ext.decode(response.responseText);
    
         //create the new structure
         var tree = createTree(data);
    
         store.setRootNode(tree);
    }
    Regards

  3. #2
    Sencha Premium Member crysfel's Avatar
    Join Date
    Feb 2008
    Location
    New York, USA
    Posts
    218
    Vote Rating
    8
    Answers
    7
    crysfel will become famous soon enough

      1  

    Default


    The easiest way to do it is to just make a regular ajax request, create the correct object structure and then load the store.

    Code:
    Ext.Ajax.request({
         url : 'data.json',
         success : processresponse
    });
    
    function processresponse(response,options){
         var data = Ext.decode(response.responseText);
    
         //create the new structure
         var tree = createTree(data);
    
         store.setRootNode(tree);
    }
    Regards

  4. #3
    Sencha Premium Member
    Join Date
    Dec 2010
    Posts
    24
    Vote Rating
    4
    demotics2002 is on a distinguished road

      0  

    Default


    OMG!!! You saved my day! Thank you...

Thread Participants: 1

Tags for this Thread