16 Replies Latest reply on Jan 21, 2011 1:42 AM by flexillu10

    Using a Tree as a menu

    flexillu10 Level 1

      I have a a tree being used as a kind of file browser. Like so:

      treemenu.png

      Is there a way to get the path to this item? So Menswear -> Sportswear-> Tops.

       

      Maybe in a list or an array? Whatever is best to pass it to my php script.

       

      3 variables A B C would do the tric actually.

       

      Example code is appreciated.

        • 1. Re: Using a Tree as a menu
          flexillu10 Level 1

          Still looking for help with this?

           

          I need to use the three terms to search my database, is there an easier way i'm missing?

          • 2. Re: Using a Tree as a menu
            Flex harUI Adobe Employee

            What does your data look like?  The selectedItem should be "Tops" and if you

            have parent references you can find the rest.

            • 3. Re: Using a Tree as a menu
              flexillu10 Level 1

              Flex harUI wrote:

               

              What does your data look like?  The selectedItem should be "Tops" and if you

              have parent references you can find the rest.

              Well the dataprovider is XML.

               

              I'm not to sure on the whole thing really, can you give me any info on the whole process?

               

              So what should my XML look like? At the moment it is like so, but this is just a guess of how it should be

               

              <folder label="root">
                   <folder label="Menswear">
                        <folder label="Sportswear">
                             <folder label="Tops">
                                <item label="Crew neck"/>
                             </folder>
                                  <folder label="Bottoms">
                                <item label="Joggers"/>
                             </folder>
                        </folder>
                   </folder>
              </folder>
              
              

               

              Is that correct? can the words "folder" and "item" be anything? How do you specify parents?

               

              Then how do i use this as the dataProvider?

               

              Example code would be brilliant

              • 4. Re: Using a Tree as a menu
                Flex harUI Adobe Employee

                If the data is XML and shows correctly in the tree, then get the tree's

                selectedItem and get the parent() and its parent() until it returns the root

                folder.

                1 person found this helpful
                • 5. Re: Using a Tree as a menu
                  flexillu10 Level 1

                  Flex harUI wrote:

                   

                  If the data is XML and shows correctly in the tree, then get the tree's

                  selectedItem and get the parent() and its parent() until it returns the root

                  folder.

                  any chance of some example code from anyone for the above? (i've been trying all day and i can't seem to get at the items)

                  • 6. Re: Using a Tree as a menu
                    Flex harUI Adobe Employee

                    What does your best attempt at the code look like?

                    • 7. Re: Using a Tree as a menu
                      flexillu10 Level 1

                      Flex harUI wrote:

                       

                      What does your best attempt at the code look like?

                       

                      Thanks for reply, looks like this:

                       

                      protected function menutree_itemClickHandler(event:ListEvent):void
                                     {
                                          var tree:Tree = event.currentTarget as Tree;
                                          
                                          if(!tree.dataDescriptor.isBranch(tree.selectedItem)){
                                               params["A"]=????;//need parent of below
                                               params["B"]=????;//need parent of selected item
                                               params["C"]=tree.selectedItem;
                                               
                                               
                                          }
                                     }
                      
                      
                      
                      • 8. Re: Using a Tree as a menu
                        Flex harUI Adobe Employee

                        Try something like this:

                         

                                                 params[C]=tree.selectedItem;

                                                  params[B]=tree.selectedItem.parent()[0];

                                                  params[A]=tree.selectedItem.parent()[0];

                        • 9. Re: Using a Tree as a menu
                          flexillu10 Level 1

                          Flex harUI wrote:

                           

                          Try something like this:

                           

                                                    params[C]=tree.selectedItem;

                                     &nbs p;           &n bsp;  params[B]=tree.selectedItem.parent()[0];

                                     &nbs p;           &n bsp;  params[A]=tree.selectedItem.parent()[0];

                           

                          Is what you wrote correct?

                           

                          params[B]=tree.selectedItem.parent()[0];

                          params[A]=tree.selectedItem.parent()[0];

                           

                          this just gives the parent of the selected item twice.

                           

                          I sort of got it working with the below code:

                           

                           

                               if(!tree.dataDescriptor.isBranch(tree.selectedItem)){
                                                   
                                                   params["C"]=tree.selectedItem.@label;
                                                   
                                                   params["B"]=tree.selectedItem.parent()[0].@label;
                                                   params["A"]=tree.selectedItem.parent()[0].parent()[0].@label;
                                                   
                                                   Alert.show(params["C"]);
                                                   Alert.show(params["B"]);
                                                   Alert.show(params["A"]);
                                              }
                          

                           

                           

                          However some nodes have more or less than one parent, how can i get all the parents up to the root? I need the code to be able to handle all possibilities.

                           

                          Again thanks for your help, nobody else has done this? Or am i doing something silly in the first place?

                          • 10. Re: Using a Tree as a menu
                            Flex harUI Adobe Employee

                            You would use a loop and an array, something like:

                             

                                var params:Array = new Array();

                                var lastNode:XML = tree.selectedItem;

                                while (lastNode)

                                {

                                    params.unshift(lastNode.@label);

                                    lastNode = lastNode.parent()[0];

                                }

                            • 11. Re: Using a Tree as a menu
                              flexillu10 Level 1

                              i get the error:

                               

                              -1118: Implicit coercion of a value with static type Object to a possibly unrelated type XML

                               

                              on the line:

                               

                              var lastNode:XML = menutree.selectedItem

                               

                              tried adding .node on the end and i get this error

                               

                              TypeError: Error #1034: Type Coercion failed: cannot convert XMLList@b73dbe1 to XML.

                               

                              Man i am so confused.

                              • 12. Re: Using a Tree as a menu
                                Flex harUI Adobe Employee

                                I am only offering suggestions, not proven code, as I don't have time to be

                                working through everyone's test cases on this forum.  I would suggest that

                                while you are waiting for a response from the forum that you try out a few

                                AS training courses or look up the common reasons for these kinds of errors.

                                 

                                It appears from what you've posted that menutree.selectedItem is an XML

                                object so try:

                                    var lastNode:XML = XML(menutree.selectedItem);

                                • 13. Re: Using a Tree as a menu
                                  aktell2007 Level 1

                                  Not that I had it mention already to you awhile ago - FLEX is research, research, research !!! I know for a fact that if you search on this forum 'something like Tree as menu or so it will come up with a few suggestions which have been addressed here before !!!This item in particular is not easy to address as I found out myself a month or so ago. In the end I was furtunat enough to get it working, but where is the fun to be a developer if you do only copy and paste. regards aktell

                                  • 14. Re: Using a Tree as a menu
                                    flexillu10 Level 1

                                    aktell2007 wrote:

                                     

                                    Not that I had it mention already to you awhile ago - FLEX is research, research, research !!! I know for a fact that if you search on this forum 'something like Tree as menu or so it will come up with a few suggestions which have been addressed here before !!!This item in particular is not easy to address as I found out myself a month or so ago. In the end I was furtunat enough to get it working, but where is the fun to be a developer if you do only copy and paste. regards aktell

                                     

                                    I have researched this, and not found a good example of a tree that uses XML from an external source. Hence why i've posted here.

                                     

                                    What i need to do is easy to address in my opinion. The tree knows which item is the parent of what because of the way it has to display the data.

                                     

                                    So surely it must be easy to get the level above "label" field until the label is root.

                                    • 15. Re: Using a Tree as a menu
                                      aktell2007 Level 1

                                      **** I have researched this, and not found a good example of a tree that uses XML from an external source. ****

                                      I agree with you fully, I did not find anything either !!! My need was to work it with a ViewStack and modules as well, and therefore I used in the end to get it working a XmlList.

                                      Another thing I like to mention as you are building an AIR App. and before you get ahead with this project:

                                      Do make sure you use throughout your developing phase code to check your memory and possibly your computers memory as well as it will build up like no tomorrow ! and when it does than you have to address your eventListeners, Vars etc. etc. and so on.

                                      Rubbish Collection in AIR can be an isuue which brings you on the bring of madness especialy when the app spose to do some work has modules or uses Html windows. In any case have fun that is the important thing in all of this developing with FLEX. regards aktell

                                      • 16. Re: Using a Tree as a menu
                                        flexillu10 Level 1

                                        Ok thanks a lot for the advice