Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Angular Material CDK Drag Drop, How to Prevent Items from Automatically Rearranging as an Element Moves?

Thanks in advance. Here's the issue:

I'm using Angular Material CDK Drag Drop (version: @angular/ckd: 7.3.7)

The documentation says "Items will automatically rearrange as an element moves."

My question is: How do I prevent items from automatically rearranging as an element moves?

Here is an animated gif of what I don't want. This is a chess board I made and you can see that the "items (chess pieces) are automatically being rearranged as the element (chess piece) moves"

Here is an animated gif of what I want. What I want is for the items (chess pieces) to not be rearranged as the element (chess piece) moves.

Here is a stackblitz with the code

like image 426
user5369 Avatar asked Jun 07 '19 18:06

user5369


People also ask

How do I turn off drag and drop on CDK?

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 you drag and drop in angular 6?

To implement drag and drop list you have to include your ngFor repeated section inside a parent div to make that section draggable. This is just a basic structure of how the draggable section should look. Now we have to provide DndList directives to this structure. Basic functioning HTML for drag and drop list.

How do you install drag and drop CDK?

Install Angular CDK Package for Drag and Drop ts file. import { DragDropModule } from '@angular/cdk/drag-drop'; @NgModule({ declarations: [...], imports: [ DragDropModule ], providers: [...], bootstrap: [...], schemas: [...] })


2 Answers

Since Angular 8, it's possible to disable this behaviour by adding cdkDropListSortingDisabled

like image 103
Abdo Driowya Avatar answered Oct 18 '22 00:10

Abdo Driowya


just create an empy cdkDragPlaceholder, well, you need enclosed the img in a div

<div class="square"
     [ngClass]="{'white': square.row % 2 === square.col %2,
  'black': square.row % 2 !== square.col % 2}"
  cdkDropList
   ...>
  <div cdkDrag cdkDragBoundary=".board">
    <img *ngIf="square.piece"
       class="piece"
       src="{{square.piece.img()}}"
       />
       <!---this is a empty dragPlaceHolder-->
       <div *cdkDragPlaceholder></div>
   </div>
</div>
like image 23
Eliseo Avatar answered Oct 17 '22 23:10

Eliseo