Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to disable dragging on a sub-element of cdkDrag?

I am using Angular CDK drag-drop from Angular Material (see documentation here). I was wondering if it is possible to disable dragging on a sub-element of cdkDrag. The problem is it is impossible to select what is written in an input of the draggable element using the mouse.

So, what I want to do is to disable dragging on all the input which are under the element which has the cdkDrag directive.

I have tried using:

  • cdkDragHandle: that would put the dragging on a specific element, not what I want to do here
  • cdkDragDisabled: that would disable dragging the whole element, not what I want to do here

Here is what my code looks like:

<div cdkDropList (cdkDropListDropped)="drop($event)">
    <div *ngFor="let element of array" cdkDrag>
        <div>
            <mat-form-field>
                <mat-label>Input 1</mat-label>
                <input matInput type="text">
            </mat-form-field>
            <mat-form-field>
                <mat-label>Input 2</mat-label>
                <input matInput type="number">
            </mat-form-field>
        </div>
    </div>
</div>

Thanks in advance for your help and time.

like image 576
Gaetitan Avatar asked May 29 '20 09:05

Gaetitan


People also ask

How do I turn off CDK drag?

If you want to disable dragging for a particular drag item, you can do so by setting the cdkDragDisabled input on a cdkDrag item. Furthermore, you can disable an entire list using the cdkDropListDisabled input on a cdkDropList or a particular handle via cdkDragHandleDisabled on cdkDragHandle .

How do I stop dragging Javascript?

to disable dragging. img. setAttribute("draggable", false);

What is cdkDropList?

CdkDropList. Container that wraps a set of draggable items. Selector: [cdkDropList] cdk-drop-list.


1 Answers

You can stop the mousedown event propagation on your form fields. Add the following to the form field element: (mousedown)="$event.stopPropagation()".

This stops the drag event from happening when you try to click into a form field and lets you interact normally with that form field.

<div cdkDropList (cdkDropListDropped)="drop($event)">
    <div *ngFor="let element of array" cdkDrag>
        <div>
            <mat-form-field>
                <mat-label>Input 1</mat-label>
                <input matInput type="text"(mousedown)="$event.stopPropagation()">
            </mat-form-field>
            <mat-form-field>
                <mat-label>Input 2</mat-label>
                <input matInput type="number"(mousedown)="$event.stopPropagation()">
            </mat-form-field>
        </div>
    </div>
</div>
like image 94
Bryan Sullivan Avatar answered Sep 21 '22 14:09

Bryan Sullivan