17 Replies Latest reply on Sep 4, 2008 10:35 AM by vikceo

    changing arrayCollection source

    vikceo Level 1
      Hie..

      in my mxml i have defined something
      <mx:ArrayCollection id="categories" source="{getTabs()}"/>
      here getTabs() returns a list of strings. based on that i m generating some buttons with labels coming from getTabs() (using a repeater component).

      now on some user action i want to change the source of the array collection through action script.
      i tried doing like categories.source = getNewTabs();

      but this is not working..

      any idea?


        • 1. Re: changing arrayCollection source
          Level 7

          "vikceo" <webforumsuser@macromedia.com> wrote in message
          news:g9kjaa$glh$1@forums.macromedia.com...
          > Hie..
          >
          > in my mxml i have defined something
          > <mx:ArrayCollection id="categories" source="{getTabs()}"/>
          > here getTabs() returns a list of strings. based on that i m generating
          > some
          > buttons with labels coming from getTabs() (using a repeater component).
          >
          > now on some user action i want to change the source of the array
          > collection
          > through action script.
          > i tried doing like categories.source = getNewTabs();
          >
          > but this is not working..

          It's bound to the function, so I think that maybe it changes to the new
          function and then immediately back to the original function. However, you
          might want to try adding

          categories.refresh() after you change the source.

          HTH;

          Amy


          • 2. Re: changing arrayCollection source
            vikceo Level 1
            hie
            sorry doesn't work...

            any other suggestions?

            Vik
            • 3. Re: changing arrayCollection source
              Gregory Lafrance Level 6
              Just base the ArrayCollection on an Array, and change that array however you want, then be sure to refresh the ArrayCollection.
              • 4. Re: changing arrayCollection source
                vikceo Level 1
                i m sorry greg did not understand it exactly.. right now i m changing the source by giving it the returned result of some other function...

                where should i use the array to base my array collection?
                • 5. Re: changing arrayCollection source
                  yigitt
                  if you want to keep
                  <mx:ArrayCollection id="categories" source="{getTabs()}"/>
                  (maybe for code clarity)
                  just implement getTabs function as bindable function. then you can handle which tabs to return inside the getTabs function.
                  if you dont know how to implement a bindable function, just check livedocs, it has a very clear example,.
                  • 6. Re: changing arrayCollection source
                    Gregory Lafrance Level 6
                    private var arr:Array = new Array();

                    [Bindable]
                    private var ac:ArrayCollection = new ArrayCollection(arr);

                    private function getTabs():void{
                    arr = myNewArray;
                    ac.refresh();
                    }
                    • 7. Re: changing arrayCollection source
                      vikceo Level 1
                      Thankx guys.. I tried Greg's solution but is not working.. just to re-review what i did is:
                      private var topLevelTabs:Array = new Array();
                      [Bindable]
                      public var categories:ArrayCollection = new ArrayCollection(topLevelTabs);

                      and on some button listener method is:
                      public function getTopLevelGroups(worklistInfo:XML, groupType:String="Category"):void{
                      var arr:ArrayCollection;
                      if(groupType == "Category") arr = VStack.getTabs(getWorklistCategories(worklistInfo));
                      else arr = VStack.getTabs(loadByExpiryDate(worklistInfo));break;

                      topLevelTabs = arr.toArray();
                      categories.source = topLevelTabs;
                      categories.refresh();
                      }

                      i made sure that i m going to the else part.. but it is not really switching the stuff...
                      • 8. Re: changing arrayCollection source
                        Level 7

                        "vikceo" <webforumsuser@macromedia.com> wrote in message
                        news:g9mol8$sn0$1@forums.macromedia.com...
                        > Thankx guys.. I tried Greg's solution but is not working.. just to
                        > re-review
                        > what i did is:
                        > private var topLevelTabs:Array = new Array();
                        > [Bindable]
                        > public var categories:ArrayCollection = new
                        > ArrayCollection(topLevelTabs);
                        >
                        > and on some button listener method is:
                        > public function getTopLevelGroups(worklistInfo:XML,
                        > groupType:String="Category"):void{
                        > var arr:ArrayCollection;
                        > if(groupType == "Category") arr =
                        > VStack.getTabs(getWorklistCategories(worklistInfo));
                        > else arr = VStack.getTabs(loadByExpiryDate(worklistInfo));break;
                        >
                        > topLevelTabs = arr.toArray();
                        > categories.source = topLevelTabs;
                        > categories.refresh();
                        > }

                        It looks like your break statement is in the else part.

                        HTH;

                        Amy


                        • 9. Re: changing arrayCollection source
                          vikceo Level 1
                          sorry a typo.. actually just to simplyfy the code i changed the switch case to a if else case. ..the break statement is not present in the actual code.
                          • 10. Re: changing arrayCollection source
                            Level 7

                            "vikceo" <webforumsuser@macromedia.com> wrote in message
                            news:g9mrkn$2km$1@forums.macromedia.com...
                            > sorry a typo.. actually just to simplyfy the code i changed the switch
                            > case to a if else case. ..the break statement is not present in the actual
                            > code.

                            What's the actual code?


                            • 11. Re: changing arrayCollection source
                              vikceo Level 1
                              Amy this is the actual code:

                              public function getTopLevelGroups(worklistInfo:XML, groupType:String="Category"):void{
                              var arr:ArrayCollection;
                              if(groupType == "Category")
                              arr = VStack.getTabs(getWorklistCategories(worklistInfo));
                              else
                              arr = VStack.getTabs(loadByExpiryDate(worklistInfo));
                              topLevelTabs = arr.toArray();
                              categories.source = topLevelTabs;
                              categories.refresh();
                              }

                              and in the mxml it is
                              private var topLevelTabs:Array = new Array();
                              [Bindable]
                              public var categories:ArrayCollection = new ArrayCollection(topLevelTabs);

                              By default it calls and do the stuff rightly for the grouptype= Category. but on click of a button i call this method and passt groupType="ABC"

                              in this case it goes to the else case but does not switch to the new value of arr.
                              • 12. Re: changing arrayCollection source
                                Level 7

                                "vikceo" <webforumsuser@macromedia.com> wrote in message
                                news:g9mu7q$5cg$1@forums.macromedia.com...
                                > Amy this is the actual code:
                                >
                                > public function getTopLevelGroups(worklistInfo:XML,
                                > groupType:String="Category"):void{
                                > var arr:ArrayCollection;
                                > if(groupType == "Category")
                                > arr = VStack.getTabs(getWorklistCategories(worklistInfo));
                                > else
                                > arr = VStack.getTabs(loadByExpiryDate(worklistInfo));
                                > topLevelTabs = arr.toArray();
                                > categories.source = topLevelTabs;
                                > categories.refresh();
                                > }
                                >
                                > and in the mxml it is
                                > private var topLevelTabs:Array = new Array();
                                > [Bindable]
                                > public var categories:ArrayCollection = new ArrayCollection(topLevelTabs);
                                >
                                > By default it calls and do the stuff rightly for the grouptype= Category.
                                > but
                                > on click of a button i call this method and passt groupType="ABC"
                                >
                                > in this case it goes to the else case but does not switch to the new value
                                > of
                                > arr.

                                what is in getTabs and loadByExpiryDate? Have you looked at arr in the
                                debugger? Why are you making arr an ArrayCollection and then turning it
                                back into an Array, when you could just use an Array to start with?


                                • 13. Re: changing arrayCollection source
                                  vikceo Level 1
                                  Hie Amy

                                  yes i checked that my arr is returning me the right set of values on switching.

                                  getTabs returns just an arrayCollection which i created based on some business logic. There is no problem in getting the right values. It is not switching actually..

                                  I also tried your suggestion to replace arr to array type instead of ArrayCollection and making required changes to the getTabs method to return an array rather array collection.

                                  Still no luck...
                                  • 14. Re: changing arrayCollection source
                                    Level 7

                                    "vikceo" <webforumsuser@macromedia.com> wrote in message
                                    news:g9n2gq$9q9$1@forums.macromedia.com...
                                    > Hie Amy
                                    >
                                    > yes i checked that my arr is returning me the right set of values on
                                    > switching.
                                    >
                                    > getTabs returns just an arrayCollection which i created based on some
                                    > business
                                    > logic. There is no problem in getting the right values. It is not
                                    > switching
                                    > actually..
                                    >
                                    > I also tried your suggestion to replace arr to array type instead of
                                    > ArrayCollection and making required changes to the getTabs method to
                                    > return an
                                    > array rather array collection.

                                    Did you remove the data binding you started out with?


                                    • 15. Re: changing arrayCollection source
                                      vikceo Level 1
                                      i hope you mean removing
                                      [Bindable] word from the lines:


                                      private var arr:Array = new Array();
                                      [Bindable]
                                      private var ac:ArrayCollection = new ArrayCollection(arr);


                                      yes earlier i did not but after your update on this i tried that too... but no success..

                                      one more thing, i just revisited my code and tried to print the value from the array collection after assigning it the new source and then refreshing it. Well i can see the new value in the array collection. So, does it meant that my arraycollection is refreshing properly and problem is somewhere else? if yes then where should i look for?


                                      • 16. Re: changing arrayCollection source
                                        Level 7

                                        "vikceo" <webforumsuser@macromedia.com> wrote in message
                                        news:g9nm50$t3b$1@forums.macromedia.com...
                                        >i hope you mean removing
                                        > [Bindable] word from the lines:
                                        >
                                        >
                                        > private var arr:Array = new Array();
                                        > [Bindable]
                                        > private var ac:ArrayCollection = new ArrayCollection(arr);
                                        >
                                        >
                                        > yes earlier i did not but after your update on this i tried that too...
                                        > but no
                                        > success..

                                        No, I meant the part where you used the curly brackets in MXML.

                                        > one more thing, i just revisited my code and tried to print the value from
                                        > the
                                        > array collection after assigning it the new source and then refreshing it.
                                        > Well
                                        > i can see the new value in the array collection. So, does it meant that my
                                        > arraycollection is refreshing properly and problem is somewhere else? if
                                        > yes
                                        > then where should i look for?

                                        I've lost track of where the original thread is, and I don't recall the
                                        structure of your app. Sorry.


                                        • 17. Re: changing arrayCollection source
                                          vikceo Level 1
                                          ok Amy closing it here and starting a new thread with the current code and my issue.

                                          Thanks for helping me out...