views:

352

answers:

3

I want my data grid to behave by default as if the user is holding the control key down. So when an item is clicked, then another item they are both part of the selection, clicking them again removes them from the selection.

I already have allowMultipleSelection = true but I can't seem to find any setting that does this. I'm working on the itemclick event in the meantime, but it seems like there might be an easy to use setting I'm missing.

Any thoughts?

A: 

You could try adding event listeners to the grid for MouseEvents (UP and/or DOWN) with the highest priority, stopping propagation, and redispatching a new MouseEvent with the same properties on the original event.target but this time with ctrlKey=true.

I'm not sure if it'll cause 10,000 other things to break.

Alex Beardsley
A: 

I tried Nalandial's idea but had no luck...can't really intercept those events, but it got me going in the right direction. Worked a lot on this then found that the solution was a lot simpler than I was making it. I just needed to extend the dataGrid class and override two functions (mouseDownHandler and mouseClickHandler) adding the ctrlKey = true there then calling the rest of the function workes perfectly. In case you want to implement it, here's the code:

package com{
    import flash.events.MouseEvent;
    import mx.controls.DataGrid;

    public class ForceCtrlDataGrid extends DataGrid{
        public function ForceCtrlDataGrid(){
            super();
        }
        override protected function mouseClickHandler(event:MouseEvent):void{
            event.ctrlKey = true;
            super.mouseClickHandler(event);
        }
        override protected function mouseDownHandler(event:MouseEvent):void{
            event.ctrlKey = true;
            super.mouseDownHandler(event);
        }
    }
}
invertedSpear
+3  A: 

You could also extend DataGrid and override the selectItem method like so:

override protected function selectItem(item:IListItemRenderer, shiftKey:Boolean, ctrlKey:Boolean, transition:Boolean = true):Boolean
{
    return super.selectItem(item, shiftKey, true, transition )
}

Less code and less likely to have impact on other elements that might be listening for that MouseEvent.

cliff.meyers
Thanks Cliff! Much cleaner and works great.
invertedSpear