Skip to content

Commit 14f13b4

Browse files
committed
add example of a re-orederable table
1 parent 173f5fa commit 14f13b4

File tree

6 files changed

+91
-0
lines changed

6 files changed

+91
-0
lines changed

src/components-examples/material/table/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ ng_module(
2020
"//src/material/progress-spinner",
2121
"//src/material/sort",
2222
"//src/material/table",
23+
"//src/cdk/table",
24+
"//src/cdk/drag-drop",
2325
],
2426
)
2527

src/components-examples/material/table/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import {MatPaginatorModule} from '@angular/material/paginator';
99
import {MatProgressSpinnerModule} from '@angular/material/progress-spinner';
1010
import {MatSortModule} from '@angular/material/sort';
1111
import {MatTableModule} from '@angular/material/table';
12+
import {DragDropModule} from '@angular/cdk/drag-drop';
13+
import {CdkTableModule} from '@angular/cdk/table';
14+
1215
import {TableBasicFlexExample} from './table-basic-flex/table-basic-flex-example';
1316
import {TableBasicExample} from './table-basic/table-basic-example';
1417
import {TableDynamicColumnsExample} from './table-dynamic-columns/table-dynamic-columns-example';
@@ -36,6 +39,7 @@ import {
3639
} from './table-text-column-advanced/table-text-column-advanced-example';
3740
import {TableTextColumnExample} from './table-text-column/table-text-column-example';
3841
import {TableWrappedExample, WrapperTable} from './table-wrapped/table-wrapped-example';
42+
import {TableReorderableExample} from './table-reorderable/table-reorderable-example';
3943

4044
export {
4145
TableBasicExample, TableBasicFlexExample,
@@ -49,6 +53,7 @@ export {
4953
TableStickyFooterExample, TableStickyHeaderExample,
5054
TableTextColumnExample, TableTextColumnAdvancedExample,
5155
TableWrappedExample, WrapperTable,
56+
TableReorderableExample,
5257
};
5358

5459
const EXAMPLES = [
@@ -63,6 +68,7 @@ const EXAMPLES = [
6368
TableStickyFooterExample, TableStickyHeaderExample,
6469
TableTextColumnExample, TableTextColumnAdvancedExample,
6570
TableWrappedExample, WrapperTable,
71+
TableReorderableExample,
6672
];
6773

6874
@NgModule({
@@ -77,6 +83,8 @@ const EXAMPLES = [
7783
MatProgressSpinnerModule,
7884
MatSortModule,
7985
MatTableModule,
86+
CdkTableModule,
87+
DragDropModule,
8088
],
8189
declarations: EXAMPLES,
8290
exports: EXAMPLES,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
table {
2+
width: 100%;
3+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<table mat-table
2+
[dataSource]="dataSource"
3+
cdkDropList
4+
cdkDropListOrientation="horizontal"
5+
(cdkDropListDropped)="drop($event)">
6+
7+
<ng-container matColumnDef="position">
8+
<th mat-header-cell cdkDrag *matHeaderCellDef> No. </th>
9+
<td mat-cell *matCellDef="let element"> {{element.position}} </td>
10+
</ng-container>
11+
12+
<ng-container matColumnDef="name">
13+
<th mat-header-cell cdkDrag *matHeaderCellDef> Name </th>
14+
<td mat-cell *matCellDef="let element"> {{element.name}} </td>
15+
</ng-container>
16+
17+
<ng-container matColumnDef="weight">
18+
<th mat-header-cell cdkDrag *matHeaderCellDef> Weight </th>
19+
<td mat-cell *matCellDef="let element"> {{element.weight}} </td>
20+
</ng-container>
21+
22+
<ng-container matColumnDef="symbol">
23+
<th mat-header-cell cdkDrag *matHeaderCellDef> Symbol </th>
24+
<td mat-cell *matCellDef="let element"> {{element.symbol}} </td>
25+
</ng-container>
26+
27+
<tr mat-header-row *matHeaderRowDef="columns"></tr>
28+
<tr mat-row *matRowDef="let row; columns: columns;"></tr>
29+
</table>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { Component } from '@angular/core';
2+
import { CdkDragDrop, moveItemInArray } from '@angular/cdk/drag-drop';
3+
4+
/**
5+
* @title Table with re-orderable columns
6+
*/
7+
@Component({
8+
selector: 'table-reorderable-example',
9+
templateUrl: './table-reorderable-example.html',
10+
styleUrls: [ './table-reorderable-example.css' ]
11+
})
12+
export class TableReorderableExample {
13+
private originalColumnPositions: string[] = ['position', 'name', 'weight', 'symbol'];
14+
columns: string[] = Object.assign([], this.originalColumnPositions);
15+
dataSource = ELEMENT_DATA;
16+
17+
drop(event: CdkDragDrop<string[]>) {
18+
moveItemInArray(this.columns, this.getCurPosition(event.previousIndex), event.currentIndex);
19+
}
20+
21+
getCurPosition(colOldIndex: number): number {
22+
const colName: string = this.originalColumnPositions[colOldIndex];
23+
const p = (e: string) => e == colName;
24+
return this.columns.findIndex(e => e == colName);
25+
}
26+
}
27+
28+
export interface PeriodicElement {
29+
name: string;
30+
position: number;
31+
weight: number;
32+
symbol: string;
33+
}
34+
35+
const ELEMENT_DATA: PeriodicElement[] = [
36+
{position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
37+
{position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'},
38+
{position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li'},
39+
{position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be'},
40+
{position: 5, name: 'Boron', weight: 10.811, symbol: 'B'},
41+
{position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C'},
42+
{position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N'},
43+
{position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O'},
44+
{position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F'},
45+
{position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne'},
46+
];

src/dev-app/table/table-demo.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,6 @@ <h3>Table with mat-text-column advanced</h3>
6666

6767
<h3>Table wrapped in reusable component</h3>
6868
<table-wrapped-example></table-wrapped-example>
69+
70+
<h3>Table wrapped re-orderable columns</h3>
71+
<table-reorderable-example></table-reorderable-example>

0 commit comments

Comments
 (0)