12 Replies Latest reply on Dec 11, 2009 11:25 AM by Daniel Cantin

    Read column infos itemRenderer class

    Daniel Cantin

      Hi,

       

         I use one class I have write as itemrenderer for an AdcancedDataGridColumn. Here is the code:

       

      package

       

       

       

       

       

      vues

      {

       

       

      import mx.containers.HBox;

       

      import mx.containers.VBox;

       

       

       

      public class gridColonne extends VBox

      {

       

       

      protected override function createChildren():void

      {

       

       

      super.createChildren();

       

      var boxHaut:HBox = new HBox();

       

      var boxMilieu:HBox = new HBox();

       

      var boxBas:HBox = new HBox();

       

      boxHaut.height = 20;

      boxHaut.percentWidth = 100;

      boxHaut.setStyle(

      "backgroundColor", 0xFF0000);

      boxHaut.setStyle(

      "color", 0xFF0000);

       

       

      this.addChild(boxHaut);

       

      boxMilieu.height = 20;

      boxMilieu.percentWidth = 100;

      boxMilieu.setStyle(

      "backgroundColor", 0x00FF00);

      boxMilieu.setStyle(

      "color", 0x00FF00);

       

       

      this.addChild(boxMilieu);

       

      boxBas.height = 20;

      boxBas.percentWidth = 100;

      boxBas.setStyle(

      "backgroundColor", 0x0000FF);

      boxBas.setStyle(

      "color", 0x0000FF);

       

       

      this.addChild(boxBas);

       

      }

       

      }

       

       

      }

       

        Now I want to read the information of the AdvancedDataGrid column from my class, I have read about class that I can use (IListItemRenderer, BaseListData, IDropInListItemRenderer)., but i need some details on how it work. An example of code would be very usefull.

       

      Regards.

        • 1. Re: Read column infos itemRenderer class
          RUSH-ME Level 3

          Hi

           

          If you want to get the information about a particular column, using the owner property of the itemrenderer you can get the Advanceddatagrid. From Advanceddatagrid you can get the column by using index.

           

          Hope this helps

           

          Warm Regards

          Rush-me

          1 person found this helpful
          • 2. Re: Read column infos itemRenderer class
            Daniel Cantin Level 1

            Hi,

             

                Thanks for the answer, I have been able to read AdvancedDataGrid from my class using this code "grd = this.document.myDG;", but from there i am not able to find the current column index.

             

            Regards,

            • 3. Re: Read column infos itemRenderer class
              RUSH-ME Level 3

              Hi

               

              AdvancedDataGrid has a property called "columns" which holds all the AdvancedDataGridcolumns.  It returns array. From that array you can get the particular Column by using index if you know which column you want.

               

              If this is not what you want let me know.

               

              Warm Regards

              Rush-me

              • 4. Re: Read column infos itemRenderer class
                Daniel Cantin Level 1

                Hi,

                 

                    Thanks for the answer, I am able to read columns informations, but my problem is to get the current column and line to be able to read the correct informations treated currently by the renderer. If you need more information let me know.

                 

                Regards,

                • 5. Re: Read column infos itemRenderer class
                  paul.williams Level 4

                  This link may be useful as it shows how to access the column index for a particular renderer:

                   

                  http://blog.flexgeek.in/2007/05/tutorial-using-same-itemrenderer-for-multiple-columns/

                  1 person found this helpful
                  • 6. Re: Read column infos itemRenderer class
                    Daniel Cantin Level 1

                    Hi,

                     

                        I have consulted the article and it was very helpfull for me. The difference with my code come from the fact that I change the display format of ths cell adding childs. I have been able to read infos about the current column and I generate my renderer depending of the cell content.

                     

                       But again I have a problem, as soon as I move with the scroll bar in the grid everything is coming confused (itemRenderer not in the good cell or no renderer at all). I thing this as a relation with itemRenderer recycling process, is there a way to control this situation?

                     

                    Regards,

                    • 7. Re: Read column infos itemRenderer class
                      paul.williams Level 4

                      This is usually due to not updating the item renderer correctly when data changes. Can you post the source for your renderer?

                      • 8. Re: Read column infos itemRenderer class
                        Daniel Cantin Level 1

                        Hi,

                         

                            Thanks for your help, here is the code of my itemRenderer class and some explanation of the code.

                         

                             1: I have try others solution to format each column depending on column infos but I think that one is good regarding performance issue.

                         

                             2: Since the code of createChildren is executed before the initialization of data, I have made some coding to get the informations (colonne+line) of the previous column to be able to get the good informations of the current cell. It work because my first column don`t use the renderer so I am able to get the good information for the currently treated column when I utilize the previous position of the get data method.

                         

                             3: I am not sure it`s the good way to do this kind of thing.

                         

                        package vues
                        {

                        import mx.collections.ArrayCollection;
                        import mx.containers.HBox;
                        import mx.containers.VBox;
                        import mx.controls.dataGridClasses.DataGridListData;
                        import mx.controls.listClasses.BaseListData;
                        import mx.controls.listClasses.IDropInListItemRenderer;

                        public class gridColonne extends VBox implements IDropInListItemRenderer
                        {

                           private static var colonne:int = 1;
                           private static var ligne:int;  
                          private static var alterne:Boolean;

                                protected var _listData:DataGridListData;
                           private   var _data:Object;
                            
                          protected override function createChildren():void
                         
                          {

                           var myProvider:ArrayCollection= new ArrayCollection();
                           var leg:ArrayCollection       = new ArrayCollection();
                           var testObj:Object            = new Object();
                           var testString:String         = "";

                           myProvider = this.document.transports;
                                    leg        = this.document.legende.legendes;
                                   
                           if (colonne > 0) {

                                        testObj = myProvider[ligne];
                                     
                            switch(colonne)
                            {
                               case 1:
                                   testString = testObj.JCL0;
                                   break;
                               case 2:
                                   testString = testObj.JCC0;
                                   break;
                               case 3:
                                   testString = testObj.JCL1;
                                   break;
                               case 4:
                                   testString = testObj.JCC1;
                                   break;
                            }

                                     if (testString.length > 0) {
                                    
                             super.createChildren();
                                    
                             var boxHaut:HBox   = new HBox();
                             var boxMilieu:HBox = new HBox();
                             var boxBas:HBox    = new HBox();

                              boxHaut.height         = 20;
                                boxHaut.percentWidth   = 100;
                             boxHaut.setStyle("backgroundColor", 0xFF0000);
                             boxHaut.setStyle("color", 0xFF0000);

                             this.addChild(boxHaut);

                              boxMilieu.height       = 20;
                                boxMilieu.percentWidth = 100;
                             boxMilieu.setStyle("backgroundColor", 0x00FF00);
                             boxMilieu.setStyle("color", 0x00FF00);

                             this.addChild(boxMilieu);

                              boxBas.height          = 20;
                                boxBas.percentWidth    = 100;
                             boxBas.setStyle("backgroundColor", 0x0000FF);
                             boxBas.setStyle("color", 0x0000FF);
                          
                             this.addChild(boxBas);
                            }   

                          
                           }
                             
                                    super.validateDisplayList();
                                    colonne = 1;
                                                
                          }

                          override public function set data(value:Object):void
                          {
                           setIt(value);
                          }
                         
                          public function get listData():BaseListData
                          {
                           return _listData;
                          }
                         
                          public function set listData(value:BaseListData):void
                          {
                           _listData = DataGridListData(value);
                           invalidateProperties();
                          }
                         
                          private function setIt(value:Object):void
                          {
                              colonne = _listData.columnIndex + 1;
                              ligne   = _listData.rowIndex;
                            
                          }
                         
                        }

                        }

                         

                        Regards,

                        • 9. Re: Read column infos itemRenderer class
                          paul.williams Level 4

                          The createChildren method will only ever be called once, the first time the renderer is added to the display list. It sounds like you need it to be called each time the data is set on your item renderer. Otherwise when the renderer is recycled you'll retain the children that were created for the last item, and these may not be appropriate for the new item.

                           

                          Instead of using createChildren() I'd suggest implementing your dynamic behaviour in commitProperties. You can do this by calling invalidateProperties() from both "set data" and "set listData":

                           

                          private var _dataChanged : Boolean;

                           

                          override public function set data(value:Object):void
                          {
                             super.data = value;

                           

                             _dataChanged = true;

                           

                             invalidateProperties();
                          }

                           

                           

                          public function set listData(value:BaseListData):void
                          {
                             _listData = DataGridListData(value);

                             _dataChanged = true;

                           

                             invalidateProperties();

                          }

                           

                          Then override commitProperties() to remove any children that were created to render the previous item and create the children you require to render the current item:

                           

                          // Pseudocode

                          override protected function commitProperties() : void

                          {

                             if ( _dataChanged )

                             {

                                colonne = _listData.columnIndex + 1;
                                ligne   = _listData.rowIndex;

                           

                                if ( differentChildrenAreRequired() )

                                {

                                   removeDynamicChildren();

                                   createDynamicChildren();

                                }

                           

                                _dataChanged = false;

                             }

                          }

                           

                          This should also avoid the problem you've experienced with createChildren being called before the data is set.

                           

                          Note that the creation and destruction of children in this way will impact performance and undermine the advantages of renderer recycling. If you are just creating HBox children to display blocks of colour then you should use the Graphics api instead.

                          • 10. Re: Read column infos itemRenderer class
                            Daniel Cantin Level 1

                            Hi,

                             

                                Thanks for the answer, I have 2 questions for you,

                             

                                1: How can I remove dynamic childrens.

                             

                                2: Did I use the same method to create childrens

                             

                                 boxMilieu.height = 20;

                                 boxMilieu.percentWidth = 100;

                                 boxMilieu.setStyle(

                            "backgroundColor", 0x00FF00);

                                 boxMilieu.setStyle(

                            "color", 0x00FF00);

                             

                             

                             

                                 this.addChild(boxMilieu);

                             

                             

                            Regards,

                            • 11. Re: Read column infos itemRenderer class
                              paul.williams Level 4

                              You can create the children in the same way as you did previously. To remove them just call removeChild(). You can access the renderer's children using methods like getChildAt() and getChildByName() or you could store the children you create in set of local private variables or an array.

                              • 12. Re: Read column infos itemRenderer class
                                Daniel Cantin Level 1

                                Hi,

                                 

                                   Thanks a lot, I am going to try this solution and come back to you later. Your help has been very helpfull.

                                 

                                Thanks again.