question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DragDrop Autoscroll feature not working with Material Table

See original GitHub issue

Relates to https://github.com/angular/components/pull/16382

Reproduction

https://stackblitz.com/edit/angular-qczaca

Steps to reproduce:

  1. Try dragging a row to the edge of its container.
  2. You can see that Material Table cannot be autoscrolled while a simple container can be… (like the div in the Stackblitz example).

Expected Behavior

I can use autoscroll feature with Material Table as well.

Actual Behavior

Autoscroll does not work with Material Table.

Environment

Angular, CDK/Material:

    "@angular/animations": "~8.2.0",
    "@angular/cdk": "~8.1.2",
    "@angular/common": "~8.2.0",
    "@angular/compiler": "~8.2.0",
    "@angular/core": "~8.2.0",
    "@angular/forms": "~8.2.0",
    "@angular/material": "^8.1.2",
    "@angular/platform-browser": "~8.2.0",
    "@angular/platform-browser-dynamic": "~8.2.0",
    "@angular/router": "~8.2.0",
    "hammerjs": "^2.0.8"
    "@angular-devkit/build-angular": "~0.802.0",
    "@angular/cli": "~8.2.0",
    "@angular/compiler-cli": "~8.2.0",
    "@angular/language-service": "~8.2.0"

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:5
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

14reactions
liesaheadcommented, Jan 19, 2021

@jraadt, The solution is not well documented, but it works like a charm. Add cdkScrollable to that div with outside classname. Here is the link, tell me if it helped: https://stackblitz.com/edit/angular-swaqkk-jdsxoo?file=src%2Fapp%2Fcdk-drag-drop-sorting-example.html

12reactions
kyle-apexcommented, Dec 6, 2019

I can see that there is an issue in above example. first time you want do drag row in the table beyond the container view (with the scroll), row is not moving correctly. It looks like it’s moved only between rows of initial view of the table ( rows that are visible when row is dragged first time). Also param of drop event returns wrong indexes. Second drag and drops works correctly. any Ideas how to solve this ?

In the current version, beforeStarted on the cdkDropList declares the element before calling beforeStarted; however, beforeStarted on the cdkDrag calls on time to avoid the issue you mentioned. Adjusting @shlomiassaf’s solution, the following worked for me:

Add actualContainer=“div.example-container” to the element with cdkDrag instead of cdkDropList and adjust the directive as following:

import { Directive, Input, ElementRef, AfterViewInit } from '@angular/core';
import { CdkDrag } from '@angular/cdk/drag-drop';

@Directive({
  selector: '[cdkDrag][actualContainer]',
})
export class CdkDropListActualContainerDirective {
  @Input('actualContainer') actualContainer: string;
  originalElement: ElementRef<HTMLElement>;

  constructor(cdkDrag: CdkDrag) {
    cdkDrag._dragRef.beforeStarted.subscribe( () => {
      var cdkDropList = cdkDrag.dropContainer;
      if (!this.originalElement) {
        this.originalElement = cdkDropList.element;
      }

      if ( this.actualContainer ) {
        const element = this.originalElement.nativeElement.closest(this.actualContainer) as HTMLElement;
        cdkDropList._dropListRef.element = element;
        cdkDropList.element = new ElementRef<HTMLElement>(element);
      } else {
        cdkDropList._dropListRef.element = cdkDropList.element.nativeElement;
        cdkDropList.element = this.originalElement;
      }
    });
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

CDK drag-n-drop auto scroll - Stack Overflow
I have faced the same issue, It happens anytime an outside element is scrollable.
Read more >
Drag and Drop | Angular Material
Returns the element that is being used as a placeholder while the current element is being dragged. Returns the root draggable element.
Read more >
drag drop with scroll
Virtual Scrolling loads and unloads elements from the DOM based on the visible parts of a list, where Drag and Drop feature supports...
Read more >
Cdk Drop List Scroll - StackBlitz
material -module';. import {CdkDragDropSortingExample} from './. app/cdk-drag-drop-sorting-example';. @NgModule({. imports: [. BrowserModule,.
Read more >
How to use drag and drop and scrolling feature in angular 7 ...
Using Angular 7 CDK you can easily implement drag and drop and scrolling feature of angular . ... import { DragDropModule } from...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found