Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular cdk drag drop - is there any way to async validate on drop and revert if validation returns false?

When droping an item I want to be able to perform an async call (e.g. check with backend for conflicts, collisions and so forth...) which returns true or false. If false, I want to "reverse" the drop so that the item appears in the original array.

I coulnd't find any references in the offical docs, so any pointers are appreciated on how to tackle this challenge.

like image 215
Han Che Avatar asked Oct 15 '25 14:10

Han Che


1 Answers

For anyone else looking for this, here's one way to do it. It would be great if the move could somehow be told to wait for the result before reverting, but I couldn't figure out how to do it.

try{
//Move the value first
transferArrayItem(event.previousContainer.data,
  event.container.data,
  event.previousIndex,
  event.currentIndex);

//Wait for the result and then roll back the change if required
this.openEdit(event.previousContainer.data[event.previousIndex]).subscribe((result:Updated) =>
  {

    if(result?.success !== 1){
      transferArrayItem(event.container.data,
        event.previousContainer.data,
        event.currentIndex,
        event.previousIndex);
    }

  });

}
like image 99
Jim Jimson Avatar answered Oct 18 '25 06:10

Jim Jimson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!