Bonjour à tous,
Je n'arrive pas à mettre en place une datatable qui s'affiche après sélection d'un bouton par un utilisateur.
Le but est que l'utilisateur sélectionner un bouton, une fois la selection faite, un nouveau get part pour récupérer un JSON et afficher le contenu dans un tableau.
Actuellement ce qui fonctionne :
L'utilisateur peut selectionner, le get est envoyé, le contenu est OK je peut le récupérer et l'afficher mais la table ne se remplit pas car je ne vois pas comment initialiser la table ou la remplir une fois le get récupéré.
La datatable au niveau html est correcte. C'est vraiment au niveau de comment l'initialiser qu'il y a un problème.
J'utilise "async" et ne veut pas utiliser "subscribe".
Code HTML : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 <!-- WORKING --> <form class="example-form"> <mat-form-field class="example-full-width"> <input type="text" placeholder="Namespace" aria-label="Number" matInput [formControl]="myControl" [matAutocomplete]="auto" > <mat-autocomplete #auto="matAutocomplete" (optionSelected)='getServices($event.option.value)'> <mat-progress-bar mode="buffer"></mat-progress-bar> <mat-option *ngFor="let option of filteredOptions | async" [value]="option.namespace"> {{option.namespace}} </mat-option> </mat-autocomplete> </mat-form-field> </form> <!-- WORKING --> <form class="example-form" *ngIf="services"> <mat-form-field class="example-full-width"> <input type="text" placeholder="Namespace" aria-label="Number" matInput [formControl]="myControl2" [matAutocomplete]="auto2" > <mat-autocomplete #auto2="matAutocomplete"> <mat-progress-bar mode="buffer"></mat-progress-bar> <mat-option *ngFor="let service of services | async" [value]="service.service"> {{service.service}} </mat-option> </mat-autocomplete> </mat-form-field> </form> <!-- EMPTY --> <div class="wrapper" *ngIf="services"> <table mat-table [dataSource]="dataSource" matSort class="mat-elevation-z8" > <ng-container matColumnDef="id" *ngFor="let service of services | async"> <th mat-header-cell *matHeaderCellDef mat-sort-header> No. </th> <td mat-cell *matCellDef="let service"> {{service.id}} </td> </ng-container> <ng-container matColumnDef="service" *ngFor="let service of services | async"> <th mat-header-cell *matHeaderCellDef mat-sort-header> No. </th> <td mat-cell *matCellDef="let service"> {{service.service}} </td> </ng-container> <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr> <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr> </table> <!-- Angular 8 pagination --> <mat-paginator [pageSizeOptions]="[5, 10, 15]" showFirstLastButtons></mat-paginator> </div>
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 import { Component, OnInit, Injectable, ViewChild } from '@angular/core'; import { RestApiService } from '../shared/rest-api.service'; import { FormControl } from '@angular/forms'; import { Observable, of, Subscription } from 'rxjs'; import { tap, startWith, debounceTime, switchMap, map, filter, distinctUntilChanged } from 'rxjs/operators'; import {MatPaginator} from '@angular/material/paginator'; import {MatSort} from '@angular/material/sort'; import {MatTableDataSource} from '@angular/material/table'; @Injectable({ providedIn: 'root' }) @Component({ selector: 'app-services-namespace', templateUrl: './services-namespace.component.html', styleUrls: ['./services-namespace.component.scss'] }) export class ServicesNamespaceComponent implements OnInit { myControl = new FormControl(); myControl2 = new FormControl(); options = []; filteredOptions: Observable<any[]>; namespaces = []; services: Observable<any[]>; displayedColumns: string[] = ['id', 'service']; dataSource: MatTableDataSource<any>; @ViewChild(MatPaginator, {static: true}) paginator: MatPaginator; @ViewChild(MatSort, { static: true }) sort: MatSort; constructor(private dataService: RestApiService) { this.filteredOptions = this.myControl.valueChanges.pipe( startWith(''), debounceTime(500), distinctUntilChanged(), switchMap(val => { return this.filter(val || '') }) ) } ngOnInit() { } filter(val: string): Observable<any[]> { // call the service which makes the http-request return this.dataService.getAllNameSpaces() .pipe( map(response => response.filter(option => { return option.namespace.toLowerCase().indexOf(val.toLowerCase()) === 0 })) ) } getServices(namespace){ this.services = this.dataService.getAllServices("services"); this.dataSource.paginator = this.paginator; this.dataSource.sort = this.sort; } }
Service Rest pour les call de GET
Merci pour l'aide
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { tap, startWith, debounceTime, switchMap, map } from 'rxjs/operators'; import { Observable, of, Subscription } from 'rxjs'; @Injectable({ providedIn: 'root' }) export class RestApiService { private REST_API_SERVER_BASE_LINK = "http://localhost:3000"; constructor(private httpClient: HttpClient) { } data = []; public getAllNameSpaces(){ return this.httpClient.get<any>(this.REST_API_SERVER_BASE_LINK + '/namespace').pipe(tap(data => this.data = data)); } // Le GET concerné est ici public getAllServices(namespace) : Observable<any[]> { return this.httpClient.get<any[]>(this.REST_API_SERVER_BASE_LINK + '/' + namespace).pipe(tap(data => this.data = data)); } }
En image ce que ça donne actuellement
![]()
Partager