New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs(cdk\drag-drop): Add table drag drop example #28969
Merged
crisbeto
merged 1 commit into
angular:main
from
GiftLanga:docs/add-drag-drop-table-example
May 1, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
29 changes: 29 additions & 0 deletions
29
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,29 @@ | ||
table { | ||
width: 100%; | ||
} | ||
|
||
.example-drag-cursor { | ||
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); | ||
background-color: white; | ||
} | ||
|
||
.cdk-drag-placeholder { | ||
opacity: 0; | ||
} | ||
|
||
.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"> | ||
<!-- Position Column --> | ||
<ng-container matColumnDef="position" sticky> | ||
<mat-header-cell *matHeaderCellDef> No. </mat-header-cell> | ||
<mat-cell *matCellDef="let element"> | ||
<mat-icon class="example-drag-cursor">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> |
49 changes: 49 additions & 0 deletions
49
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,49 @@ | ||
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; | ||
|
||
drop(event: CdkDragDrop<string>) { | ||
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add a background color to this one? Currently it's transparent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I'll add it.