2 Replies Latest reply on Sep 16, 2009 9:39 AM by rfkrocktk

    Removing data items from DataGroup

    rfkrocktk Level 1

      I need to remove data items from a Spark DataGroup on an action of one of the items. My itemrenderer for that DataGroup (which extends the Spark ItemRenderer class) has a button within it that is more or less a "delete" or "remove" button. The desired functionality is to click the button and remove that item from the DataGroup. Unfortunately, I can't find a way to access the dataProvider of the DataGroup from within my ItemRenderer. Am I missing something? I'm building from 4.0.0.8883.

        • 1. Re: Removing data items from DataGroup
          Shongrunden Adobe Employee

          You can reach the DataGroup from a renderer via the parent property.

           

          Here is a quick code sample that should do what you are looking for:

           

          <s:Application
              xmlns:fx="http://ns.adobe.com/mxml/2009"
              xmlns:s="library://ns.adobe.com/flex/spark">
             
              <s:DataGroup>
                  <s:layout><s:VerticalLayout /></s:layout>
                  <s:dataProvider>
                      <s:ArrayList>
                          <fx:String>item 0</fx:String>
                          <fx:String>item 1</fx:String>
                          <fx:String>item 2</fx:String>
                          <fx:String>item 3</fx:String>
                          <fx:String>item 4</fx:String>
                      </s:ArrayList>
                  </s:dataProvider>
                  <s:itemRenderer>
                      <fx:Component>
                          <s:ItemRenderer>
                              <s:states>
                                  <s:State name="normal" />
                                  <s:State name="hovered" />
                              </s:states>
                             
                              <fx:Script>
                                  <![CDATA[
                                      import spark.components.DataGroup;
                                     
                                      public function deleteItem():void {
                                         
                                          var parentDataGroup:DataGroup = parent as DataGroup;
                                         
                                          // remove the item
                                          parentDataGroup.dataProvider.removeItemAt(parentDataGroup.dataProvider.getItemIndex(data) )
                                      }
                                  ]]>
                              </fx:Script>
                             
                              <s:Button label="Delete {data}" click="deleteItem()" />                       
                          </s:ItemRenderer>
                      </fx:Component>
                  </s:itemRenderer>       
              </s:DataGroup>
             
          </s:Application>
          • 2. Re: Removing data items from DataGroup
            rfkrocktk Level 1

            Thanks, I think that totally solves my problem