3 Replies Latest reply on Jul 29, 2006 11:15 AM by takak

    Iterating through openItems (Tree)

    takak
      I have a tree called cat_tree, which looks like:

      <mx:Tree id="cat_tree" dataProvider="{categories_list.list}" labelField="@label" showRoot="true" width="375" height="250" change="event.currentTarget.editable=false;updateGroups()" editable="false" itemClick="event.currentTarget.editable=true" itemEditEnd="event.currentTarget.editable=false;updateCategory()"/>

      Oh and by the way, if you ever wanted to figure out that problem of making a tree only editable on double-click, the above is the answer. Sorry, to go off on a different tangent here, but I know people have butted their heads against walls on this one from browsing through some forums. You simply just set editable to "false" and then for itemClick, you set it to "event.currentTarget.editable = true", then for itemEditEnd, you set the value to "event.currentTarget.editable = false" and for change you set the value to "event.currentTarget.editable = false".

      Basically what that does is when you click on an item (before you click on an item, editable is set to false, so it doesn't go into "edit" mode), it turns on editability for the tree. Then, when you click again, since editability is now enabled, it goes into "edit" mode. ItemEditEnd tells the tree to turn off editibility when you finish editing the item again. So, this works great, but if you then click on one node of a tree and then click on another, it goes straight into edit mode, so then you add change and set it to "event.currentTarget.editable = false", so when you change nodes, it resets the editability to false and then (luckily) itemClick gets called after change, so then itemClick starts it's process all over again.

      I hope that all made sense. Anyways, if you need to use this a lot, I would advise putting it into a custom component.

      So, that tells you why the above code might look a little bit "funky". Anyways, moving on to the real reason of my post...

      I have a function called updateCategory which gets called after a node is edited. Basically, I want to store the values in tree.openItems and then restore them when the dataProvider gets updated (the dataProvider is an HTTPService that gets resent).

      My function looks like:

      private function updateCategory():void{
      var openedItems:Array = new Array();
      Alert.show(cat_tree.openItems.children().length());
      x:int;
      for(x = 0; x < cat_tree.openItems.length(); x ++){
      openedItems[x] = cat_tree.openItems[x].@id;
      }

      var new_name:String = TextInput(cat_tree.itemEditorInstance).text
      var rVars:Object = new Object();
      rVars.new_name = new_name;
      rVars.category = cat_tree.selectedItem.@id;
      categories_list_http.send(rVars);

      x:int;
      for(x = 0; x < openedItems.length(); x ++){
      cat_tree.expandItem(categories_list.list.node.(@id==openedItems[x]),true,false,false);
      }
      }

      Should this work in theory, or am I totally off the mark. I had initially tried something like:

      private function updateCategory():void{

      var openedItems:Object = new Object();
      openedItems = cat_tree.openItems;

      var new_name:String = TextInput(cat_tree.itemEditorInstance).text
      var rVars:Object = new Object();
      rVars.new_name = new_name;
      rVars.category = cat_tree.selectedItem.@id;
      categories_list_http.send(rVars);

      cat_tree.openItems = openedItems;
      }

      but that didn't work at all.

      Any pointers.

      I know, I could just use a second HTTPService call and use that for updating and have my initial one just never refresh, and I may have to do that, but now my curiosity has gotten the better of me.

        • 1. Re: Iterating through openItems (Tree)
          Lord Bron
          I'm with you your first attempt. It seemed the most logical way to accomplish it to me. Seems silly to have such an nice property and not have the expected behavior to go with it!
          • 2. Iterating through openItems (Tree)
            Lord Bron Level 1
            I've got it to finally work with openItems. I blogged the complete source on my blog.

            Here's snippets from my code.

            In the Script tag:
            ...
            [Bindable]
            public var tempOpen:Object = new Object();
            [Bindable]
            public var refreshSO:Boolean = false;
            ...
            public function switchDP():void
            {
            tempOpen = testTree.openItems;
            refreshSO = true;
            if (currentDP == "tempObject")
            {
            currentDP = "secondObject";
            testTree.dataProvider = secondObject;
            }
            else
            {
            currentDP = "tempObject";
            testTree.dataProvider = tempObject;
            }
            }

            public function renderFunc():void
            {
            if (refreshSO)
            {
            refreshSO = false;
            testTree.openItems = tempOpen;
            }
            }

            In my mxml code:
            <mx:Tree id="testTree" render="renderFunc()" width="250" click="onTreeNodeClick(event)" dataProvider="{tempObject}" labelField="label" />

            That works beautifully for me!
            • 3. Re: Iterating through openItems (Tree)
              takak Level 1
              Dear Tom,

              Thanks a lot for this and trying to help me figure this out. Unfortunately, this doesn't work with a dataProvider that's an HTTPService. I just posted another topic called "expandItem super weird behavior" as now I am close, but getting a really weird thing happening. (I did use some of your notes, such as using "render" and stuff).

              Thanks Again!