Skip to content

CdkDrop should be a Directive rather than a Component #13341

Closed
@josephperrott

Description

@josephperrott

By using a directive rather than a component, the drop area can be easily added on to templating which is controlled by another component.

An example of would be in the MatTable/CdkTable:

Currently you must do the following for each column:

<ng-container cdkColumnDef="position">
  <th cdk-header-cell *cdkHeaderCellDef>
    <cdk-drop [connectedTo]="drops">
      <span cdkDrag>No.</span>
    </cdk-drop>
  </th>
  <td cdk-cell *cdkCellDef="let element"> {{element.position}} </td>
</ng-container>

coupled with something in your components class to determine all of the drop zones.

@ViewChildren(CdkDrop) dropChildren: QueryList<CdkDrop>;
drops: CdkDrop[] = [];
ngAfterInit() {
  this.dropChildren.changes.subscribe(() => {
    this.drops = this.dropsChildren.toArray();
  });
  // Simple hack for quick example
  Promise.resolve().then(() => {
    this.drops = this.dropsChildren.toArray();
  });
}

By moving to using directives we can have the following:

<ng-container cdkColumnDef="position">
  <th cdk-header-cell *cdkHeaderCellDef cdkDrag>No.</th>
  <td cdk-cell *cdkCellDef="let element"> {{element.position}} </td>
</ng-container>

<tr cdk-header-row *cdkHeaderRowDef="displayedColumns" cdkDrop></tr>

Metadata

Metadata

Assignees

Labels

P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgent

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions