Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable CDK Drop due to condition

I'm trying to find a way how to disable dropping by using CDK due to some conditions.If the list which I wanna drop is empty should be disabled.I couldn't find a way to do that inside a method in .ts file.There are some HTML directives for that but not useful for me.

if(event.container.data.length==1){
  event.previousContainer.disabled=true;
}

I found this .disabled method for dragged containers but it only works when I Drag.I need an event to drop.

https://stackblitz.com/edit/angular-ui7u9m?file=src/app/cdk-drag-drop-disabled-sorting-example.ts

In this stackblitz example,there are two lists for example when Avilable lists is empty(no elements in array),dropping shouldnt be allowed anymore to this list.Could you help me to do that?

like image 558
Timuçin Çiçek Avatar asked Mar 16 '20 12:03

Timuçin Çiçek


People also ask

How do I turn off drag and drop on CDK?

Disable dragging 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 CDK?

The DragDropModule is imported from '@angular/cdk/drag-drop' and the module is added to import array as shown above. We have added class = ”divlayout” and the details of the class are in app. component.

How do I create a resource conditionally with CDK?

In summary, creating a resource conditionally with CDK requires us to do the following: downcast the resource we want to create conditionally to it’s level 0 construct equivalent (e.g. from s3.Bucket to s3.CfnBucket) attach the condition to the lower level construct using cfnResource.cfnOptions.condition = myCondition

How do I add a condition to an IAM policy in CDK?

Condition key names are case-insensitive, i.e. including the aws:TagKeys condition is equivalent to testing for AWS:TAGKEYS. In order to add condition (s) to an IAM policy in AWS CDK, we have to use the addCondition or addConditions methods on an instance of the PolicyStatement class.

How to use conditions in AWS CDK?

In order to use conditions in AWS CDK, we have to set the conditions prop when instantiating the PolicyStatement class. Let's look at an example, where we create an IAM role and attach policies with conditions to it. Copied! we created a policy statement with a condition.

How to disable copy of source list without CDK drag attributes?

You can just set above your source list that you want to disable copy of source list without CDK drag attributes after dragStarted event, it works fine, if you think that it bad decision write - why? trigger for appear of fake block you can use - dragStarted (event: CdkDragStart) { this.isDragging = true; } I don't need connected lists.


1 Answers

You can use the Angular Material enterPredicate from CdkDropList (Docs)

@Input('cdkDropListEnterPredicate')

enterPredicate: (drag: CdkDrag, drop: CdkDropList) => boolean

Function that is used to determine whether an item is allowed to be moved into a drop container.

E.g.:

html:

<div
      cdkDropList
      [cdkDropListData]="basket"
      class="example-list"
      (cdkDropListDropped)="drop($event)"
      [cdkDropListEnterPredicate]="canDrop">
      <div class="example-box" *ngFor="let item of basket" cdkDrag>{{item}}</div>
    </div>

component:

  canDrop() {
    return this.basket && this.basket.length > 0;
  }

Check attached Stackblitz.

Edit 1:

Additional you can use the attached list where your dropped too:

  canDrop(item: CdkDrag, list: CdkDropList) {    
    console.log(list.getSortedItems().length)
    return list && list.getSortedItems().length && list.getSortedItems().length > 0;
  }

Edit 2:

You can overwrite the function to pass in a parameter:

...
[cdkDropListEnterPredicate]="dropListEnterPredicate(basket)">
...
  dropListEnterPredicate(list: []){
    return function(drag: CdkDrag, drop: CdkDropList) {
        return list.length > 0;
    };
like image 66
zerocewl Avatar answered Nov 09 '22 19:11

zerocewl