Home > Flex 4 > Deleting items in a List from an item renderer

Deleting items in a List from an item renderer

It’s easy to delete an item in a spark List from within an item renderer. This is handy if you have a List with a custom renderer that provides a button to delete the item that is associated with that renderer.

You can do this by using the ItemRenderer’s owner property to access the List it is in, drill down to its dataProvider and delete the data item:

<s:ItemRenderer>
    <fx:Script>
        <![CDATA[
                import spark.components.List;
                public function deleteItem():void {
                    var parentList:List = owner as List;
                    // remove the item
                    parentList.dataProvider.removeItemAt(parentList.dataProvider.getItemIndex(data))
                }
            ]]>
        </fx:Script>
        <s:HGroup>
            <s:Label text="{data}" />
            <s:Button id="remove" label="X"  click="deleteItem()"/>
        </s:HGroup>
</s:ItemRenderer>

Another approach is to delete items based on the selectedItem/selectedIndex, but I prefer this method since it allows you to delete items without needing to select them.

Here is an example that uses the deleteItem() method listed above:

View Source

This should work the same way for DataGroup and SkinnableDataContainer as well.

Note: This sample requires Flex SDK 4.0.0.10545 or higher. You can get the latest SDK builds from opensource.adobe.com.

  1. felix
    June 1st, 2010 at 18:51 | #1

    yay! thanx for the tips!

  2. mzx
    December 29th, 2010 at 02:42 | #2

    nice, but is it anywhere documented that itemRenderer’s owner is ( and still in later sdk’s ) List control itself?

  3. Steven Shongrunden
    December 29th, 2010 at 12:22 | #3

    @mzx – Yes, this is officially documented in the DOM spec here: http://opensource.adobe.com/wiki/display/flexsdk/Gumbo+DOM+Tree+API

    This blog post might also be interesting as it demonstrates the difference between parent and owner:
    http://flexponential.com/2009/12/08/differences-between-ivisualelement-parent-and-ivisualelement-owner/

  4. Luciana Cuarelli
    December 27th, 2011 at 03:31 | #4

    It’s exactly what I was googling for :D
    Thank you xD

  1. No trackbacks yet.