Skip to content

Commit f01cb3a

Browse files
committed
table+filtering+sorting
1 parent c8574bc commit f01cb3a

File tree

7 files changed

+98
-1
lines changed

7 files changed

+98
-1
lines changed

section_15-angular-material_2/src/app/app-routing.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ import { TooltipsComponent } from './mat-components/tooltips/tooltips.component'
1515
import { TabsComponent } from './mat-components/tabs/tabs.component';
1616
import { DialogsComponent } from './mat-components/dialogs/dialogs.component';
1717
import { TypoComponent } from './mat-components/typo/typo.component';
18+
import { TableComponent } from './mat-components/table/table.component';
1819

1920
const routes: Routes = [
21+
{ path: 'table', component: TableComponent },
2022
{ path: 'typo', component: TypoComponent },
2123
{ path: 'courses/edit', component: DialogsComponent },
2224
{ path: 'dialogs', component: DialogsComponent },

section_15-angular-material_2/src/app/mat-components/material.module.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
1414
import { MatTooltipModule } from '@angular/material/tooltip';
1515
import { MatTabsModule } from '@angular/material/tabs';
1616
import { MatDialogModule } from '@angular/material/dialog';
17+
import { MatTableModule } from '@angular/material/table';
18+
// components
1719
import { CheckboxComponent } from './checkbox/checkbox.component';
1820
import { RadioComponent } from './radio/radio.component';
1921
import { SelectComponent } from './select/select.component';
@@ -28,6 +30,10 @@ import { TooltipsComponent } from './tooltips/tooltips.component';
2830
import { TabsComponent } from './tabs/tabs.component';
2931
import { DialogsComponent } from './dialogs/dialogs.component';
3032
import { TypoComponent } from './typo/typo.component';
33+
import { TableComponent } from './table/table.component';
34+
import { FilterPipe } from './table/filter.pipe';
35+
import { MatSortModule } from '@angular/material/sort';
36+
3137

3238

3339
@NgModule({
@@ -46,6 +52,8 @@ import { TypoComponent } from './typo/typo.component';
4652
TabsComponent,
4753
DialogsComponent,
4854
TypoComponent,
55+
TableComponent,
56+
FilterPipe,
4957
],
5058
imports: [
5159
CommonModule,
@@ -62,7 +70,9 @@ import { TypoComponent } from './typo/typo.component';
6270
MatProgressSpinnerModule,
6371
MatTooltipModule,
6472
MatTabsModule,
65-
MatDialogModule
73+
MatDialogModule,
74+
MatTableModule,
75+
MatSortModule
6676
]
6777
})
6878
export class MaterialModule { }
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Pipe, PipeTransform } from '@angular/core';
2+
3+
@Pipe({
4+
name: 'filter'
5+
})
6+
export class FilterPipe implements PipeTransform {
7+
8+
transform(items: any[], query: string): any[] {
9+
if (!query) {
10+
return items;
11+
}
12+
return items.filter((el: any) => {
13+
return JSON.stringify(el).toLowerCase().includes(query.toLowerCase());
14+
});
15+
}
16+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<div>
2+
<mat-form-field class="example-full-width">
3+
<input [(ngModel)]="query" matInput placeholder="Filter">
4+
</mat-form-field>
5+
</div>
6+
7+
<mat-table [dataSource]="dataSource | filter: query" matSort class="mat-elevation-z0">
8+
9+
<ng-container *ngFor="let item of displayedColumns">
10+
<ng-container [matColumnDef]="item">
11+
<mat-header-cell *matHeaderCellDef mat-sort-header> {{item}} </mat-header-cell>
12+
<mat-cell *matCellDef="let element"> {{element[item]}} </mat-cell>
13+
</ng-container>
14+
</ng-container>
15+
16+
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
17+
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
18+
</mat-table>

section_15-angular-material_2/src/app/mat-components/table/table.component.scss

Whitespace-only changes.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { Component, OnInit, ViewChild } from '@angular/core';
2+
import { TodosService } from '../../todos.service';
3+
import { MatSort } from '@angular/material/sort';
4+
import { MatTableDataSource } from '@angular/material/table';
5+
6+
export interface PeriodicElement {
7+
name: string;
8+
position: number;
9+
weight: number;
10+
symbol: string;
11+
}
12+
13+
const ELEMENT_DATA: PeriodicElement[] = [
14+
{position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
15+
{position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'},
16+
{position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li'},
17+
{position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be'},
18+
{position: 5, name: 'Boron', weight: 10.811, symbol: 'B'},
19+
{position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C'},
20+
{position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N'},
21+
{position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O'},
22+
{position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F'},
23+
{position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne'},
24+
];
25+
26+
@Component({
27+
selector: 'material-table',
28+
templateUrl: './table.component.html',
29+
styleUrls: ['./table.component.scss']
30+
})
31+
export class TableComponent implements OnInit {
32+
33+
public query: string;
34+
public displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
35+
36+
dataSource = new MatTableDataSource(ELEMENT_DATA);
37+
@ViewChild(MatSort, {static: true}) sort: MatSort;
38+
39+
40+
41+
constructor(private service: TodosService) { }
42+
43+
44+
ngOnInit(): void {
45+
this.dataSource.sort = this.sort;
46+
// this.service.getCollection().subscribe(res => this.todos = res);
47+
48+
}
49+
50+
}

section_15-angular-material_2/src/app/navbar/navbar.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<li><a [routerLink]="['/tooltips']">tooltips</a></li>
1818
<li><a [routerLink]="['/tabs']">tabs</a></li>
1919
<li><a [routerLink]="['/dialogs']">dialogs</a></li>
20+
<li><a [routerLink]="['/table']">table</a></li>
2021
</ul>
2122
</nav>
2223
</header>

0 commit comments

Comments
 (0)