2 Replies Latest reply on Apr 16, 2010 7:36 AM by Zolotoj

    Set data and custom item renderer

    Zolotoj Level 3

      We usually override set data when we assign data to a custom item renderer when it's used in a datagrid.

      When new record is added to datagrid how can I trigger set data to assign data to a custom renderer in the new record?

       

      Thanks

        • 1. Re: Set data and custom item renderer
          Flex harUI Adobe Employee

          If a new record is added to the ArrayCollection, the DG should create a new

          renderer and assing its data for you.

          • 2. Re: Set data and custom item renderer
            Zolotoj Level 3

            Yes, that's right. But in my situation it is a bit different. Let me show some code.

            Here is my custom item renderer:

            comboBoxCheckItemRendererDataGridDynamic.mxml

            <?xml version="1.0" encoding="utf-8"?>
            <mx:ComboBox xmlns:fx="http://ns.adobe.com/mxml/2009" implements="mx.controls.listClasses.IDropInListItemRenderer" 
                            xmlns:s="library://ns.adobe.com/flex/spark" 
                            xmlns:mx="library://ns.adobe.com/flex/mx">
                 <fx:Script>
                      <![CDATA[
                           import mx.collections.ArrayCollection;
                           import mx.controls.CheckBox;
                           import mx.controls.DataGrid;
                           import mx.controls.listClasses.BaseListData;
                           import mx.controls.listClasses.IDropInListItemRenderer;
                           import mx.events.FlexEvent;
                      [Bindable]     
                      public var datas:XML;
                      override public function set data(value:Object):void
                      {
                           super.data = value;
                           trace(1);               
                           if(value != null)
                           {
                                dataProvider = datas["sku" + value.sku];
                           }     
                      }                    
                      public function saveCheckState(evt:Event):void
                      {
                           var dataProviderItem:Object = dataProvider.getItemAt(dataProvider.getItemIndex(selectedItem));
                           dataProviderItem.selected = CheckBox(evt.currentTarget).selected;
                           dataProvider.setItemAt(dataProviderItem, dataProvider.getItemIndex(selectedItem));     
                      }
                      ]]>
                 </fx:Script>
                 <mx:itemRenderer>
                      <fx:Component>
                           <mx:HBox width="100%">
                                <mx:Label text="{XML(data).action}"/>
                                <mx:Spacer width="100%"/>
                                <mx:CheckBox id="check" selected="{XML(data).selected == 'true'}"
                                                change="outerDocument.saveCheckState(event);"/>
                           </mx:HBox>
                      </fx:Component>
                 </mx:itemRenderer>
            </mx:ComboBox>
            
            It is a combobox with checkboxes for multiple selections.
            Implementation:
            [Bindable]
            var plannedAtions:ClassFactory = new ClassFactory(comboBoxCheckItemRendererDataGridDynamic);
             
            When a new record is added to datagrid a dataProvider for this renderer is not set because there is no data for it yet. 
            When a user has entered a sku the server returns data for the entered sku and that data is added to datas:
            XML(plannedAtions.properties.datas).appendChild(resultXML.children());
            dataGrid.invalidateList();
            So, I was able to solve my initial problem of refreshing the item renderer.
            But now I am having another rather weird problem. My code works fine as long as 
            I dont check any check box prior adding a new record. If I do then
            XML(plannedAtions.properties.datas).appendChild(resultXML.children());
            does not add new data and as a result I dont see dropdown data in the new record. 
            Any possible idea?
             
            Thanks