3 Replies Latest reply on Apr 10, 2008 1:09 AM by peterskeeter

    DataGrid itemRenderer done in code?

    michael_teter
      I've searched for info on how to implement itemRenderer for a DataGrid, and despite seeing some docs and examples, they're all very different from what I think I want, and what I think should also be possible.

      Here's what I want: for one column (called "alert"), I want to be able to programmatically display an appropriate image (or even just render a character, such as "X") based on the data value of that cell.

      I would think it should be something like this:

      <mx:Script>
      ...
      private function RenderAlert(... ? ) :String {
      if (data == "delayed") return "D"
      else if (data == "canceled") return "X"
      }
      </mx:Script>

      <mx:DataGrid ...>
      <mx:columns>
      <mx:DataGridColumn headerText="Alert" dataField="alert" itemRenderer="RenderAlert"/>
      </mx:columns>
      </mx:DataGrid>

      So, if anyone can point me to an example like this, I'd be very appreciative. I'm really hoping it's possible, as all the other methods I've seen illustrated don't appear to handle the render decisions in code.

      Thanks!
        • 1. Re: DataGrid itemRenderer done in code?
          hs_ted Level 1
          This has many good examples as well as explanations: http://weblogs.macromedia.com/pent/
          • 2. Re: DataGrid itemRenderer done in code?
            michael_teter Level 1
            Thanks for the link to the examples.

            Since I didn't see exactly what I'm trying to do (I saw other good things), I'll ask a different question:

            In a DataGrid, is each cell rendering a label control inside it?

            I had this impression that I could set an itemRenderer="{myfunction}" in the DataGrid definition, and elsewhere in script I could define myfunction() and have it return a string (or perhaps an image). Perhaps my overriding renderer needs to somehow create a component, then pass that back to the Grid or otherwise reach in and alter the grid's data to show my preferred control/value.
            • 3. Re: DataGrid itemRenderer done in code?
              peterskeeter
              By default, I'm not exactly sure what each cell is.I'm sure if we dug into the docs somewhere it would tell us..
              I've always done any conditional stuff in my itemrenderer component.
              Specifically in the set data function which you should override:
              i.e the following is a snippet that sets a combobox itemrenderer in a datagrid. It gets the ID of the current object being rendered then looks it
              up in an arraycollection to get the "english/display name" of that ID then sets that as the selected item in the combo box

              override public function set data(value:Object):void {
              yy = this.parentApplication.AC_semesters;
              if ((value != null) && (yy != null) && yy.length > 0) {
              a_course = Course(value);
              this.invalidateProperties();
              for (var i:int = 0; i < yy.length; i++)
              {
              var firstItem:Object = yy.getItemAt(i);
              var myid:int = a_course.crs_semester;
              if (firstItem.sem_id == myid)
              cb3.selectedIndex = i;
              }
              }
              }

              so in your case you could make up an itemrenderer component being a label, then in your set data function, have a conditional check to set the label.text = "X" if the value passed in is equal to whatever.
              Hope this helps...