angulardragulang2-dragulaangular-dragula

Ng2-Dragula: Enable drag at run time


So, I have this following template.

<div dragula="'card-images'" [dragulaModel]="images">
  <child-component [card]="imageCard" [ngClass]="cssClass(card)" *ngFor="let imageCard of images"></child-component>
</div>

The function cssClass sets the class based on the type of imageCard. So it is set to draggable and non draggable based on the type of image Card. Now, I want the child component to be draggable only if its property isSelected is true only after long press and this is done dynamically. How can I achieve this ? How to make child component draggable at run time ?

Thank you.


Solution

  • You can provide moves function and put the condition when the items should be dragable.

    In your case, you can do it two steps -

    1. Add the class to element on the property based.

    <child-component [ngClass]="{'no-drag' : card.selected != true}"></child-component>
    

    2. drag element if doesn't have the class no-drag.

      constructor(private dragulaService: DragulaService) {
          dragulaService.setOptions('card-images', {
            moves: (el, source, handle, sibling) => !el.classList.contains('no-drag')
          });
        }