Home > Flex 4 > Multiple selection in a spark List without the control key

Multiple selection in a spark List without the control key

The spark List component in Flex 4 allows you to select multiple items when you set allowMultipleSelection to true. With this flag set you can select multiple items by using the control/command key.

If you want to have a List that allows multiple selection, but does not require the user to hold down the control key when selecting items you can subclass List and override the item_mouseDownHandler() method.

The following example demonstrates a subclass of List called CheckList that does just that:

View Source

This sample uses an item renderer function to use a different renderer based on if the item is a heading or not. You can still select these heading items, but the heading renderer doesn’t look any different when it’s selected. Examining the selectedItems property of the List will show that the heading items are actually selected. If you don’t want to allow these items to be selected check out this post.

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

  1. December 14th, 2009 at 10:29 | #1

    Thanks, this is extremely helpful!

  2. Steven Shongrunden
    December 14th, 2009 at 11:46 | #2

    Great – Glad it helped :)

  3. anton
    September 28th, 2010 at 14:28 | #3

    override protected function item_mouseDownHandler(event:MouseEvent):void
    {
    event.ctrlKey=true;
    super.item_mouseDownHandler(event);
    }

  4. Steven Shongrunden
    September 30th, 2010 at 21:13 | #4

    @anton – Nice and simple, thanks for pointing that out

  5. November 16th, 2010 at 23:40 | #5

    Thanks for this! I was searching for a while — very simple.

    Here’s a tough one for you — I have an item renderer for my s:List which has a button inside of it. Thing is, I don’t want the selection to change if the button is clicked — only if anything other than the button is clicked. How would I go about cancelling the event propagation?

    Thanks!

  6. November 16th, 2010 at 23:53 | #6

    Answered my own question:

    override protected function item_mouseDownHandler(event:MouseEvent):void
    {
    if(event.target.id != “tableBtn”) {
    var newIndex:Number = dataGroup.getElementIndex(event.currentTarget as IVisualElement);

    // always assume the Ctrl key is pressed by setting the third parameter of
    // calculateSelectedIndices() to true
    selectedIndices = calculateSelectedIndices(newIndex, event.shiftKey, true);
    }
    }

  7. Steven Shongrunden
    November 17th, 2010 at 00:30 | #7

    @Mike Haggerty – Good question, thanks for posting your solution. Another slightly more generic solution might be like this:

    override protected function item_mouseDownHandler(event:MouseEvent):void {
        if (event.target is ItemRenderer){
            event.ctrlKey = true;
            super.item_mouseDownHandler(event);
        }
    }

    (This is also simplified from my original post by following anton’s suggestion)

    Then in your ItemRenderer you would set mouseEnabled=”false” on elements you want to cause the renderer to be selected, for example the Label.

  8. er
    January 10th, 2011 at 06:58 | #8

    thank alot this what i need 10x

  9. huuck
    February 14th, 2011 at 10:09 | #9

    @anton
    PRO. You sire, have been give all of my Internets.

  10. March 25th, 2011 at 15:13 | #10

    I should have done it this way!!!! Thank you for the idea. I had to simulate this behavior using the List component. But I don’t know why I didnt think of doing a custom Sub List class. :( Thanks again!!

  1. March 22nd, 2010 at 19:06 | #1
  2. March 24th, 2010 at 08:39 | #2