4 Replies Latest reply on Apr 7, 2009 9:07 AM by florian_h

    [DataGrid/item renderer] refer to the dataField, not to the whole row


      My Hello,


      I have a DataGrid and an arraycollection as DataProvider with a custom item renderer to display the data:

      var arrCol:ArrayCollection = new ArrayCollection;
      arrCol.addItem( {title:"Title", col1:"Column 1", col2:"Column 2", col3:"Column 3"} );
      <mx:Component id="myCustomIR"> 
           <!-- how to find out the current column? -->
           <mx:Label text="{data.col1.toString()}" /> <!-- this refers to col1, so it doesn't work for col2 and col3 -->
      <mx:DataGrid dataProvider="{arrCol}">
                <mx:DataGridColumn dataField="title">
                <mx:DataGridColumn dataField="col1" itemRenderer="{myCustomIR}"/>
                <mx:DataGridColumn dataField="col2" itemRenderer="{myCustomIR}"/>
                <mx:DataGridcolumn dataField="col3" itemRenderer="{myCustomIR}"/>
      <!-- desired output: [ Title ] [ (label)Column 1 ] [ (label)Column 2 ] [ (label)Column 3 ] -->



      My problem is how to find out, which column's data the IR should display, because the "data"-Object which the itemRenderer uses to access the data, refers to the currently selected row, but doesn't refer to the dataField which that specific column is ment to display.


      In the above code example, I use the same IR for all three 'colX'-Columns. But how can I tell the item renderer sth like 'display the value of the dataField of the column you are rendering at the moment!' ?

      (actually this example is pretty much simplified, as I wouldn't need an item renderer just to display the value of the dataField, but it is a little bit more complex in my case)


      I hope I could explain my problem and anybody has a hint for me


      Best regards,

      florian h

        • 1. Re: [DataGrid/item renderer] refer to the dataField, not to the whole row
          florian_h Level 1

          I edited the first post as I think it was a little bit ambiguous .... or is this just impossible to do?

          • 2. Re: [DataGrid/item renderer] refer to the dataField, not to the whole row



            Create new component [file -> new - > MXML component]

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Canvas  xmlns:mx="http://www.adobe.com/2006/mxml" width="400"  height="34" horizontalScrollPolicy="off" verticalScrollPolicy="off">
              <mx:Label   text="{data.name}" />

              <mx:Label  text="{data.password}"  />


            add it to your dataGrid as itemRenderer

            for dataProvider you will have arrayCollection of  NewObject (name,password)


            create new class:

            package com
                public class NewObject extends  Object
                    public var name:String = "";
                    public var  password:String = "";
                    public function NewObject (name:String,password:String)
                        this.name= date;
                         this.password= day;


            and your dataProvider will be like this one :


            public var myList:ArrayCollection = new ArrayCollection([
                            new NewObject ("joe","123"),
                             new NewObject ("dummy",1234")

            • 3. Re: [DataGrid/item renderer] refer to the dataField, not to the whole row
              florian_h Level 1

              Thanks for your reply! But I think you missunderstood me (because of my ambiguous explanations )



              I have one item renderer component and want to use it for different colums with different data fields (but of the same type) ...so the item renderer has to determine in which column it is, to use the correct dataField (defined for the column of the dataGrid) data.

              I made a little illustration (I'm rly bad in explaining things, maybe this will make it clearer):



              Btw: This could be done by creating three different item renderers , one using "data.field1", the other "data.field2" and the third using "data.field3", but this is pretty much a bad solution I think (most notably if there are even more columns) ... there has to be a way to determine the dataField of the current column in which the item renderer is...?!



              Best regards,


              • 4. Re: [DataGrid/item renderer] refer to the dataField, not to the whole row
                florian_h Level 1

                Okay, I found a workaround which is quite fine for me: I'm using a horizontalList as item renderer in the DataGrid cell to display those items (with another item renderer inside the horizontalList) ... thank's for your help so far!