5 Replies Latest reply on Jul 4, 2011 7:33 AM by daslicht

    Flex 4.1 Best way to make button in Custom ItemRenderer to call function in parent

    FM_Flame Level 1

      When I push the button in the ItemRenderer I would like to call a function that is in the parent that contains the List with the ItemRenderer. Also that function should be able to get the data of that ItemRenderer.

       

      So at the moment I see only as a solution a custom event with the data of the ItemRenderer.

       

      Is it possible if for example I set a MouseEvent listener on the list and detect if that button is clicked on to call that function - I managed to do that so far as you probably figured, but now I can't get in which item was that button so I can get it's data. Going parent parent parent of that button to reach the renderer to get it's data seems kinda not the best solution.

       

      Any ideas ?

        • 2. Re: Flex 4.1 Best way to make button in Custom ItemRenderer to call function in parent
          Zolotoj Level 3

          In your item renderer you can create the following function:

          protected function clickHandler(event:Event):void
                  {       
                      var dynEvent:DynamicEvent = new DynamicEvent("removeRow", true);
                      // attach any data to the dynamic event object

                      dynEvent.rowIndex = Object(owner).rowIndex;
                      dispatchEvent(dynEvent);               
                  }

          Create this in the parent container:

          addEventListener("removeRow", removeRow);

          ..

          private function removeRow(event:DynamicEvent):void
                      {               
                          // access data using event

                         trace(event.rowIndex)
                      }

           

          HTH

          • 3. Re: Flex 4.1 Best way to make button in Custom ItemRenderer to call function in parent
            FM_Flame Level 1

            I know how to do it with events, I was asking if there is another approach or if this is the best one ?

            • 4. Re: Flex 4.1 Best way to make button in Custom ItemRenderer to call function in parent
              Zolotoj Level 3

              In my opinion, it is the best.

              1 person found this helpful
              • 5. Re: Flex 4.1 Best way to make button in Custom ItemRenderer to call function in parent
                daslicht Level 2

                Another Approach would be to pass the parent application as parameter to the item.

                 

                 

                ItemRenderer:

                 

                <?xml version="1.0" encoding="utf-8"?>
                <s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
                                    xmlns:s="library://ns.adobe.com/flex/spark" 
                                    xmlns:mx="library://ns.adobe.com/flex/mx" 
                                    autoDrawBackground="true">
                     
                     
                     <fx:Script>
                          <![CDATA[
                               [Bindable] public var _parent:ParentCall;  // or just _parent:*
                          ]]>
                     </fx:Script>
                     <s:Label text="{data}"/>
                     <s:Button x="117" y="1" label="Trigger Parent Function" click="{_parent.doit()}"/>
                     
                </s:ItemRenderer>
                

                 

                mainApplication:

                <?xml version="1.0" encoding="utf-8"?>
                <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
                                  xmlns:s="library://ns.adobe.com/flex/spark" 
                                  xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
                                  creationComplete="application1_creationCompleteHandler(event)"
                                  >
                
                     <fx:Script>
                          <![CDATA[
                               import mx.controls.Alert;
                               import mx.events.FlexEvent;
                               [Bindable] private var itemRendererFactory:ClassFactory;
                               
                               protected function application1_creationCompleteHandler(event:FlexEvent):void
                               {
                                    itemRendererFactory = new ClassFactory(mytem);
                                    itemRendererFactory.properties = { _parent:this};
                                    list.itemRenderer = itemRendererFactory;
                                    
                               }
                               
                               public function doit():void
                               {
                                    Alert.show('yeah!');
                               }
                               
                          ]]>
                     </fx:Script>
                     <s:List id='list'  >
                          <s:dataProvider>
                          <s:ArrayCollection>
                               <fx:String>item 1</fx:String>
                               <fx:String>item 1</fx:String>
                          </s:ArrayCollection>
                               
                          </s:dataProvider>
                     </s:List>
                </s:Application>