Skip to content

Commit 87b6f6a

Browse files
author
Your Name
committed
Angular Material In Depth
1 parent f6c0bc9 commit 87b6f6a

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

src/app/course/course.component.html

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,33 @@ <h2>{{course?.description}}</h2>
1414
matSort matSortDisableClear matSortActive="seqNo" matSortDirection="asc"
1515
[dataSource]="lessons" multiTemplateDataRows>
1616

17+
<ng-container matColumnDef="select">
18+
19+
<th mat-header-cell *matHeaderCellDef>
20+
21+
<mat-checkbox [checked]="selection.hasValue() && isAllSelected()"
22+
[indeterminate]="selection.hasValue() && !isAllSelected()"
23+
24+
(change)="toggleAll()">
25+
26+
27+
</mat-checkbox>
28+
29+
30+
</th>
31+
32+
<td mat-cell *matCellDef="let lesson" (click)="$event.stopPropagation()">
33+
34+
<mat-checkbox (change)="onLessonToggled(lesson)"
35+
[checked]="selection.isSelected(lesson)">
36+
37+
</mat-checkbox>
38+
39+
40+
</td>
41+
42+
</ng-container>
43+
1744
<ng-container matColumnDef="seqNo">
1845

1946
<th mat-header-cell *matHeaderCellDef mat-sort-header>#</th>
@@ -40,7 +67,7 @@ <h2>{{course?.description}}</h2>
4067

4168
<ng-container matColumnDef="expandedDetail">
4269

43-
<td mat-cell *matCellDef="let lesson" colspan="3">
70+
<td mat-cell *matCellDef="let lesson" colspan="4">
4471
{{lesson.longDescription}}
4572
</td>
4673

src/app/course/course.component.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {CoursesService} from "../services/courses.service";
88
import {debounceTime, distinctUntilChanged, startWith, tap, delay, catchError, finalize} from 'rxjs/operators';
99
import {merge, fromEvent, throwError} from 'rxjs';
1010
import {Lesson} from '../model/lesson';
11+
import {SelectionModel} from '@angular/cdk/collections';
1112

1213

1314
@Component({
@@ -29,12 +30,15 @@ export class CourseComponent implements OnInit, AfterViewInit {
2930
@ViewChild(MatSort)
3031
sort: MatSort;
3132

33+
selection = new SelectionModel<Lesson>(true, []);
34+
35+
3236
constructor(private route: ActivatedRoute,
3337
private coursesService: CoursesService) {
3438

3539
}
3640

37-
displayedColumns = ['seqNo', "description", "duration"];
41+
displayedColumns = ['select', 'seqNo', "description", "duration"];
3842

3943
expandedLesson: Lesson = null;
4044

@@ -46,6 +50,14 @@ export class CourseComponent implements OnInit, AfterViewInit {
4650

4751
}
4852

53+
onLessonToggled(lesson:Lesson) {
54+
55+
this.selection.toggle(lesson);
56+
57+
console.log(this.selection.selected);
58+
59+
}
60+
4961
loadLessonsPage() {
5062

5163
this.loading = true;
@@ -93,6 +105,19 @@ export class CourseComponent implements OnInit, AfterViewInit {
93105

94106
}
95107

108+
isAllSelected() {
109+
return this.selection.selected?.length == this.lessons?.length;
110+
}
111+
112+
toggleAll() {
113+
if (this.isAllSelected()) {
114+
this.selection.clear();
115+
}
116+
else {
117+
this.selection.select(...this.lessons);
118+
}
119+
}
120+
96121
}
97122

98123

0 commit comments

Comments
 (0)