21
Nov
07

Selecting multiple items in Flex List and DataGrid controls

The following example shows how you can have multiple selected items in a List or DataGrid control at the same time. To select multiple items at once, hold down the Shift or Control keys on your keyboard while pressing the mouse button on a list item.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/11/21/allowing-multiple-selected-items-in-flex-list-and-datagrid-controls/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="horizontal"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Array id="arr">
        <mx:Object col1="One.1" col2="One.2" col3="One.3" />
        <mx:Object col1="Two.1" col2="Two.2" col3="Two.3" />
        <mx:Object col1="Three.1" col2="Three.2" col3="Three.3" />
        <mx:Object col1="Four.1" col2="Four.2" col3="Four.3" />
        <mx:Object col1="Five.1" col2="Five.2" col3="Five.3" />
        <mx:Object col1="Six.1" col2="Six.2" col3="Six.3" />
        <mx:Object col1="Seven.1" col2="Seven.2" col3="Seven.3" />
        <mx:Object col1="Eight.1" col2="Eight.2" col3="Eight.3" />
    </mx:Array>

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="allowMultipleSelection:">
                <mx:CheckBox id="checkBox" selected="true" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:List id="list"
            allowMultipleSelection="{checkBox.selected}"
            dataProvider="{arr}"
            labelField="col1"
            verticalScrollPolicy="on" />

    <mx:DataGrid id="dataGrid"
            allowMultipleSelection="{checkBox.selected}"
            dataProvider="{arr}"
            verticalScrollPolicy="on" />

</mx:Application>

View source is enabled in the following example.


9 Responses to “Selecting multiple items in Flex List and DataGrid controls”


  1. 1 Terence Nov 29th, 2007 at 9:24 am

    how do you obtain the multiple selected through event on change?

    i know u can get 1 object through

    var selectedItem:Object=event.target.selectedItem;

    how do u get all the objects that are selected?

  2. 2 peterd Nov 29th, 2007 at 10:42 am

    Terence,

    You can use the selectedItems property instead of selectedItem.
    The selectedItems property returns an array of objects representing all the selected items.

    For more information, see the selectedItems property in the ListBase class in the Flex documentation.

    Hope that helps,
    Peter

  3. 3 Terence Nov 29th, 2007 at 10:54 am

    thank you. that worked :D

  4. 4 Reda Makhchan May 12th, 2008 at 10:56 am

    slt Peter,

    juste une truc pour selectedItems c’est un array, c’est bien, mais comment je peut tester si une valeur string exist sur ce derniers?

    ...
    if(item.Idracine == myListe.grdLigne.selectedItems.Idracine)
    ...
    

    merci d’avance pour ce site Peter :)

  5. 5 peterd May 12th, 2008 at 4:37 pm

    Reda Makhchan,

    Sorry, I’m not sure I understand your question, but does this help?

    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
            layout="vertical"
            verticalAlign="middle"
            backgroundColor="white">
    
        <mx:Script>
            <![CDATA[
                import mx.controls.Alert;
                import mx.events.ListEvent;
    
                private function list_change(evt:ListEvent):void {
                    var item:String = "Four";
                    var idx:int = list.selectedItems.indexOf(item);
                    if (idx > -1) {
                        Alert.show("Item was selected.", "index = " + idx);
                    } else {
                        Alert.show("Item was not selected.");
                    }
                }
            ]]>
        </mx:Script>
    
        <mx:Array id="arr">
            <mx:String>One</mx:String>
            <mx:String>Two</mx:String>
            <mx:String>Three</mx:String>
            <mx:String>Four</mx:String>
            <mx:String>Five</mx:String>
            <mx:String>Six</mx:String>
            <mx:String>Seven</mx:String>
            <mx:String>Eight</mx:String>
        </mx:Array>
    
        <mx:List id="list"
                allowMultipleSelection="true"
                dataProvider="{arr}"
                labelField="col1"
                verticalScrollPolicy="on"
                width="100"
                change="list_change(event);" />
    
    </mx:Application>
    

    Peter

  6. 6 Reda Makhchan May 13th, 2008 at 2:55 am

    hello Peter,

    Sorry I spoke french, well what I want to say is :

    I’ve an two arrays :

    the first is affacted to dataprovider in a grdLigne (allowmultiSelect = true)
    so I can select ligne 11, ligne 12 … and I need to get the concerned value from a second array.

    Ithink we get the selected value 1 by 1 with:
    grdLigne.selectedItems[0].ligne,grdLigne.selectedItems[1].ligne …

    and I need to filter that array by selected lignes (filterFunc):


    arrCol2.filterFunction = processFilter;

    private function processFilter(item:Object):Boolean {
    //will not work
    if(item.ligne == grdLigne.selectedItems.ligne)
    return true;
    else return false;
    }
    any idea?

    that what I was talking about ;), sorry and thks in advance Peter

  7. 7 Reda Makhchan May 13th, 2008 at 2:59 am

    And About ‘IndexOf’ it works for a simple array yes, but I don’t think it works in this case :(

  8. 8 Halfway there... May 21st, 2008 at 1:50 pm

    Thats swell (and easy), so how can you set more than one selected item using ActionScript?

  9. 9 Lance Pollard Sep 7th, 2008 at 10:50 am

    Hey Peter,

    I love this website, I use it almost everyday. 99% of the stuff on this you can’t find anywhere else. Thanks so much.

    Peace,
    Lance

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".