forked from angular/components
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(cdk\drag-drop): Add table drag drop example
Adds an example that shows how to drag and drop table rows Fixes angular#25168
- Loading branch information
Showing
7 changed files
with
130 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/components-examples/cdk/drag-drop/cdk-drag-drop-table/cdk-drag-drop-table-example.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
table { | ||
width: 100%; | ||
} | ||
|
||
.dragCursor { | ||
margin-right: 16px; | ||
cursor: move; | ||
} | ||
|
||
.cdk-drag-preview { | ||
box-sizing: border-box; | ||
border-radius: 4px; | ||
box-shadow: 0 5px 5px -3px rgba(0, 0, 0, 0.2), | ||
0 8px 10px 1px rgba(0, 0, 0, 0.14), | ||
0 3px 14px 2px rgba(0, 0, 0, 0.12); | ||
} | ||
|
||
.cdk-drag-placeholder { | ||
background: #e0e0e0; | ||
} | ||
|
||
.cdk-drag-animating { | ||
transition: transform 250ms cubic-bezier(0, 0, 0.2, 1); | ||
} | ||
|
||
.cdk-drop-list-dragging .mat-row:not(.cdk-drag-placeholder) { | ||
transition: transform 250ms cubic-bezier(0, 0, 0.2, 1); | ||
} |
38 changes: 38 additions & 0 deletions
38
src/components-examples/cdk/drag-drop/cdk-drag-drop-table/cdk-drag-drop-table-example.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<mat-table #table [dataSource]="dataSource" class="mat-elevation-z8" cdkDropList (cdkDropListDropped)="drop($event)" | ||
cdkDropListData="dataSource" [cdkDropListDisabled]="dragDisabled"> | ||
<!-- Position Column --> | ||
<ng-container matColumnDef="position" sticky> | ||
<mat-header-cell *matHeaderCellDef> No. </mat-header-cell> | ||
<mat-cell *matCellDef="let element"> | ||
<mat-icon class="dragCursor" (mousedown)="dragDisabled = false;">reorder</mat-icon> | ||
<span>{{element.position}}</span> | ||
</mat-cell> | ||
</ng-container> | ||
|
||
<!-- Name Column --> | ||
<ng-container matColumnDef="name"> | ||
<mat-header-cell *matHeaderCellDef> Name </mat-header-cell> | ||
<mat-cell *matCellDef="let element"> {{element.name}} </mat-cell> | ||
</ng-container> | ||
|
||
<!-- Weight Column --> | ||
<ng-container matColumnDef="weight"> | ||
<mat-header-cell *matHeaderCellDef> Weight </mat-header-cell> | ||
<mat-cell *matCellDef="let element"> {{element.weight}} </mat-cell> | ||
</ng-container> | ||
|
||
<!-- Symbol Column --> | ||
<ng-container matColumnDef="symbol"> | ||
<mat-header-cell *matHeaderCellDef> Symbol </mat-header-cell> | ||
<mat-cell *matCellDef="let element"> {{element.symbol}} </mat-cell> | ||
</ng-container> | ||
|
||
<!-- Quantity Column --> | ||
<ng-container matColumnDef="quantity"> | ||
<mat-header-cell *matHeaderCellDef> Quantity of Element </mat-header-cell> | ||
<mat-cell *matCellDef="let element">{{element.quantity}}</mat-cell> | ||
</ng-container> | ||
|
||
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row> | ||
<mat-row *matRowDef="let row; columns: displayedColumns;" cdkDrag [cdkDragData]="row"></mat-row> | ||
</mat-table> |
53 changes: 53 additions & 0 deletions
53
src/components-examples/cdk/drag-drop/cdk-drag-drop-table/cdk-drag-drop-table-example.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import {Component, ViewChild} from '@angular/core'; | ||
import {CdkDragDrop, CdkDropList, CdkDrag, moveItemInArray} from '@angular/cdk/drag-drop'; | ||
import {MatTable, MatTableModule} from '@angular/material/table'; | ||
import {MatIconModule} from '@angular/material/icon'; | ||
|
||
export interface PeriodicElement { | ||
name: string; | ||
position: number; | ||
weight: number; | ||
symbol: string; | ||
quantity: number; | ||
} | ||
|
||
export const ELEMENT_DATA: PeriodicElement[] = [ | ||
{position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H', quantity: 100}, | ||
{position: 2, name: 'Helium', weight: 4.0026, symbol: 'He', quantity: 100}, | ||
{position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li', quantity: 100}, | ||
{position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be', quantity: 100}, | ||
{position: 5, name: 'Boron', weight: 10.811, symbol: 'B', quantity: 100}, | ||
{position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C', quantity: 100}, | ||
{position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N', quantity: 100}, | ||
{position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O', quantity: 100}, | ||
{position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F', quantity: 100}, | ||
{position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne', quantity: 100}, | ||
]; | ||
|
||
/** | ||
* @title Drag&Drop table | ||
*/ | ||
@Component({ | ||
selector: 'cdk-drag-drop-table-example', | ||
templateUrl: 'cdk-drag-drop-table-example.html', | ||
styleUrl: 'cdk-drag-drop-table-example.css', | ||
standalone: true, | ||
imports: [CdkDropList, CdkDrag, MatTableModule, MatIconModule], | ||
}) | ||
export class CdkDragDropTableExample { | ||
@ViewChild('table', {static: true}) table: MatTable<PeriodicElement>; | ||
|
||
displayedColumns: string[] = ['position', 'name', 'weight', 'symbol', 'quantity']; | ||
dataSource = ELEMENT_DATA; | ||
dragDisabled = true; | ||
|
||
drop(event: CdkDragDrop<string>) { | ||
// Return the drag container to disabled. | ||
this.dragDisabled = true; | ||
|
||
const previousIndex = this.dataSource.findIndex(d => d === event.item.data); | ||
|
||
moveItemInArray(this.dataSource, previousIndex, event.currentIndex); | ||
this.table.renderRows(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters