如果没有数据模型,则 Angular Material 在列表之间拖放

标签 angular drag-and-drop angular-material angulardraganddroplists

更新:

我创建了一个 stackblitz重现问题。


考虑我拥有的以下静态模板:

<div class="websiteleftColumn" cdkDropList #left="cdkDropList"
  [cdkDropListConnectedTo]="[right]">
    <div class="panelWithMuchContent" cdkDrag> much HTML </div>
    <div class="panelWithMuchContent" cdkDrag> much HTML </div>
    <div class="panelWithMuchContent" cdkDrag> much HTML </div>
</div>

<div class="websiteRightColumn" cdkDropList #right="cdkDropList"
  [cdkDropListConnectedTo]="[left]">
    <div class="panelWithMuchContent" cdkDrag> much HTML </div>
    <div class="panelWithMuchContent" cdkDrag> much HTML </div>
    <div class="panelWithMuchContent" cdkDrag> much HTML </div>
</div>

我希望能够自由地对两列中的面板以及两列之间的面板进行重新排序。相反,我可以拖动面板,但一旦我放下一个面板,什么也不会发生,它就会恢复到原始状态。

猜测是因为背后没有数据模型。在 example s,可拖动的div通过 *ngFor 渲染在页面上来自数组。还有一个drop(event: CdkDragDrop<string[]>)绑定(bind)到它们的组件方法,每次发生放置时都会更新数据模型。

但我的问题是,我不仅有可以放入某个数组的简单列表元素,还有网站的整个部分,其中包含我想要拖动的大量 HTML 代码。

如何为其创建数据模型? (如果真的是 Angular 缺失的话)

最佳答案

查看此answer因为它不起作用的原因,这里还有一个 example如何使用 ng-template 和数据模型正确实现拖放:

import { Component, Input, ViewChild, TemplateRef } from '@angular/core';
import { CdkDragDrop, moveItemInArray, transferArrayItem } from '@angular/cdk/drag-drop';

@Component({
  selector: 'hello',
  template: `

    <div class="row" cdkDropListGroup>

      <div class="col-6" cdkDropList [cdkDropListData]="list1"
        (cdkDropListDropped)="panelDropped($event)">

        <div cdkDrag *ngFor="let p of list1">
          <ng-container *ngTemplateOutlet="this[p+'Panel']">
          </ng-container>
        </div>

      </div>

      <div class="col-6" cdkDropList [cdkDropListData]="list2"
        (cdkDropListDropped)="panelDropped($event)">

        <div cdkDrag *ngFor="let p of list2">
          <ng-container *ngTemplateOutlet="this[p+'Panel']">
          </ng-container>
        </div>

      </div>

    </div>

    <ng-template #aPanel><div class="card"></div></ng-template>
    <ng-template #bPanel><div class="card"></div></ng-template>
    <ng-template #cPanel><div class="card"></div></ng-template>
    <ng-template #dPanel><div class="card"></div></ng-template>
    <ng-template #ePanel><div class="card"></div></ng-template>
    <ng-template #fPanel><div class="card"></div></ng-template>

  `,
  styles: [`

    .col-6:nth-child(1) { background-color: plum; }
    .col-6:nth-child(2) { background-color: peru; }
    .card { background-color: blue; height: 10em; margin: 0.5em; }

  `]
})
export class HelloComponent {
  @Input() name: string;

  @ViewChild('aPanel') aPanel: TemplateRef<any>;
  @ViewChild('bPanel') bPanel: TemplateRef<any>;
  @ViewChild('cPanel') cPanel: TemplateRef<any>;
  @ViewChild('dPanel') dPanel: TemplateRef<any>;
  @ViewChild('ePanel') ePanel: TemplateRef<any>;
  @ViewChild('fPanel') fPanel: TemplateRef<any>;

  list1: Array<string> = ['a', 'b', 'c'];
  list2: Array<string> = ['d', 'e', 'f'];

  panelDropped(event: CdkDragDrop<string[]>) {
    if (event.previousContainer === event.container) {
      moveItemInArray(event.container.data,
        event.previousIndex, event.currentIndex);
    } else {
      transferArrayItem(event.previousContainer.data,
        event.container.data, event.previousIndex, event.currentIndex);
    }
  }

}

关于如果没有数据模型,则 Angular Material 在列表之间拖放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54130044/

相关文章:

Angular Material 多对象拖放与调整大小问题

Meteor:Angular2 VS Blaze 困惑

javascript - 可观察对象是否发出 get 请求?

javascript - Firefox 中的 HTML 5 拖放无法按预期工作

使用 trackby 的 Angular Drag and Drop 正在迅速恢复

javascript - Dropzone js - maxFiles 不工作

javascript - CSS - 水平滚动上的粘性div?

Angular 4 的 Angular Material

angular - 在 ng2-dragula 中询问删除确认

Angular ControlValueAccessor 和 markAsTouched