6 Replies Latest reply on Jul 7, 2008 4:32 AM by timbronze

    Flex Item Renderers for a Nuub

    timbronze
      Hello - I am a bit of a newbie to flex and I am wanting to display a custom itemRenderer based on the value in an XMLList which is used to populate the datagrid.

      My XML looks something like this...

      <list>
      <item>
      <name>John</name>
      <tel>234234</tel>
      <status>9</status>
      </item>
      <item>
      <name>Ann</name>
      <tel>234</tel>
      <status>4</status>
      </item>
      <item>
      <name>James</name>
      <tel>111</tel>
      <status>1</status>
      </item>
      </list>


      Basically, in the third column of my DataGrid i am going to be showing a custom itemRenderer based on the "status" of that item ... any tips on how to do this in mxml / actionscriptyness?

      My DataGrid is assigned a dataprovider in the form of an XML list which is obtained via a HTTPService call and all the data goes in correctly. I just need to populate this itemrenderer based on the status of each row... HELP!

      I have seen some examples on the interweb of various different ways of writing itemRenderers all looking very different so am confused about how to go about writing a simple one. All I basically need it to do is, if say the status is 3 then show a button and a text label in the "cell" while if the status is say 5 show just a label. Nothing too complicated!
        • 1. Re: Flex Item Renderers for a Nuub
          SujitG Level 2
          Hi,

          When you assign a custom itemRenderer for a column in DataGrid, property named "data" will be assigned the object (item node in your XML) corresponding that particular row in the DataGrid. So in your item renderer you can override this property setter function and do required computation. Please find sample code attached code. In the sample below in the item renderer, which is a Button component; I am setting the Button label to value in the "status" node of the XML.

          Hope this helps.
          • 2. Re: Flex Item Renderers for a Nuub
            timbronze Level 1
            Thanks SujitG - that is very useful to know :)

            I have one final query which an example would really help to solve... What if I wanted to show a simple label if the status were 1, or a button if the status were 2, how would I go about doing this? Basically, the status is going to determine what visual control I show in the cell.

            Many thanks for all your help.
            • 3. Re: Flex Item Renderers for a Nuub
              Gregory Lafrance Level 6
              Use an HTTPService to load the data, and instead of using lastResult use ResultEvent. In the result handler check the status value of each row and set the itemRenderer for each cell of each row.

              Having said this, it is possible an item renderer can only be set on a column as a whole, not differently on cells in each row in a column.
              • 4. Re: Flex Item Renderers for a Nuub
                timbronze Level 1
                Hi Greg, thanks for the reply... I am a bit of a newbie to flex - would you be able to provide a bit of sample code, or point me in the direction to where I can find some sample code that will do what you suggested? This is basically my function that i have called on the result of the httpservice...

                private function PopulateDataGrid(event:ResultEvent):void
                {
                myDataXml = event.result as XML;
                myData = myDataXml.item;
                }

                How would I modify this to do an "if the status is X, then show itemRenderer of XYZ, else if the status is Y, then show an itemRenderer of 123 on the column of the datagrid with ID = "StatusCell" and dataField="status"?


                many thanks and sorry for the nuubiness of my questions.

                Thanks,
                Dan
                • 5. Re: Flex Item Renderers for a Nuub
                  Gregory Lafrance Level 6
                  I'm thrilled I actually got this to work. Hope it is of practical use to you, or gets you started:
                  • 6. Re: Flex Item Renderers for a Nuub
                    timbronze Level 1
                    Greg that is perfect- thank u!