6 Replies Latest reply on Aug 31, 2009 1:44 AM by preetp

    Letting a user add a node to a tree

    arkcto Level 1
      Hi,

      Currently I have a tree that is built dynamically with a remote object call. Also, if the user clicks on a node, a menu pops up allowing the user to create a child for the node. How do I let the user add a node to the tree ? I saw tangential examples showing how to add xml nodes, but I am unable to translate that to trees that are populated NOT in static xml, but from a remote object call.

      I have the following function signature :

      // parentIndex - the index of the parent node, where the user wants to add a child node
      // childLabel - the label for the child node.
      private addChild(parentIndex:int, childLabel:String) {


      }

      Thanks for your help.
        • 1. Re: Letting a user add a node to a tree
          tspratt
          It does not matter how the dataProvider is initially populated. You still add nodes the same way. What is not working?

          I have an example on cflex.net that is an tree-based xml editor.

          Also, don't use the same name for your methods that Flex uses internally (addChild) it is dangerous.

          Tracy
          • 2. Letting a user add a node to a tree
            arkcto Level 1
            Hi Tracy, this the code I am using from PeterEnt's blog. His example was about drag-and-drop. I am trying to use it (maybe I shouldn't:) for my child node insertion.

            myTree.selectedIndex = parentIndex;
            var node:XML = myTree.selectedItem as XML; // interestingly, myTree.selectedItem is not null, but node is null. Why ?
            var p:*;

            if(myTree.dataDescriptor.hasChildren(node)) {
            p = node;
            parentIndex = 0;
            }

            else { // I am getting a null pointer exception here
            p = node.parent();
            }
            var insert:XML = <node />;
            insert.@label = "New Toy";
            insert.@type = "Toy";
            myTree.dataDescriptor.addChildAt(p, insert, parentIndex+1);

            I am getting the parent value (label etc) correctly for myTree.selectedItem. But when I do node:XML = myTree.selectedItem as XML, I am getting null value for null. That's why I asked in my first message if there is a difference in populating the data statically as XML v.s. loading the data using remote object (which is the case here).

            Appreciate your help.
            • 3. Re: Letting a user add a node to a tree
              ntsiii Level 3
              One thing to watch out for is when you assign a dataProvider to a tree (or any other control), you can't access many of the properties(like selecteditem) immediately, because the Tree has not had time to render itself.

              In cases like this, use callLater() to invoke the access/manipulation.

              I advise getting the functionality to work by button click before you add the drag/drop to the mix.

              Tracy
              • 4. Re: Letting a user add a node to a tree
                arkcto Level 1
                selectedItem is not the problem. I am getting the right results.

                However when I convert it to XML
                var node:XML = myTree.selectedItem as XML

                node is coming as null.

                Why is that ? and what should I do ?

                THanks.
                • 5. Letting a user add a node to a tree
                  arkcto Level 1
                  Never mind. I figured it out.

                  Btw, it does matter how the data is populated. If it's XML use XML, if it's remote object, use object for the inserted node.
                  • 6. Re: Letting a user add a node to a tree
                    preetp

                    Hi,

                     

                    I was searching for help for loading a tree through remote object.

                    It seems you have the code for the same. Can you please help me out with the same?

                    I am new to flex. So any help would be great.

                     

                    Thanks and Regards.