Search code examples
angulartypescriptangular-materialangular-cdk

How to make a mat-table row drag-drop work with cdkDragHandle so that the row is only draggable using the handle?


I found this stackblitz example of adding drag-drop to a mat-table using angular cdk. However, the desired behavior is that the row is only draggable using the element with the cdkDragHandle directive. In this example you can drag the element by clicking anywhere on the row. How can this be modified so that the row is only draggable using the drag handle?

https://stackblitz.com/edit/angular-igmugp


Solution

  • I have found a somewhat simple issue to this complex problem. For any simple text td in the draggable tr, we can use the pointer-events:none and it will disable all the text element.

    On the handle icon, use the pointer-events:all and it will enable dragging from only the icon.

    This also has the issue where it disables all the anchor and buttons. So for icon and buttons do the follwoing

    1. using mouseDown set a flag
    2. on drag start, check the drag and throw mouseup event
    3. on drag stop, check if flag is set and reset flag and return

    check this stackblits for working answer https://stackblitz.com/edit/angular-rwzc76