4 Replies Latest reply on Feb 21, 2007 5:18 AM by peterent

    ItemRenderer using ClassFactory

    kishankumar
      Hi,
      I am having a problem using item render in a DataGrid using ClassFactory.
      I am using a custom item render. I have made this custom itemrender implementing the IFactory Interface.

      --MyItemRenderer.mxml--
      <mx:Hbox implements="mx.core.IFactory">
      <mx:Script>
      [Bindable]
      public var dataArray:ArrayCollection;
      </mx:Script>
      <mx:ComboBox id="combo" dataProvider="{dataArray}" />
      </mx:HBox>
      In the Application File
      --MainApplication.mxml--
      public var docCategoryComboEditor:ClassFactory;
      docCategoryComboEditor = new ClassFactory(MyItemRenderer);
      docCategoryComboEditor.properties = {dataArray:documentCategories}
      <mx:DataGrid id="dg"......
      <mx:DataGridColumn itemRenderer="DocumentCategoryComboRenderer"/>
      </mx:DataGrid>
      dg.verticalScrollPosition=2;

      I have provided only the relvant code. To explain it short, I am fetching DocumentCategories (Collection) using a remote object.
      I am instantiating ClassFactory and setting a property dataArray using the collection just fetched from the remoteobject.

      In the dataGridColumn the itemRenderer is set to the instance of the classFactory. Most of the things are working fine. But problem comes when I execute the line which changes the verticalScrollPosition. The exact error is mentioned in
      http://www.mail-archive.com/flexcoders@yahoogroups.com/msg50380.html.
      It is having problems with listItems variable of ListBase. I cannot exactly figure out the underlying problem. Please help.

      Other way I can write

      <mx:DataGridColumn itemRenderer="MyItemRenderer"/>
      This works perfectly fine. But I need to pass some properties to MyItemRenderer to make it a fully reusable itemrenderer.
      If I put the above line, then I cannot pass any properties to MyItemRenderer right? I will lose the flexibility.

      Would appreciate any help on this.

      Regards,
      Kishan


        • 1. Re: ItemRenderer using ClassFactory
          FlightGuy Level 1
          I don't see where you're actually using the factory you've created (docCategoryComboEditor). Is this because you've edited too much out, or are you really not using it? Also, I don't believe you need to implement IFactory just to use your class as the parameter to ClassFactory (which does, itself implement IFactory). You only need this if you're writing your own class factory.

          try itemRenderer="{docCategoryComboEditor}"

          Tim
          • 2. Re: ItemRenderer using ClassFactory
            peterent Level 2
            It's pretty rare to implement IFactory. We take care of the housework for you when you do: itemRender="your.class.Here".

            Ideally, an itemRenderer shouldn't need outside data - it's purpose is to use the data in the row to display whatever it is supposed to do. What kind of properties do you need to set?
            • 3. Re: ItemRenderer using ClassFactory
              kishankumar Level 1
              Hi ,
              Thanks for your reply. The purpose of passing some properties to the item renderer is to make it reusable across project.
              Say I am using combobox as a itemrenderer, I need to populate the combobox with a dataprovider. I can create a reusable combobox renderer and pass the dataprovider as the property whereever I am going to use it. This way I can reuse it. Hope you get the point. Is there any other way to create reusable item renderers?.
              By the way I searched at length about this problem, no luck.
              The work around was like, instead of specifying the itemrenderer at the <mx> definition, if I define the itemrenderer using actionscript and then call invalidate method , it is giving the required results.
              However I am really inclined to know how to make it work with the <mx> definition itself.

              Regards,
              Kishan
              • 4. Re: ItemRenderer using ClassFactory
                peterent Level 2
                I'll have to research this a little. I do know that using your own ClassFactory can aid in passing in parameters to the class. I just don't know this off the top of my head. I understand what you are trying to do and it does make sense.

                In the past when I've used a ComboBox in an itemRenderer, I had the ComboBox's data right in with the data for the row. For example, here's a row that might be for the selection of a color:

                { name:"Peter", color:{value:"Green", db:["Red","Green","Blue"]}, value: 14}

                So there are 3 fields: name, color, and value. The color field is itself an object with 2 fields: value and db. The db field is an Array to be used as the ComboBox's dataProvider.

                In this way every row could have its own unique dataProvider. Using this technique you can supply the same Array to every row and have only 1 instance of the dataProvider shared by every itemRenderer in the column.

                But I'll check out the ClassFactory some more to see if that will help you.