Search code examples
ag-gridag-grid-angular

Ag-grid: How disable cell click event when enableCellTextSelection is used and text is selected?


Using ag-Grid Community and Angular, I set [enableCellTextSelection]="true" to enable cell text selection to facilitate copy-paste, but I found that the cellClicked event still fires when text is selected and the mouse button is released (i.e. the mouse is clicked, dragged and released in the same cell).

Is there a way to detect that text is selected in the cellClicked event and "cancel / quick exit" it?

I looked for a way to distinguish between the mouse down and mouse up coordinates but couldn't find anything...

Thanks.


Version in use

  • ag-grid-angular: 23.2.1
  • ag-grid-community: 23.2.1

Solution

  • Here's a little workaround you can use. Use window.getSelection() to get the text selected by the user. From this, you can determine if text has been selected or not.

    Change your cellClicked callback function to this:

    cellClicked(event)
        {
          if (window.getSelection().type !== 'Range')
          {
            //text has not been selected, the cell has been clicked
            console.log('cellClicked');
          }
        }
    

    Demo