Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 8 & Angular Material: Drag Scrolling with CdkDropList item

I'm trying to provide scrolling while dragging a cdkDropList item. As of right now the page can't scroll without using the mousewheel to scroll. I was hoping to be able to just scroll through the page based on the dragging of the list item. After googling it looks like it wasn't possible until a few months ago?!

I found the following commit on the angular material repo: https://github.com/crisbeto/material2/commit/b4be85f6716a2d1a432ef7109aa06e7255324222

but haven't found any documentation on the angular material site. I was curious if anyone has implemented any auto drag scrolling on a CdkDropList element with Angular Material since this was released. I'm newer to angular. I've tried adding the cdkScrollable tag to the div but have been able to get the auto-scroll function to work while dragging any of the elements in the list.

Thoughts/advice?

like image 665
dxlachx Avatar asked Dec 12 '19 01:12

dxlachx


People also ask

What is an Angular 8?

Angular 8 is a client-side TypeScript based, front-end web framework by Google. Angular 8 is a great, reusable UI (User Interface) library for the developers which help in building attractive, steady, and utilitarian web pages and web application.

Why is Angular 8 used?

Angular 8 is an open-source, client-side TypeScript based JavaScript framework. It is written in TypeScript and complied into JavaScript. Angular 8 is used to create dynamic web applications. It is very similar to its previous versions except having some extensive features.

Is Angular 8 still supported?

angular.io/guide/releases#lts-fixes , based on the documentation , Angular version 8 is no longer supported.

Is Angular 8 same as AngularJS?

It is also important to note that Angular is different from AngularJS. It is an open-source project which can be freely used and changed by anyone. It extends HTML attributes with Directives, and data is bound with HTML.

What is the use of angular 8?

Angular 8 is a client-side TypeScript based, front-end web framework by Google. Angular 8 is a great, reusable UI (User Interface) library for the developers which help in building attractive, steady, and utilitarian web pages and web application.

What is the latest version of angular?

Angular community has released its latest version which is known as Angular 8. If you are familiar with previous version of Angular, it will not be difficult for you. You can easily upgrade your Angular CLI to version 8. What is Angular 8? Angular 8 is a client-side TypeScript based framework which is used to create dynamic web applications.

What is angular?

What is Angular? Angular is basically is an open-source, JavaScript-based client-side framework that helps us to develop a web-based application. Actually, Angular is one of the best frameworks for developing any Single Page Application or SPA Applications.

What is a single page application in Angular 8?

Angular 8 is the updated version of Angular 2. A single page application (SPA) is a web application that fits on a single page. All your code (JS, HTML, CSS) is recovered with a single page load. Also, navigate between pages performed without reviving the entire page.


2 Answers

From version 9.1.0, the auto scrolling is supported by setting cdkScrollable directive on the parent that should scroll.

So, for v9.1.0 and up, the following code should work:

<div style="height: 500px; overflow-y: auto" cdkScrollable>
  <div cdkDropList class="example-list" (cdkDropListDropped)="drop($event)">
    <div class="example-box" *ngFor="let movie of movies" cdkDrag>{{movie}}</div>
  </div>
</div>

Stackblitz demo:

https://stackblitz.com/edit/angular-swaqkk-njehwg (uses Angular CDK v10.0.1)

https://stackblitz.com/edit/angular-swaqkk-tssfag (uses Angular CDK v13.3.6)

Update 2022-05-11

  • From v11.1.0, configurable scroll speed is supported, by using cdkDropListAutoScrollStep property. Take a look at the answer posted by @Ben Racicot: Angular 8 & Angular Material: Drag Scrolling with CdkDropList item.

Also, I've posted a more complete answer, which includes more examples and also solutions for Angular8, at the following topic: Angular CDK - issue with scrolling and dragging element inside nested scrollable div

like image 139
andreivictor Avatar answered Nov 15 '22 06:11

andreivictor


Just combining my research into an answer. Dig deeper into the D&D API for what exists.

  <div #boardWrapper class="board-wrapper" cdkScrollable>
    <!-- cdkDropListGroup when multiple droplists -->    
    <div #boardColumns class="board-columns" cdkDropListGroup>
      <!-- A drop list -->
      <div
        cdkDropList
        [cdkDropListAutoScrollDisabled]="false" <-- enable
        [cdkDropListAutoScrollStep]="35"        <-- speed
        [cdkDropListData]="data.arr"
        (cdkDropListDropped)="drop($event)"
      >
        <!-- A drag item -->
        <div
          cdkDrag
          [cdkDragData]="item.data"
          (cdkDragMoved)="onDragMoved($event)"
          (cdkDragStarted)="dragStarted($event)"
        >content</div>
      </div>
    </div>
  </div>
like image 31
Ben Racicot Avatar answered Nov 15 '22 06:11

Ben Racicot