I want to refresh my angular-material table (mat-table) when i insert a new book. The table is not refrsh automatically, i must refresh the browser for seeing the new book inserted.
Steps to do it
- Click to the "Create" button"
- Fill all fields in the MatDialog
- Click to the "ENVOYER" BUTTON
- A new book is inserted in the database
But the table doesn't load the last book inserted automatically.
How can i tell to my booklistcomponent (angular component which shows the mat-table) to add automatically the new book without refresh the page.
import { Component, OnInit, ViewChild } from '@angular/core';
import { MatPaginator } from '@angular/material/paginator';
import { MatTableDataSource } from '@angular/material/table';
import {MatDialog, MatDialogConfig, MAT_DIALOG_DATA, MatDialogRef} from '@angular/material/dialog';
import { Book } from '../models/book';
import { BookService } from '../services/book.service';
import { BookComponent } from '../book/book.component';
//import { BookComponent } from '../book/book.component';
@Component({
selector: 'app-booklist',
templateUrl: './booklist.component.html',
styleUrls: ['./booklist.component.css']
})
export class BooklistComponent implements OnInit {
public pbookList;
booksResult: Book[] ;
dataSource;
searchKey: string;
// private dialog: MatDialog;
displayedColumns: string[] = ['titre', 'auteur','isbn', 'date enregistrement', 'date publication', 'actions'];
// dataSource = new MatTableDataSource<Book>(this.booksResult);
listData: MatTableDataSource<Book>;
@ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;
constructor(private bookService: BookService, public dialog: MatDialog) { }
ngOnInit(): void {
this.chargerLivres();
this.dataSource = new MatTableDataSource<Book>(this.booksResult);
}
applyFilter() {
this.listData.filter = this.searchKey.trim().toLowerCase();
}
chargerLivres(){
console.log("list des books avant affcihage");
this.bookService.loadBooks().subscribe(
(result: Book[]) => {
console.log("list des books : ",result);
this.booksResult = result;
this.listData = new MatTableDataSource(result);
this.pbookList=result;
console.log("bookResult : ",this.booksResult);
},
error => {
console.log("Erreur : ",error);
}
);
}
onCreate() {
this.bookService.initializeFormGroup();
const dialogConfig = new MatDialogConfig();
dialogConfig.disableClose = true;
dialogConfig.autoFocus = true;
dialogConfig.width = "60%";
//dialogConfig.width = "350px";
this.dialog.open(BookComponent,dialogConfig);
}
}
<div class="container">
<div fxFlex="60">
<div class="search-div mr-2">
<button mat-raised-button (click)="onCreate()">
<mat-icon>add</mat-icon>Create
</button>
<mat-form-field class="ml-2 search-form-field" floatLabel="never">
<input matInput [(ngModel)]="searchKey" placeholder="Search" autocomplete="off" (keyup)="applyFilter()">
<button mat-button matSuffix mat-icon-button aria-label="Clear"*ngIf="searchKey" (click)="onSearchClear()">
<mat-icon>close</mat-icon>
</button>
</mat-form-field>
</div>
<div class="mat-elevation-z8">
<table mat-table [dataSource]="listData">
<ng-container matColumnDef="titre">
<th mat-header-cell *matHeaderCellDef> Titre </th>
<td mat-cell *matCellDef="let element"> {{element.title}} </td>
</ng-container>
<ng-container matColumnDef="auteur">
<th mat-header-cell *matHeaderCellDef> Auteur </th>
<td mat-cell *matCellDef="let element"> {{element.author}} </td>
</ng-container>
<ng-container matColumnDef="isbn">
<th mat-header-cell *matHeaderCellDef> Isbn </th>
<td mat-cell *matCellDef="let element"> {{element.isbn}} </td>
</ng-container>
<ng-container matColumnDef="date enregistrement">
<th mat-header-cell *matHeaderCellDef> Date enregistrement </th>
<td mat-cell *matCellDef="let element"> {{element.registerDate | date:'dd/MM/yyyy'}} </td>
</ng-container>
<ng-container matColumnDef="date publication">
<th mat-header-cell *matHeaderCellDef> Date publication </th>
<td mat-cell *matCellDef="let element"> {{element.releaseDate | date:'dd/MM/yyyy'}} </td>
</ng-container>
<ng-container matColumnDef="actions">
<th mat-header-cell *matHeaderCellDef>Actions</th>
<td mat-cell *matCellDef="let row">
<button mat-icon-button><mat-icon>launch</mat-icon></button>
<button mat-icon-button color="warn"><mat-icon>delete_outline</mat-icon></button>
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
<mat-paginator [pageSizeOptions]="[5, 10, 20]"></mat-paginator>
</div>
</div>
</div>
import { Component, Inject, OnInit } from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { NgxSpinnerService } from 'ngx-spinner';
import { Book } from '../models/book';
import { BookService } from '../services/book.service';
@Component({
selector: 'app-book',
templateUrl: './book.component.html',
styleUrls: ['./book.component.css']
})
export class BookComponent implements OnInit {
public book = new Book();
constructor(public service: BookService, private spinner: NgxSpinnerService,public dialogRef: MatDialogRef<BookComponent>,@Inject(MAT_DIALOG_DATA) public data: any) { }
ngOnInit(): void {
}
onNoClick(): void {
this.dialogRef.close();
}
onSubmit() {
if (this.service.form.valid) {
const formValue = this.service.form.value;
this.book.author = formValue['auteur'];
this.book.isbn = formValue['isbNum'];
this.book.category = formValue['categ'];
this.book.title = formValue['titre'];
this.book.totalExamplaries = formValue['totExemplaire'];
var localDate = new Date(formValue['publiDate']);
if(localDate.getTimezoneOffset() < 0){
localDate.setMinutes(localDate.getMinutes() - localDate.getTimezoneOffset() );
}else{
localDate.setMinutes(localDate.getMinutes() + localDate.getTimezoneOffset() );
}
this.book.releaseDate = localDate;
if (!this.service.form.get('$key').value){
//Insert the new book
this.saveNewBook(this.book);
}
else {
this.service.form.reset();
}
}
}
/**
* Save new book
* @param book
*/
saveNewBook(book: Book){
this.spinner.show();
this.service.saveBook(book).subscribe(
(result: Book) => {
console.log("Resultat du livre : ",result);
if(result.id){
this.spinner.hide();
console.log("Le livre a ajoute avec id : ",result.id);
}
},
error => {
this.spinner.hide();
console.log("erreur survenue lors de ajout : ",error);
}
);
}
onClear() {
this.service.form.reset();
this.service.initializeFormGroup();
}
onClose() {
this.service.form.reset();
this.service.initializeFormGroup();
this.dialogRef.close();
}
/**
* Save zone local date to the book releaseDate property :
* there is a recognized problem with datepicker @angular/material timezone conversion.
* @param book
*/
setLocalDateToDatePicker(book: Book){
var localDate = new Date(book.releaseDate);
if(localDate.getTimezoneOffset() < 0){
localDate.setMinutes(localDate.getMinutes() - localDate.getTimezoneOffset() );
}else{
localDate.setMinutes(localDate.getMinutes() + localDate.getTimezoneOffset() );
}
book.releaseDate = localDate;
}
}
<mat-toolbar>
<span>{{service.form.controls['$key'].value?"Modifier un livre":"Nouveau livre"}}</span>
<span class="fill-remaining-space"></span>
<button class="btn-dialog-close ml-auto" mat-stroked-button (click)="onClose()" tabIndex="-1"><mat-icon>clear</mat-icon></button>
</mat-toolbar>
<form [formGroup]="service.form" class="normal-form" (submit)="onSubmit()">
<mat-grid-list cols="2" rowHeight="300px">
<mat-grid-tile>
<div class="controles-container">
<input type="hidden" formControlName="$key">
<mat-form-field>
<input formControlName="titre" matInput placeholder="Titre*" >
<mat-error>Le titre est obligatoire.</mat-error>
</mat-form-field>
<mat-form-field>
<input formControlName="auteur" matInput placeholder="Auteur*" >
<mat-error>Le nom de l'auteur est obligatoire.</mat-error>
</mat-form-field>
<mat-form-field>
<input formControlName="isbNum" matInput placeholder="Isbn*" >
<mat-error>L'isbn est obligatoire.</mat-error>
</mat-form-field>
</div>
</mat-grid-tile>
<mat-grid-tile>
<div class="controles-container">
<input type="hidden" value="test" name="test">
<mat-form-field>
<input formControlName="totExemplaire" type="number" matInput placeholder="Total Exemplaires*" >
<mat-error>Le nombre d'exemplaire est obligatoire.</mat-error>
</mat-form-field>
<mat-form-field>
<input formControlName="publiDate" [matDatepicker]="myDatepicker" matInput placeholder="Date publication*" >
<mat-datepicker-toggle matSuffix [for] = "myDatepicker" placeholder="mm/dd/yyyy">
</mat-datepicker-toggle><mat-datepicker #myDatepicker></mat-datepicker>
<mat-error>Le date est obligatoire.</mat-error>
</mat-form-field>
<mat-form-field>
<mat-select formControlName="categ" placeholder="Categorie" >
<ng-container >
<mat-option value="C">Cuisine</mat-option>
<mat-option value="I">Informatique</mat-option>
<mat-option value="J">Jeunesse</mat-option>
<mat-option value="M">Mathématiques</mat-option>
<mat-option value="B">Bande dessiné</mat-option>
</ng-container>
</mat-select>
</mat-form-field>
<div class="button-row">
<button mat-raised-button color="primary" class="mr-2" type="submit" [disabled]="service.form.invalid">Envoyer</button>
<button mat-raised-button color="warn" (click)="onClear()">Effacer</button>
</div>
</div>
</mat-grid-tile>
</mat-grid-list>
</form>
this is the stackblitz link : https://stackblitz.com/github/lnquaidorsay/bibliofront or https://jvmipwlay.github.stackblitz.io/books
Any idea ?