6 Replies Latest reply on Apr 9, 2009 11:24 AM by Gregory Lafrance

    How do I access a datagrid in a component?

    Handycam Level 1

      I have an app made out of several instances on an MXML component, in a viewstack.

       

      Each component has 2 datagrids, one is populated with an XMLListCollection and one is empty.  People are supposed to drag items from the full one to the empty one.  Fine so far.

       

      However, I need to use the items that have been dragged into the second list elsewhere in the app, such as other components that show progress etc.

       

      What's the best way to do this?  In olden times I had a collection in the main app which I updated via Application.application.myCollection, but I'd rather do it in a cleaner, more OOP way.  Thanks!

        • 1. Re: How do I access a datagrid in a component?
          Gregory Lafrance Level 6

          Actually, I think accessing the data provider is the best way. After drag n drop, the 2nd DG data provider should be updated, that way you have access to the data anywhere.

          • 2. Re: How do I access a datagrid in a component?
            Handycam Level 1

            That makes sense, but how would I get that from another component?

             

            The components that have these grids are created in a loop as  pasted below. In each new "recipeStep" instance, the grid I am looking to access its data provider is called "itemsChosen".

             

            // Create and add steps

            private function createSteps():void{

            var stepNumber:uint = 1;

            var numSteps:uint = dataXML.length;

                    for each(var step:XML in dataXML){

                      var recipeStep:CyorStep = new CyorStep();

                      mainViewStack.addChild(recipeStep);

                      recipeStep.label = stepNumber +". "+ step.stepTitle;

                      recipeStep.itemChoicesList = step..item;

                      recipeStep.prompt.text = step.prompt;

                      recipeStep.bodyText.text = step.bodyText;

                      recipeStep.stepTitle.text = step.stepTitle;

                      recipeStep.minChoices = step.min;

                      recipeStep.maxChoices = step.max;

                      recipeStep.stepNumber = stepNumber;

            if (stepNumber == numSteps){

                 recipeStep.nextBtn.label = "FINISH";

            }

            recipeStep.addEventListener("GO_PREVIOUS",prevStep);

            recipeStep.addEventListener("GO_NEXT",nextStep);

            stepNumber++;

                  }

            }

             

             

            In this case, let's assume I have another data grid in a different component whose data provider I want to match the current contents of the first recipeStep's itemsChosen datagrid's data provider.  what would be the syntax to access this first step?  Through the view stack?  Or another way?

            • 3. Re: How do I access a datagrid in a component?
              Handycam Level 1

              OK, I have discovered if I use the object's name property:

               

              for each(var step:XML in dataXML){

              var recipeStep:CyorStep = new CyorStep();

              mainViewStack.addChild(recipeStep);

              recipeStep.name = "step"+stepNumber.toString();

              recipeStep.stepTitle.text = step.stepTitle;

              ...etc...

               

               

              I can later, in the same function, do:

               

              var foo:Object = mainViewStack.getChildByName("step1");

              trace (foo.stepTitle.text);

               

              But this seems round-about, and doesn't help me outside of this particular function.

               

              I feel like I am close, but it is very frustrating and if I can nail this down it will make my life a lot easier...

              • 4. Re: How do I access a datagrid in a component?
                Gregory Lafrance Level 6

                If each component has 2 datagrids, and you need to use the items that have been dragged into the second list elsewhere in the app, such as other components that show progress etc., how about simply accessing them like this:

                 

                myCompInstance.myDPCollection

                 

                or

                 

                myCompInstance.myDataGrid.dataProvider

                 

                In some cases you might need to create a set/get method to access, hard to say.

                 

                Since the original post your situation has become a bit more complex, I'm not sure if I am following it clearly enough.

                 

                Sometimes it is possible to have a MVC model that stores all the data for access by the rest of the app.

                 

                Here is my Flex Cookbook post on a simple MVC:

                 

                http://www.adobe.com/cfusion/communityengine/index.cfm?event=showDetails&postId=11246&prod uctId=2&loc=en_US

                • 5. Re: How do I access a datagrid in a component?
                  Handycam Level 1

                  No, I get what you're saying.  The only thing I think I am stuck on is a way to name the component instances I create in that loop.

                   

                  For example:

                   

                          for each(var step:XML in dataXML){

                  var recipeStep:CyorStep = new CyorStep();

                  mainViewStack.addChild(recipeStep);

                  ...

                   

                   

                  What I need to do here, following your prior post, is access recipeStep.myDataGrid.dataProvider.  This I follow.  However, this loop creates 5 recipeSteps.  How, outside of this function could I access (for example) recipeStep2.myDataGrid.dataProvider?

                  • 6. Re: How do I access a datagrid in a component?
                    Gregory Lafrance Level 6

                    In another of your posts I suggested implementing the component as an AS class, not in MXML, then you can create a constructor to take a String parameter, and use that to set this.name and/or super.name.