5 Replies Latest reply on May 14, 2009 2:25 PM by Michael Borbor

    Send a value to an itemRenderer

    SiHoop Level 1

      Is it possible to send a value to an itemRenderer? I'm using an itemRenderer to format the cells of a datagrid, and want to use a VSlider to change the criteria that determine how the cells are formatted. I created a public variable in the renderer, and created an instance of the renderer in the main file. However, if I try to access the variable from the main file, I get an error. If what I'm doing is wrong, how can I send a variable to the itemRenderer?

      Thanks!

       

      Here's the main file:

      <?xml version="1.0"?>
      <mx:Application backgroundGradientColors="[0xA30900, 0xE22B03]" xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="srvc.send();">               
      <mx:Script>
           <![CDATA[
           import mx.rpc.events.ResultEvent;
          import mx.controls.dataGridClasses.DataGridColumn;
          import mx.collections.XMLListCollection;
          private var someVal:ColoredBackgroundItemRenderer
           [Bindable] public var itemData:XMLListCollection ;   
           private function dataHandler(event:ResultEvent):void{
                itemData = new XMLListCollection(event.result.EXAMPLE1 as XMLList);
          }
           private function changeSize():void{
                //trace("qq="+someVal.myVar)
                someVal.myVar=hSlider.value
          }
           ]]>
      </mx:Script>
      <mx:HTTPService id="srvc" url="example1.xml" result="dataHandler(event)" resultFormat="e4x"/>
      <mx:Canvas backgroundColor="0xffffff" height="500" width="1000"> 
                                         
           <mx:DataGrid  dataProvider="{itemData}"   rowCount="20">
                <mx:columns>
                     <mx:DataGridColumn id="column1"  headerText="Item Number" dataField="item" textAlign="center"  itemRenderer="ColoredBackgroundItemRenderer" />
                     <mx:DataGridColumn id="column2" headerText="Difficulty" dataField="difficulty" textAlign="center" itemRenderer="ColoredBackgroundItemRenderer" />
                     <mx:DataGridColumn id="column3"  headerText="Discrimination" dataField="discrimination" textAlign="center" itemRenderer="ColoredBackgroundItemRenderer"/>
                     <mx:DataGridColumn headerText="Remove Item" textAlign="center" itemRenderer="ColoredBackgroundItemRendererCheckBox" />
                </mx:columns>
           </mx:DataGrid>
          <mx:VSlider id="hSlider" 
          dataTipPlacement="top" 
          minimum="0" maximum="1" value=".5" 
          change="changeSize();" x="487" y="168"/>
      </mx:Canvas>
      </mx:Application>
      
      
      

        and here's the renderer:ColoredBackgroundItemRenderer.mxml

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Label xmlns:mx="http://www.adobe.com/2006/mxml">
        <mx:Script>
          <![CDATA[
           import flash.display.Graphics;
           public var myVar:Number=.5
           override protected function updateDisplayList(unscaledWidth:Number, 
           unscaledHeight:Number):void{
                super.updateDisplayList(unscaledWidth,unscaledHeight);     
                var g:Graphics = graphics;
                var colors:Array
                if(data.difficulty > myVar){
                     colors=[0x0000CC]
                }else if(data.difficulty > .3){
                     colors=[0x00CC00]
                }else{
                     colors=[0xCC0000]
           }
           g.clear();
           g.beginFill(colors[0], .5);          
           g.drawRect(0, -2, unscaledWidth, unscaledHeight+4 );
           g.endFill();
            }
          ]]>
        </mx:Script>
      </mx:Label>
      
      

       

      My xml file is:example1.xml

       

      <?xml version="1.0" encoding="windows-1252" ?>
      <TABLE>
         <EXAMPLE1>
            <difficulty> 0.2 </difficulty>
            <discrimination> 0.2 </discrimination>
            <item> 1 </item>
         </EXAMPLE1>
      </TABLE>

        • 1. Re: Send a value to an itemRenderer
          _Natasha_ Level 4

          Hi,

           

          of cource you get a error at this code: someVal is null, so you can't set any properties.

           

          You can set your variable to be static and set it on initialization. But it would be the same for all itemRenderers.

           

          The best way to send a value to itemRenderer - data. You can add this parameter to each itemData.

          • 2. Re: Send a value to an itemRenderer
            SiHoop Level 1

            Thanks for the comment about someVal being null; that makes sense.

            Would you explain what you mean about sending a value to the itemRenderer though data. Do you mean that I would add an attribute/ element to the XMLListCollection, update that value when the VSlider changes, then access it through attribute.data in the renderer?

            • 3. Re: Send a value to an itemRenderer
              _Natasha_ Level 4

              You can bind Slider value to xml. (See example below)

              And after that in itemRenderer you can get the limit instead of myVar.

               

               

              <?xml version="1.0" encoding="windows-1252" ?>
              <TABLE>
                 <EXAMPLE1>
                    <difficulty> 0.2 </difficulty>
                    <discrimination> 0.2 </discrimination>
                    <item> 1 </item>

                   <limit>{mySlider.value}</limit>
                 </EXAMPLE1>
              </TABLE>

              • 4. Re: Send a value to an itemRenderer
                SiHoop Level 1

                I understand what you are saying about changing the xml and binding the value of the slider to the xml element. I think that gives me a way to access data.limit inside the renderer. What I don't understand is how I update the dataProvider (i..e itemData) in the main file. I assume that if I update the dataProvider, then data.limit will change automatically in the renderer. Would you help me to understand how to update the dataProvider to reflect the changing value of the VSlider?

                Thanks again.

                 

                • 5. Re: Send a value to an itemRenderer
                  Michael Borbor Level 4

                  Probably you should read this, it's the first part of a series that talks about item renderers, it'll be useful to understand the ins and outs of them.

                   

                  http://www.adobe.com/devnet/flex/articles/itemrenderers_pt1.html