drag-and-drop - CDK拖放式自动滚动
问题描述
我有一个 Angular 7 应用程序,使用 CDK Drag-n-Drop 在很长的列表中拖放行。
当拖动项目离开当前视图时,我应该怎么做才能让长列表自动滚动?
我可以参考任何示例代码吗?
解决方案
我遇到了同样的问题,只要外部元素可滚动,就会发生这种情况。这是未解决的问题 - https://github.com/angular/components/issues/16677。- 我稍微修改了此链接中提到的解决方案。
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;
}
});
}
}
模板
<div mat-dialog-content class="column-list">
<div class="column-selector__list">
<div cdkDropList (cdkDropListDropped)="drop($event)">
<div
*ngFor="let column of data"
cdkDrag
actualContainer="div.column-list"
>
</div>
</div>
</div>
</div>
推荐阅读
- javascript - 反应:未捕获(承诺)错误:请求失败,状态码为 400
- google-apps-script - 如何基于二级,甚至三级过滤器进行过滤?
- css - 反应在更新时更改 CSS 背景
- regex - 如何匹配行尾的空格作为前一句
- django - UUID 主键的格式错误的十六进制 UUID 错误字符串
- java - 如何处理 CDI 创建的 Runnable
- graphql - 使用 gatsby-source-prismic 无法返回 Prismic 切片
- c# - 使用 Kendo Grid 和 Odata 数据源对数字列进行过滤的最简单方法是什么?
- php - Laravel 不能通过外键连接两个表
- php - 如何更新我的查询以符合 sql_mode=only_full_group_by 启用?