|
@@ -1,58 +1,115 @@
|
|
|
-import { Component, OnInit } from '@angular/core';
|
|
|
|
|
-import {Student} from "./student.model";
|
|
|
|
|
-import {StudentService} from "./student.service";
|
|
|
|
|
|
|
+import { AfterViewInit, Component, Inject, OnInit, ViewChild } from '@angular/core';
|
|
|
|
|
+import { Student } from "./student.model";
|
|
|
|
|
+import { StudentService } from "./student.service";
|
|
|
|
|
+import { MatTable, MatTableDataSource } from "@angular/material/table";
|
|
|
|
|
+import { MatPaginator } from "@angular/material/paginator";
|
|
|
|
|
+import { SelectionModel } from "@angular/cdk/collections";
|
|
|
|
|
+import { MAT_DIALOG_DATA, MatDialog, MatDialogRef } from "@angular/material/dialog";
|
|
|
|
|
+import { MatSort } from "@angular/material/sort";
|
|
|
|
|
|
|
|
@Component({
|
|
@Component({
|
|
|
selector: 'app-students',
|
|
selector: 'app-students',
|
|
|
templateUrl: './students.component.html',
|
|
templateUrl: './students.component.html',
|
|
|
styleUrls: ['./students.component.css']
|
|
styleUrls: ['./students.component.css']
|
|
|
})
|
|
})
|
|
|
-export class StudentsComponent implements OnInit {
|
|
|
|
|
- studentList?: Student[];
|
|
|
|
|
|
|
+export class StudentsComponent implements OnInit, AfterViewInit {
|
|
|
|
|
+ displayedColumns: string[] = ['select', 'firstname', 'lastname', 'email', 'lastname'];
|
|
|
|
|
+ studentList = new MatTableDataSource<Student>([]);
|
|
|
student?: Student;
|
|
student?: Student;
|
|
|
|
|
+ selection = new SelectionModel<Student>(true, []);
|
|
|
|
|
+ @ViewChild(MatTable) table!: MatTable<Student>;
|
|
|
|
|
+ @ViewChild(MatPaginator) paginator!: MatPaginator;
|
|
|
|
|
+ @ViewChild(MatSort) sort!: MatSort;
|
|
|
|
|
|
|
|
- constructor(private studentService: StudentService) { }
|
|
|
|
|
|
|
+ constructor(private studentService: StudentService, public dialog: MatDialog) {
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ ngOnInit() {
|
|
|
|
|
+ this.getStudents();
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- ngOnInit() { this.getStudents(); }
|
|
|
|
|
|
|
+ ngAfterViewInit() {
|
|
|
|
|
+ this.studentList.sort = this.sort;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ isAllSelected() {
|
|
|
|
|
+ const numSelected = this.selection.selected.length;
|
|
|
|
|
+ const numRows = this.studentList.data.length;
|
|
|
|
|
+ return numSelected === numRows;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ masterToggle() {
|
|
|
|
|
+ if (this.isAllSelected()) {
|
|
|
|
|
+ this.selection.clear();
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ this.selection.select(...this.studentList.data);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ checkboxLabel(row?: Student): string {
|
|
|
|
|
+ if (!row) {
|
|
|
|
|
+ return `${this.isAllSelected() ? 'deselect' : 'select'} all`;
|
|
|
|
|
+ }
|
|
|
|
|
+ return `${this.selection.isSelected(row) ? 'deselect' : 'select'} row ${(row.id || 0) + 1}`;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
getStudents(): void {
|
|
getStudents(): void {
|
|
|
this.studentService.getStudents()
|
|
this.studentService.getStudents()
|
|
|
- .subscribe(studentList => this.studentList = studentList);
|
|
|
|
|
|
|
+ .subscribe(studentList => this.studentList.data = studentList);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- add(firstname: string, lastname: string, email: string, telephone: string): void {
|
|
|
|
|
|
|
+ add({firstname, lastname, email, telephone}: Student): void {
|
|
|
firstname = firstname.trim();
|
|
firstname = firstname.trim();
|
|
|
lastname = lastname.trim();
|
|
lastname = lastname.trim();
|
|
|
email = email.trim();
|
|
email = email.trim();
|
|
|
telephone = telephone.trim();
|
|
telephone = telephone.trim();
|
|
|
- this.studentService.addStudent({ firstname, lastname, email, telephone } as Student)
|
|
|
|
|
- .subscribe({
|
|
|
|
|
- next: (student: Student) => { this.studentList?.push(student); },
|
|
|
|
|
- error: () => {},
|
|
|
|
|
- complete: () => {
|
|
|
|
|
- if (this.studentList != undefined) {
|
|
|
|
|
- this.studentService.totalItems.next(this.studentList.length);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ this.studentService.addStudent({firstname, lastname, email, telephone} as Student).subscribe({
|
|
|
|
|
+ next: (student: Student) => {
|
|
|
|
|
+ this.studentList.data = [...this.studentList.data, student];
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ delete(): void {
|
|
|
|
|
+ const selectedStudents: Student[] = this.selection.selected;
|
|
|
|
|
+ this.studentService.deleteStudents(selectedStudents).subscribe({
|
|
|
|
|
+ next: ({ records }) => {
|
|
|
|
|
+ for (const rec of records) {
|
|
|
|
|
+ this.studentList.data = this.studentList.data.filter(student => student.id !== rec);
|
|
|
}
|
|
}
|
|
|
- });
|
|
|
|
|
|
|
+ },
|
|
|
|
|
+ });
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- delete(student: Student): void {
|
|
|
|
|
- this.studentList = this.studentList?.filter(c => c !== student);
|
|
|
|
|
- this.studentService.deleteStudent(student).subscribe(() => {
|
|
|
|
|
- // for automatic update of number of students in parent component
|
|
|
|
|
- if(this.studentList != undefined) {
|
|
|
|
|
- this.studentService.totalItems.next(this.studentList.length);
|
|
|
|
|
|
|
+ openDialog(): void {
|
|
|
|
|
+ const dialogRef = this.dialog.open(AddStudentDialog, {
|
|
|
|
|
+ width: '250px',
|
|
|
|
|
+ data: {},
|
|
|
|
|
+ });
|
|
|
|
|
+ dialogRef.afterClosed().subscribe(result => {
|
|
|
|
|
+ if (result) {
|
|
|
|
|
+ this.add(result);
|
|
|
}
|
|
}
|
|
|
- }
|
|
|
|
|
- );
|
|
|
|
|
|
|
+ })
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- deleteAll(): void {
|
|
|
|
|
- this.studentService.deleteStudents().subscribe(() => {
|
|
|
|
|
- }
|
|
|
|
|
- );
|
|
|
|
|
|
|
+ applyFilter(event: Event) {
|
|
|
|
|
+ const filterValue = (event.target as HTMLInputElement).value;
|
|
|
|
|
+ this.studentList.filter = filterValue.trim().toLowerCase();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+@Component({
|
|
|
|
|
+ selector: 'add-student-dialog',
|
|
|
|
|
+ templateUrl: 'add-student-dialog.component.html',
|
|
|
|
|
+})
|
|
|
|
|
+export class AddStudentDialog {
|
|
|
|
|
+ constructor(public dialogRef: MatDialogRef<AddStudentDialog>, @Inject(MAT_DIALOG_DATA) public student: Student) {
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ onNoClick(): void {
|
|
|
|
|
+ this.dialogRef.close();
|
|
|
|
|
+ }
|
|
|
|
|
+}
|