2 Replies Latest reply on Jan 7, 2010 5:14 AM by archemedia

    ItemRenderer for DataGrid

    archemedia Level 4

      Here's a nice problem

       

      Please test the code below.

      As you will see, it works fine! The scores (7) are presented neatly. That's because in the itemRenderer for column 2, I bind the text property to

      data.item2.score.

      BUT

      In my application, I DON'T KNOW WHAT THIS PROPERTY WILL BE because the dataProvider is generated dynamically. The names of the properties are generated dynamically using a prefix ("item") and a suffix which is an id from the database. So the dataProvider could hold the props 'item325' and 'item23' and so on...

      What I want to do is to store the name of the property in the labelField of the column (as I did in the example). The itemRenderer should now get its data using that labelField.

      In my ItemRenderer which says now 'text="{data.item2.score}"' should be 'text="{data[referenceToLabelField].score}"'

       

       

      Any Ideas?

      thx;

      d

       

       

       

       

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
          <mx:Script>
              <![CDATA[
                  import mx.collections.ArrayCollection;
                 
                  [Bindable]
                  private var dp:ArrayCollection = new ArrayCollection();
                 
                  private function init():void
                  {
                      var a:Array = new Array();
                      for (var i:int = 0; i < 10; i++)
                      {
                          a.push({pupil: "Pupil " + i, item2: {score: 7}});
                      }
                      dp.source = a;
                  }
                 
                 
              ]]>
          </mx:Script>
          <mx:DataGrid dataProvider="{dp}">
              <mx:columns>
                  <mx:DataGridColumn headerText="Column 1" dataField="pupil" width="200"/>
                  <mx:DataGridColumn headerText="Column 2" dataField="item2" width="200">
                      <mx:itemRenderer>
                          <mx:Component>
                              <mx:Label text="{data.item2.score}" width="10"/>
                          </mx:Component>
                      </mx:itemRenderer>
                  </mx:DataGridColumn>
              </mx:columns>
          </mx:DataGrid>
         
      </mx:Application>

        • 1. Re: ItemRenderer for DataGrid
          funkysoul Level 4

          In my opinion, it's wrong using variable names, you should always reference default field names in your example as item2.

           

          Now you need to explain me why exactly you want to get the ID's from the database, I assume you are creating a game, therefore my next advice would be to create a db entry for every time someone plays the game, and fill the db fields structured, therefore you will always know how to get all the details without having to care too much with the ID's.

           

          If the above is not an option then you could also get the ID before you call the datagrid and concatenate the id with the item string and pass it to the datagrid.

          • 2. Re: ItemRenderer for DataGrid
            archemedia Level 4

            The project I'm working on is a school test generator. I created a test generator. Let's say I create a test named "Final exam". In this test I link a student group and I add as much testItems as I want. Let's say I make 2 testItems named "Written result" and "Attendance during schoolyear". Now, my application will generate scores for each student and for each item.

             

            We'll generate a dataGrid as:

             

                                Written result          Attendance during schoolyear

            Pupil 1               7                                   5

            Pupil 2               6                                   9

            Pupil 3               8                                   2

             

             

            In this simple example, my dataProvider could look like this:

             

            [

            {pupil: "Pupil 1", score1: 7, score2: 5},

            {pupil: "Pupil 2", score1: 6, score2: 9},

            {pupil: "Pupil 3", score1: 8, score2: 2}

            ]

             

            Setting the labelField to 'score1' and 'score2' will work.

            So far, so good! My problem however is that the scores are not just figures but Objects! Why? Because my system makes it possible to VIEW the scores in diffrent ways, such as figures, smileys, checkboxes etc. The diffrent columns in my grid will each have a diffrent itemRenderer. The itemRenderers will take properties from their dataObject to render the output. My dataProvider looks like:

             

            [

            {pupil: "Pupil 1", score1: {score: 7, foo: 'whatever'}, score2: {score: 5, foo: 'whatever'}},

            {pupil: "Pupil 2", score1: {score: 6, foo: 'whatever'}, score2: {score: 9, foo: 'whatever'}},

            {pupil: "Pupil 3", score1: {score: 8, foo: 'whatever'}, score2: {score: 2, foo: 'whatever'}}

            ]

             

            Setting the labelField to 'score1' and 'score2' won't work!


            The data property for the itemRenderer of column 2 should be

                 {score: 7, foo: 'whatever'}

            instead of

                 {pupil: "Pupil 1", score1: {score: 7, foo: 'whatever'}, score2: {score: 5, foo: 'whatever'}}

             

            Does that make things clearer?

             

            Thx,

             

            d