Search code examples
actionscript-3apache-flexdatagridflex4.5flex-spark

Flex 4.5 Spark DataGrid - Detect column clicked in selectionChange handler


I have a spark datagrid with selectionMode="multipleRows".

I have three columns in the datagrid.

I don't want the row selection to happen when the user's click falls on the third column of a row.

The row selection should happen only when one of the first two columns is clicked.

How do I achieve this? There is a selectionChanging event for the datagrid, but the GridSelectionEvent object received in the handler does not seem to provide any information about the column on which the click happened.

Thanks!


Solution

  • I figured this out myself. I am not sure if this is a bug in the spark DataGrid. The following is definitely a hack and not clean.

    In the grid_mouseDownHandler function in the DataGrid.as file, there is a line:

    const columnIndex:int = isCellSelection ? event.columnIndex : -1;
    

    This line is causing the columnIndex to be set as -1 if the selectionMode of the DataGrid is anything other than GridSelectionMode.SINGLE_CELL or GridSelectionMode.MULTIPLE_CELLS. As I mentioned in the original question, I need my datagrid to have a selectionMode of GridSelectionMode.MULTIPLE_ROWS.

    I sub-classed the DataGrid and re-implemented the grid_mouseDownHandler (basically copy-pasted the whole function). I changed only the above line to always assign the columnIndex to event.columnIndex.

    (I also had to copy some more functions which were referenced by the grid_mouseDownHandler over to my sub-class because those functions were protected or mx_internal. (toggleSelection, extendSelection, isAnchorSet)

    Then, in the selectionChanging event handler, I could just do the following:

    if( 2 == event.selectionChange.columnIndex )
    {
        event.preventDefault();
    }
    

    I realize that this is not a clean solution, but it is the best I could think of. Maybe someone can suggest a better solution?