Preventing a user from selecting an item in a Flex DataGrid control

by Peter deHaan on February 1, 2008

in DataGrid

The following example shows how you can prevent a user from selecting items in a Flex DataGrid control by setting the selectable property.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/02/01/preventing-a-user-from-selecting-an-item-in-a-flex-datagrid-control/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.events.ListEvent;
            import mx.controls.Alert;

            private function dataGrid_itemClick(evt:ListEvent):void {
                Alert.show(evt.itemRenderer.data.label, evt.type);
            }
        ]]>
    </mx:Script>

    <mx:Array id="arr">
        <mx:Object label="Alert" />
        <mx:Object label="Button" />
        <mx:Object label="ButtonBar" />
        <mx:Object label="CheckBox" />
        <mx:Object label="ColorPicker" />
        <mx:Object label="ComboBox" />
    </mx:Array>

    <mx:ApplicationControlBar dock="true">
        <mx:CheckBox id="checkBox"
                label="selectable:"
                labelPlacement="left"
                selected="true" />
    </mx:ApplicationControlBar>

    <mx:DataGrid id="dataGrid"
            dataProvider="{arr}"
            selectable="{checkBox.selected}"
            rowCount="4"
            width="200"
            change="dataGrid_itemClick(event);"
            itemClick="dataGrid_itemClick(event);">
        <mx:columns>
            <mx:DataGridColumn dataField="label" />
        </mx:columns>
    </mx:DataGrid>

</mx:Application>

View source is enabled in the following example.

As you can see, if you click an item in a DataGrid control when the selectable property is false, only the itemClick event is dispatched, but if the selectable property is true, both the change and itemClick events are dispatched.

If you just want to disable the roll over highlighting, see “Disabling item roll over highlighting in the Flex DataGrid control”.

{ 7 comments… read them below or add one }

1 Kyle Hayes February 2, 2008 at 8:50 am

Interestingly enough, it still pops open an alert if selectable = false

Reply

2 peterd February 2, 2008 at 11:34 am

Kyle Hayes,

Yeah, but I guess technically it is correct since you are clicking an item, and it isn’t getting selected/highlighted.
I just added the itemClick business in there because I eventually expected somebody to ask how you could detect a click, and then I wouldn’t have to go figure it out and then reply in the comments like I actually know what I’m doing.

Regards,
Peter

Reply

3 Jamie Samland February 21, 2008 at 2:00 pm

How can I disable individual rows of a datagrid? I have a grid that’s displaying current and forecasted data. I don’t want the users to be able to do anything with forecast data.

Reply

4 Brad Grant July 11, 2008 at 7:02 am

I notice selectable=”false” also disables the rollover highlighting. We want to keep the rollover highlight for a visual aid but not allow the rows to be selected. I tried forcing useRollover to true but that did not work as selectable seems to take precedence. Is there a way to keep the highlighting for rollover with selectable set to false?

Reply

5 gyps October 9, 2009 at 4:56 am

Try to set selectedItem to null in onChange handler.

Reply

6 dave September 12, 2008 at 2:40 pm

I’ve been searching for a way to disable individual rows as well but can’t find an answer. Anyone have an idea?

Great site, btw. I monitor it daily for solutions. Thanks so much for making it.

Reply

7 peterd September 12, 2008 at 4:01 pm

dave,

Check out Alex Harui’s excellent entry on “Disabling List Selection”.

Peter

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree

Previous post:

Next post: