gpt4 book ai didi

Angular Nested Drag and Drop/CDK Material cdkDropListGroup cdkDropList 嵌套

转载 作者:行者123 更新时间:2023-12-04 14:07:27 25 4
gpt4 key购买 nike

我使用 CDK Material 拖放实用程序来创建启用拖放功能的表单编辑器。
它工作正常,但嵌套了 cdkDropListcdkDropListGroup不起作用。
我无法将任何内容拖入嵌套的下拉列表容器。

<div class="container">
<div class="row" cdkDropListGroup>
<div class="col-2">
<div id="toolbox" cdkDropList>
...
</div>
</div>
<div class="col-10">
<div id="formContainer" cdkDropList>
...
<div class="row">
<div class="col-md-6" cdkDropList>
... column 1 content
</div>
<div class="col-md-6" cdkDropList>
... column 1 content
</div>
</div>
</div>
</div>
</div>
</div>
Drag and drop not working

最佳答案

我花了一些时间,但由于这些帖子的提示,我终于找到了解决方案:

  • https://github.com/angular/components/issues/14093#issuecomment-462395200
  • https://stackoverflow.com/a/59758611/4549486

  • 问题在于 cdkDropListGroup不支持嵌套下拉列表。您需要将下拉列表与 [cdkDropListConnectedTo] 连接起来。捆绑。
    但是,如果您只将列表连接到 [cdkDropListConnectedTo] 的数组绑定(bind)列表顺序会影响放置行为。此外,嵌套下拉列表中的排序将不起作用。
    为了避免这些问题,您需要创建一个寻找正确 cdkDropList 的服务。拖动时。
    export class DragDropService {
    dropLists: CdkDropList[] = [];
    currentHoverDropListId?: string;

    constructor(@Inject(DOCUMENT) private document: Document) {}

    public register(dropList: CdkDropList) {
    this.dropLists.push(dropList);
    }

    dragMoved(event: CdkDragMove<IFormControl>) {
    let elementFromPoint = this.document.elementFromPoint(
    event.pointerPosition.x,
    event.pointerPosition.y
    );

    if (!elementFromPoint) {
    this.currentHoverDropListId = undefined;
    return;
    }

    let dropList = elementFromPoint.classList.contains('cdk-drop-list')
    ? elementFromPoint
    : elementFromPoint.closest('.cdk-drop-list');

    if (!dropList) {
    this.currentHoverDropListId = undefined;
    return;
    }

    this.currentHoverDropListId = dropList.id;
    }

    dragReleased(event: CdkDragRelease) {
    this.currentHoverDropListId = undefined;
    }
    }
  • register添加一个新的下拉列表到 dropList每个 cdkDropList 使用的数组.
  • dragMoved确定正确 cdkDropList在鼠标指针下方。

  • 最好的办法是创建一个自己的组件,其中包含 cdkDropList .
    以下组件仅用于简单和演示目的。您不应直接使用服务属性。
    <div
    *ngIf="container"
    cdkDropList
    [cdkDropListData]="container.controls"
    [cdkDropListConnectedTo]="dragDropService.dropLists"
    [cdkDropListEnterPredicate]="allowDropPredicate"
    (cdkDropListDropped)="dropped($event)"
    >
    <div
    *ngFor="let item of container.controls"
    cdkDrag
    [cdkDragData]="item"
    (cdkDragMoved)="dragMoved($event)"
    (cdkDragReleased)="dragReleased($event)"
    >
    Drag Content
    </div>
    </div>
    export class FormContainerComponent implements OnInit, AfterViewInit {
    @ViewChild(CdkDropList) dropList?: CdkDropList;
    @Input() container: IFormContainer | undefined;

    allowDropPredicate = (drag: CdkDrag, drop: CdkDropList) => {
    return this.isDropAllowed(drag, drop);
    };

    constructor(
    public dragDropService: DragDropService
    ) {}
    ngOnInit(): void {}

    ngAfterViewInit(): void {
    if (this.dropList) {
    this.dragDropService.register(this.dropList);
    }
    }
    dropped(event: CdkDragDrop<IFormControl[]>) {
    // Your drop logic
    }

    isDropAllowed(drag: CdkDrag, drop: CdkDropList) {
    if (this.dragDropService.currentHoverDropListId == null) {
    return true;
    }

    return drop.id === this.dragDropService.currentHoverDropListId;
    }

    dragMoved(event: CdkDragMove<IFormControl>) {
    this.dragDropService.dragMoved(event);
    }

    dragReleased(event: CdkDragRelease) {
    this.dragDropService.dragReleased(event);
    }
    }

  • 每当 cdkDrag被移动,dragMoved确定正确 cdkDropList
  • 每当 cdkDrag被释放,复位确定cdkDropList
  • 最重要的方法是isDropAllowed设置为 [cdkDropListEnterPredicate]="allowDropPredicate" 的方法到 cdkDropList
  • 如前所述,cdk Material 无法确定正确的下拉列表。
  • 如果cdk选择了错误的下拉列表,我们只是不允许 通过返回 false 下降。在这种情况下,cdk 会自动选择下一个可能的 cdkDropList这是正确的:)


  • Drag and Drop nested working

    关于Angular Nested Drag and Drop/CDK Material cdkDropListGroup cdkDropList 嵌套,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67337934/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com