4 Replies Latest reply on Jul 12, 2011 3:00 AM by mikeyin85

    How to reference an itemRenderer from the main application MXML file?

    pbesi

      Hello.

      Here my problem.

       

      I've a spark datagrid in my main mxml application file. One of its columns has an itemrender, more precisaly a GridItemRenderer. Here the grid:

       

       

       

      <s:DataGrid id="societaPersonasGrid"
      dataProvider="...">
      
           <s:columns>
                <s:ArrayList>
                    <s:GridColumn....>
                    <s:GridColumn....>
                     <s:GridColumn id="contactReference"  itemRenderer="renderers.GridCheckBoxRenderer"/>
                </s:ArrayList>
           </s:columns>
      </s:DataGrid>
      

       

       

       

      My GridItemRenderer is the following mxml file:

       

       

       

      <s:GridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
      xmlns:s="library://ns.adobe.com/flex/spark" 
      xmlns:mx="library://ns.adobe.com/flex/mx">
      
      
      <s:HGroup left="35">
      <s:CheckBox id="rifOwner" enabled="false"/>
      </s:HGroup>
      
      </s:GridItemRenderer>
      
      
      

       

      As you can see I've a checkbox. I need to change its state from the main application via actionscript. My problem is that I don't know how to reference the checkbox, it's id "rifOwner" is not available.

      I also tried to work with the inline itemrenderer solution, (the itemrender definition is inside the main application file using the <fx:Component> tag ) but again, I'm not able to reference the checkbox id from actionscript.

      Can anyone help me?

       

      Thank you

       

      Pbesi

       

       

        • 1. Re: How to reference an itemRenderer from the main application MXML file?
          Flex harUI Adobe Employee

          Renderer visuals should be data-driven.  The renderer should be watching

          some information in the app and change state when it changes.  Otherwise, it

          will get messed up when scrolling.

           

          For Spark DG, the renderers have a prepare() event that you can override and

          then add a listener to some flag to determine the state.

          • 2. Re: How to reference an itemRenderer from the main application MXML file?
            kokorito Level 4

            easiest way is through the dataProvider, and then bind properties with the renderer data property

             

            for instance, you could have a property in the dataprovider call checkboxEnabled

            then in the itenrenderer you have enable="{data.checkBoxEnabled}"

             

            so to change the state of the checkbox you would change the dataProvider and then update the grid.

             

            similarly you need to bind the selected property of the checkbox to a property in the dataProvider

            • 3. Re: How to reference an itemRenderer from the main application MXML file?
              pbesi Level 1

              harUI,

              Thank you.

               

              Ok, in my main app, I wrote the following code:

               

              dispatchEvent(new Event('eventTest',true));

               

              In my itemrenderer I wrote:

               

              <s:GridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
                                  xmlns:s="library://ns.adobe.com/flex/spark" 
                                  xmlns:mx="library://ns.adobe.com/flex/mx"
                                  initialize="init()">
                   
                   <fx:Script>
                             <![CDATA[
                             
                                       import mx.controls.Alert;
              
                             
                                       private function init():void{
                                            addEventListener('eventTest',testMethod);
                             }
                             
                             private function testMethod():void
                             {
                                            Alert.show("hello!");
                             }
              

               

              Both the dispatchEvent and addEventListener are executed. But the method "testMethod" is not execeuted after the dispatchEvent.

              It seems it is not triggered!

               

              Do you have any suggestion please?

               

              Thank you

              Pbesi

              • 4. Re: How to reference an itemRenderer from the main application MXML file?
                mikeyin85

                Change your

                 

                addEventListener

                to

                parentDocument.addEventListener (or any other object that dispatches the event)

                 

                Note: the dispatcher object and listener object must be the same one.

                In your case, the  dispatcher object isn't the render component, but the addEventListener is wroted in your render.

                 

                Regards,

                Mike