【问题标题】:Angular CdkDragDrop inside a table cell, and drag across rowsAngular CdkDragDrop 在表格单元格内,并跨行拖动
【发布时间】:2019-05-25 01:26:30
【问题描述】:

我正在尝试在 Angular 7 中添加一个表格,其中一列包含一个列表,我希望允许用户将这些列表项中的任何一个拖放到不同的行中。

我一直在尝试使用 CdkDragDrop,并将其放在一个表格中,但它似乎不起作用。

请在下面找到代码:

    <table
    mat-table
    [dataSource]="general"
    style="width:100%; margin-top: 10px; margin-bottom: 50px"
    *ngIf="general.data.length !== 0">
    <ng-container matColumnDef="Camera_Physical_Index">
      <th mat-header-cell *matHeaderCellDef>Camera Physical Index</th>
      <td mat-cell *matCellDef="let element">
        {{ element.Camera_Physical_Index }}
      </td>
    </ng-container>

    <ng-container matColumnDef="Analytical_Function">
      <th mat-header-cell *matHeaderCellDef>Analytical Function</th>
      <td mat-cell *matCellDef="let element">
        <div
          cdkDropList
          [cdkDropListData]="general.data"
          (cdkDropListDropped)="dropGroup($event)"
        >
          <div
            cdkDropListGroup
            class="example-container"
            cdkDrag
            [cdkDragData]="element"
          >
            <div
              cdkDropList
              class="example-list"
              id="{{ element.Camera_Physical_Index }}"
              [cdkDropListData]="element.Analytical_Function"
              (cdkDropListDropped)="dropItem($event)"
              [cdkDropListConnectedTo]="getConnectedList()"
            >
              <div
                class="example-box"
                *ngFor="let item of element.Analytical_Function"
                cdkDrag
                [cdkDragData]="item"
              >
                {{ item }}
              </div>
            </div>
          </div>
        </div>
      </td>
    </ng-container>

    <tr mat-header-row *matHeaderRowDef="generalColumns"></tr>
    <tr mat-row *matRowDef="let row; columns: generalColumns"></tr>
  </table>

期望的结果如下:

Desired UI

【问题讨论】:

    标签: html angular typescript angular7


    【解决方案1】:

    要使列表连接起来,您应该将 cdkDropListGroup 放在该列的 ng-container 中,这样它将包含该列的所有列表(每行的列表)。

    请在下面的 stackblitz 中找到我试图作为您的示例的地方:

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

    一般来说: 如果您有多个列表并且想要从一个拖动到另一个:

    1. 您应该通过全局中的指令 cdkDropListGroup 连接所有这些列表,以便自动分组和连接它们。
    2. 在每个可拖动项周围添加 cdkDropList
    3. cdkDrag 添加到项目中,使其可拖动
    4. 添加 cdkDropListDropped 事件来监听它,以便在用户完成拖动后更新数据模型。

    angular material

    【讨论】:

      猜你喜欢
      • 2013-07-17
      • 1970-01-01
      • 2020-08-13
      • 2017-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-16
      相关资源
      最近更新 更多