1. #1
    Ext User
    Join Date
    Aug 2007
    Posts
    60
    Vote Rating
    0
    mscdex is on a distinguished road

      0  

    Default Populating a tree from a list of paths?

    Populating a tree from a list of paths?


    Is there a way to do this? I have the paths of each treenode stored in a database (e.g. "Topmost/Child/Child/Leafnode") and want to be able to add these nodes to a tree by returning the json representation of the paths to the TreeLoader constructor. How can I do this?

  2. #2
    Ext User
    Join Date
    Aug 2007
    Posts
    60
    Vote Rating
    0
    mscdex is on a distinguished road

      0  

    Default


    I guess this is not possible since there isn't even a "parent" config option for TreeNodes? Or is there another way?

  3. #3
    Ext User
    Join Date
    Aug 2007
    Posts
    59
    Vote Rating
    1
    Barius is on a distinguished road

      0  

    Default


    You mean you just want to put the string of json data directly into the js code, rather than using an asynchrous call?

    In your root node just add the parameter

    children: <your json encoded data here>

    Make sure your json data is an array (i.e. wrap it in [] if necessary).

  4. #4
    Ext User
    Join Date
    Aug 2007
    Posts
    60
    Vote Rating
    0
    mscdex is on a distinguished road

      0  

    Default


    Quote Originally Posted by Barius View Post
    You mean you just want to put the string of json data directly into the js code, rather than using an asynchrous call?

    In your root node just add the parameter

    children: <your json encoded data here>

    Make sure your json data is an array (i.e. wrap it in [] if necessary).
    Sorry, I mean I would just initially obtain the contents for the tree from a call to a php script, and it would store the children in the tree and do no more future updates.

  5. #5
    Ext User
    Join Date
    Aug 2007
    Posts
    59
    Vote Rating
    1
    Barius is on a distinguished road

      0  

    Default


    So when the tree first loads you want it to get all the data from the server at once, then not request again after that?

    If you load all the data the first time then the tree will by nature not request again because a request is only sent if the data has not already been obtained. So all you need to do is setup a basic tree as per the examples at the documentation site (extjs.com/deploy.ext/docs). When you send your data to the tree, just send everything at once in one big json string.

  6. #6
    Ext User
    Join Date
    Aug 2007
    Posts
    60
    Vote Rating
    0
    mscdex is on a distinguished road

      0  

    Default


    How would I specify a hierarchy (children) though? There is no children or parent config option in the 1.1.1 docs for treenodes?

  7. #7
    Ext User
    Join Date
    Aug 2007
    Posts
    59
    Vote Rating
    1
    Barius is on a distinguished road

      0  

    Default


    In PHP, create a multi-level array so it looks like this:

    Code:
    A{
      [0]
        'id'=>'some id'
        'text'=>'displayed text'
        'leaf'=>true/false (false for this because it has children)
        'cls'=>css class
        'other params you feel like adding'=>'whatever'
        'children'=>B{
                            [0]
                               'id'=>'an id'
                               'text'=>'some text'
                               'leaf'=>true/false (true because this has no children)
                               'cls'=>css class
                               'other params you feel like adding'=>'whatever'
                               'children'=>empty
                        }
      [1]
        ....
    }
    To turn it into JSON, just use the built in php function json_encode($A).

  8. #8
    Ext User
    Join Date
    Aug 2007
    Posts
    60
    Vote Rating
    0
    mscdex is on a distinguished road

      0  

    Default


    Quote Originally Posted by Barius View Post
    In PHP, create a multi-level array so it looks like this:

    Code:
    A{
      [0]
        'id'=>'some id'
        'text'=>'displayed text'
        'leaf'=>true/false (false for this because it has children)
        'cls'=>css class
        'other params you feel like adding'=>'whatever'
        'children'=>B{
                            [0]
                               'id'=>'an id'
                               'text'=>'some text'
                               'leaf'=>true/false (true because this has no children)
                               'cls'=>css class
                               'other params you feel like adding'=>'whatever'
                               'children'=>empty
                        }
      [1]
        ....
    }
    To turn it into JSON, just use the built in php function json_encode($A).
    I think what I'm having trouble with is generating the actual multidimensional arrays such that they will be converted to valid json to be able to be consumed.

    I have a table with rows that have a field containing a string in the form of "CategoryFoo/CategoryBar/CategoryBlah" or "CategoryFoo/CategoryBar". For each row in the table I'm trying to first explode() the string, create the correct the corresponding multidimensional array (CategoryFoo's children=>CategoryBar's children=>CategoryBlah) such that each level has the correct treenode config options and then combine/merge (sucessfully) that temporary array with a main array that will contain the previously stored arrays. I then convert the main array to json by json->encode (I have to use the PECL library).

    But I cannot figure out how to code such a thing for this kind of situation, I've tried many different ways but then just when I think I have it the method of combining the temporary array with the main one does not work as expected and always go back to the drawing board.

  9. #9
    Ext User
    Join Date
    Aug 2007
    Posts
    60
    Vote Rating
    0
    mscdex is on a distinguished road

      0  

    Default


    I managed to get a single array created for the field from a single row in my mysql table and it looks like this:

    Code:
    Array
    (
        [0] => Array
            (
                [text] => SomeCategory
                [children] => Array
                    (
                        [0] => Array
                            (
                                [text] => OneSubCategory
                                [children] => Array
                                    (
                                        [0] => Array
                                            (
                                                [text] => AnotherSubCategory
                                                [children] => 
                                            )
    
                                    )
    
                            )
    
                    )
    
            )
    )
    However, if I have a main array that will eventually contain all of the categories combined together, I have not yet figured out how to successfully combine all children nodes that share the same hierarchy of parents.

Thread Participants: 1