4 Replies Latest reply on Jun 22, 2010 10:08 PM by BhaskerChari

    Using Itemrenderer to do simple stuff

    armalock

      Hi all,

       

      For simple stuff like concat (e.g. {data.author}: {data.title}), it is better to use labelFunction. The problem is this kind of functionality is not available on all tags. E.g. for data grid columns, I can only specify dataField, which will cause the grid to do a lookup like data[dataField] to determine the value. My main problem is internationalization. When I have a number, date etc in my raw data, I need to convert these for display. I do not want to change the array itself since that is in my model layer. I found using item renderers and label functions to be the quickest solution. However, for simple stuff like calling convertDate( ) on the data, an item renderer sounds heavy weight. Is there another way or is this a legitimate use of item renderer?

        • 1. Re: Using Itemrenderer to do simple stuff
          Flex harUI Adobe Employee

          I'd probably still use labelFunction.

          1 person found this helpful
          • 2. Re: Using Itemrenderer to do simple stuff
            armalock Level 1

            Thanks. But how do you use labelFunction within a data grid? I have something like this:

             

            .........

             

                <mx:DataGridColumn headerText="{Localizer.i18n(RSConstants.timestamp)}" dataField="timestampColumn">

                    <mx:itemRenderer>

                      <mx:Component>

                        <mx:Label text="{Localizer.convertDateFromEpoch(data["timestampColumn"])}">

                          <mx:Script>

                            import myproject.locale.Localizer;

                          </mx:Script>

                        </mx:Label>

                      </mx:Component>

                    </mx:itemRenderer>

                  </mx:DataGridColumn>

            .........

             

             

            How can this be written without using the itemRenderer?

            • 3. Re: Using Itemrenderer to do simple stuff
              BhaskerChari Level 4

              Hi armalocak,

               

              You can do the following using the labelFunction...in itemrenderer..

               

              private function healthTotalLabelFunction(item:Object, column:DataGridColumn):String
                {
                 return convertDate(item.date);
                }


              private function convertDate(date:Date):String
              {
              var dateString:String = //write your logic here;
              return dateString;
              }

               

              <mx:DataGrid>
                 <mx:columns>
                  <mx:DataGridColumn headerText="BenefitType" width="222" color="#747474" dataField="BenefitName"/>
                  <mx:DataGridColumn headerText="Total" textAlign="right" width="87" labelFunction="healthTotalLabelFunction"/>
                 </mx:columns>
                </mx:DataGrid>

               

              Note: You can even display the data in a column by making use of labelFunction without specifying the dataField property for DataGridColumn.

               

              If you observe clearly I havent specified dataField proprty for second column...but still it will display data..because of labelFunction..

               

              If this post answers your question or helps, please kindly mark it as such.


              Thanks,

              Bhasker Chari

              • 4. Re: Using Itemrenderer to do simple stuff
                armalock Level 1

                Excellent. I was unaware that labelFunction can be used that way within a data grid . I am guessing the sorting will still work if I provide the dataField correct? (Sort on raw data is okay). Thanks!