Skip to content

Commit bc612dc

Browse files
committed
docs(cdk\drag-drop): Add table drag drop example
Adds an example that shows how to drag and drop table rows Fixes #25168
1 parent f245153 commit bc612dc

File tree

6 files changed

+127
-0
lines changed

6 files changed

+127
-0
lines changed

src/cdk/drag-drop/drag-drop.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,11 @@ whenever an item is about to be moved into a new index. If the predicate returns
238238
item will be moved into the new index, otherwise it will keep its current position.
239239

240240
<!-- example(cdk-drag-drop-sort-predicate) -->
241+
242+
### Reordering table rows
243+
Angular Material provides seamless integration of drag-and-drop functionality into tables,
244+
by adding the `cdkDropList` directive to your mat-table and handling the `(cdkDropListDropped)`
245+
event, you can enable drag-and-drop interactions within your table. This allows users to reorder
246+
rows or perform other custom actions with ease.
247+
248+
<!-- example(cdk-drag-drop-table) -->

src/components-examples/cdk/drag-drop/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ ng_module(
1313
"//src/cdk/drag-drop",
1414
"//src/cdk/overlay",
1515
"//src/cdk/portal",
16+
"//src/material/icon",
17+
"//src/material/table",
1618
],
1719
)
1820

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
table {
2+
width: 100%;
3+
}
4+
5+
.example-drag-cursor {
6+
margin-right: 16px;
7+
cursor: move;
8+
}
9+
10+
.cdk-drag-preview {
11+
box-sizing: border-box;
12+
border-radius: 4px;
13+
box-shadow: 0 5px 5px -3px rgba(0, 0, 0, 0.2),
14+
0 8px 10px 1px rgba(0, 0, 0, 0.14),
15+
0 3px 14px 2px rgba(0, 0, 0, 0.12);
16+
background-color: white;
17+
}
18+
19+
.cdk-drag-placeholder {
20+
opacity: 0;
21+
}
22+
23+
.cdk-drag-animating {
24+
transition: transform 250ms cubic-bezier(0, 0, 0.2, 1);
25+
}
26+
27+
.cdk-drop-list-dragging .mat-row:not(.cdk-drag-placeholder) {
28+
transition: transform 250ms cubic-bezier(0, 0, 0.2, 1);
29+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<mat-table #table [dataSource]="dataSource" class="mat-elevation-z8" cdkDropList (cdkDropListDropped)="drop($event)"
2+
cdkDropListData="dataSource">
3+
<!-- Position Column -->
4+
<ng-container matColumnDef="position" sticky>
5+
<mat-header-cell *matHeaderCellDef> No. </mat-header-cell>
6+
<mat-cell *matCellDef="let element">
7+
<mat-icon class="example-drag-cursor">reorder</mat-icon>
8+
<span>{{element.position}}</span>
9+
</mat-cell>
10+
</ng-container>
11+
12+
<!-- Name Column -->
13+
<ng-container matColumnDef="name">
14+
<mat-header-cell *matHeaderCellDef> Name </mat-header-cell>
15+
<mat-cell *matCellDef="let element"> {{element.name}} </mat-cell>
16+
</ng-container>
17+
18+
<!-- Weight Column -->
19+
<ng-container matColumnDef="weight">
20+
<mat-header-cell *matHeaderCellDef> Weight </mat-header-cell>
21+
<mat-cell *matCellDef="let element"> {{element.weight}} </mat-cell>
22+
</ng-container>
23+
24+
<!-- Symbol Column -->
25+
<ng-container matColumnDef="symbol">
26+
<mat-header-cell *matHeaderCellDef> Symbol </mat-header-cell>
27+
<mat-cell *matCellDef="let element"> {{element.symbol}} </mat-cell>
28+
</ng-container>
29+
30+
<!-- Quantity Column -->
31+
<ng-container matColumnDef="quantity">
32+
<mat-header-cell *matHeaderCellDef> Quantity of Element </mat-header-cell>
33+
<mat-cell *matCellDef="let element">{{element.quantity}}</mat-cell>
34+
</ng-container>
35+
36+
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
37+
<mat-row *matRowDef="let row; columns: displayedColumns;" cdkDrag [cdkDragData]="row"></mat-row>
38+
</mat-table>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import {Component, ViewChild} from '@angular/core';
2+
import {CdkDragDrop, CdkDropList, CdkDrag, moveItemInArray} from '@angular/cdk/drag-drop';
3+
import {MatTable, MatTableModule} from '@angular/material/table';
4+
import {MatIconModule} from '@angular/material/icon';
5+
6+
export interface PeriodicElement {
7+
name: string;
8+
position: number;
9+
weight: number;
10+
symbol: string;
11+
quantity: number;
12+
}
13+
14+
export const ELEMENT_DATA: PeriodicElement[] = [
15+
{position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H', quantity: 100},
16+
{position: 2, name: 'Helium', weight: 4.0026, symbol: 'He', quantity: 100},
17+
{position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li', quantity: 100},
18+
{position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be', quantity: 100},
19+
{position: 5, name: 'Boron', weight: 10.811, symbol: 'B', quantity: 100},
20+
{position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C', quantity: 100},
21+
{position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N', quantity: 100},
22+
{position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O', quantity: 100},
23+
{position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F', quantity: 100},
24+
{position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne', quantity: 100},
25+
];
26+
27+
/**
28+
* @title Drag&Drop table
29+
*/
30+
@Component({
31+
selector: 'cdk-drag-drop-table-example',
32+
templateUrl: 'cdk-drag-drop-table-example.html',
33+
styleUrl: 'cdk-drag-drop-table-example.css',
34+
standalone: true,
35+
imports: [CdkDropList, CdkDrag, MatTableModule, MatIconModule],
36+
})
37+
export class CdkDragDropTableExample {
38+
@ViewChild('table', {static: true}) table: MatTable<PeriodicElement>;
39+
40+
displayedColumns: string[] = ['position', 'name', 'weight', 'symbol', 'quantity'];
41+
dataSource = ELEMENT_DATA;
42+
43+
drop(event: CdkDragDrop<string>) {
44+
const previousIndex = this.dataSource.findIndex(d => d === event.item.data);
45+
46+
moveItemInArray(this.dataSource, previousIndex, event.currentIndex);
47+
this.table.renderRows();
48+
}
49+
}

src/components-examples/cdk/drag-drop/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ export {CdkDragDropOverviewExample} from './cdk-drag-drop-overview/cdk-drag-drop
1515
export {CdkDragDropRootElementExample} from './cdk-drag-drop-root-element/cdk-drag-drop-root-element-example';
1616
export {CdkDragDropSortingExample} from './cdk-drag-drop-sorting/cdk-drag-drop-sorting-example';
1717
export {CdkDragDropSortPredicateExample} from './cdk-drag-drop-sort-predicate/cdk-drag-drop-sort-predicate-example';
18+
export {CdkDragDropTableExample} from './cdk-drag-drop-table/cdk-drag-drop-table-example';

0 commit comments

Comments
 (0)