Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular cdk drap drop - dynamic placeholder height

I want to set the placeholder height to height of dragging element.

For now I'm using static height placeholder of smallest possible element height. I couldn't find any informations about how to do it and for now having no idea.

component html

<div class="taskList" cdkDropList id="{{ 'cdk-task-drop-list-' + categoryId }}" [cdkDropListData]="taskList"
[cdkDropListConnectedTo]="categoryIdList" (cdkDropListDropped)="onDrop($event)">
    <ng-container *ngIf="task.isApproved || task.authorId===userId || userAccessLevel >= 3">
        <div class="placeholder" *cdkDragPlaceholder></div>
        <div class="task">
            ...
        </div>
    </ng-container>
</div>

css

.placeholder{
    position: relative;
    margin-top: 1px; 
    margin-bottom: 5px;
    min-height: 75px;
    transition: transform 250ms cubic-bezier(0, 0, 0.2, 1);
    vertical-align: middle;
}

Any ideas?

like image 667
Smokovsky Avatar asked Mar 03 '20 12:03

Smokovsky


1 Answers

Try to get height of dragging element, and based on this, change placeholder height.

cdkDragStarted(event:any){
   this.height = event.source.element.nativeElement.offsetHeight
}

HTML:

<div  class="example-box"  *ngFor="let movie of movies; let i = index"  (cdkDragStarted)="cdkDragStarted($event)" cdkDrag >
    <div [ngStyle]="{'min-height.px':height  }" class="example-custom-placeholder" *cdkDragPlaceholder></div>
    {{movie}}
</div>

Here is my example: https://stackblitz.com/edit/angular-zhdujp-kppghs?file=src/app/cdk-drag-drop-custom-placeholder-example.ts

like image 169
BartoszTermena Avatar answered Sep 20 '22 16:09

BartoszTermena