import { Component, OnInit } from '@angular/core'; import {OffreStage} from…
- User
import { Component, OnInit } from '@angular/core'; import {OffreStage} from "./service/offre-stage.service"; import {Router} from "@angular/router"; import {FormGroup,FormControl} from "@angular/forms";
const ELEMENT_DATA: OffreStage[] = [ { id: 1, intitule: 'Développeur web', type: 'Stage', dateDebut: '2021-06-01', dateFin: '2021-09-01', etat: 'En cours', action: '' }, { id: 2, intitule: 'Développeur mobile', type: 'Stage', dateDebut: '2021-06-01', dateFin: '2021-09-01', etat: 'En cours', action: '' }, ]; @Component({ selector: 'app-offre-stage', templateUrl: './offre-stage.component.html', styleUrls: ['./offre-stage.component.scss'] })
export class OffreStageComponent implements OnInit { displayedColumns: string[] = ['intitule', 'type', 'dateDebut', 'dateFin', 'etat', 'action']; dataSource = ELEMENT_DATA; myform: FormGroup;
constructor(private router: Router) { this.myform = new FormGroup({ intitule: new FormControl(''), type: new FormControl(''), dateDebut: new FormControl(''), dateFin: new FormControl(''), etat: new FormControl(''), }); }
ngOnInit(): void { } }
- Assistant
The code provided is for an Angular component named
OffreStageComponent, which seems to be responsible for displaying and handling internship offers. Let's break down the different parts of the code for better understanding:-
Imports and Declarations:
Component,OnInitfrom@angular/core: These are Angular decorators and interfaces used for defining components and lifecycle hooks.FormGroup,FormControlfrom@angular/forms: These are used for managing form controls and their states.Routerfrom@angular/router: This is used for navigation.
-
Sample Data:
ELEMENT_DATA: This is a constant array ofOffreStageobjects. It serves as a sample data source for the component.
-
Component Metadata:
@Component({ selector: 'app-offre-stage', templateUrl: './offre-stage.component.html', styleUrls: ['./offre-stage.component.scss'] })This metadata is used to define the selector, template URL, and style URLs for the component.
-
Component Class:
displayedColumns: Defines the columns that will be displayed in the table.dataSource: The data source that feeds the table. In this case, it is theELEMENT_DATA.myform: A reactive form group that holds controls forintitule,type,dateDebut,dateFin, andetat.
-
Constructor and Initialization:
- The constructor sets up the form group with controls.
- The
ngOnInitmethod is a lifecycle hook that is called after the component is initialized. It's currently empty but can be used for initializing data or other startup tasks.
Here is the complete code for the
OffreStageComponentfor reference:import { Component, OnInit } from '@angular/core'; import { Router } from '@angular/router'; import { FormGroup, FormControl } from '@angular/forms'; interface OffreStage { id: number; intitule: string; type: string; dateDebut: string; dateFin: string; etat: string; action: string; } const ELEMENT_DATA: OffreStage[] = [ { id: 1, intitule: 'Développeur web', type: 'Stage', dateDebut: '2021-06-01', dateFin: '2021-09-01', etat: 'En cours', action: '' }, { id: 2, intitule: 'Développeur mobile', type: 'Stage', dateDebut: '2021-06-01', dateFin: '2021-09-01', etat: 'En cours', action: '' }, ]; @Component({ selector: 'app-offre-stage', templateUrl: './offre-stage.component.html', styleUrls: ['./offre-stage.component.scss'] }) export class OffreStageComponent implements OnInit { displayedColumns: string[] = ['intitule', 'type', 'dateDebut', 'dateFin', 'etat', 'action']; dataSource = ELEMENT_DATA; myform: FormGroup; constructor(private router: Router) { this.myform = new FormGroup({ intitule: new FormControl(''), type: new FormControl(''), dateDebut: new FormControl(''), dateFin: new FormControl(''), etat: new FormControl(''), }); } ngOnInit(): void { } }To complete the implementation, you will need the corresponding HTML and possibly CSS files. Here is a basic template for
offre-stage.component.html:<div class="container"> <form [formGroup]="myform"> <div class="form-group"> <label for="intitule">Intitulé</label> <input id="intitule" formControlName="intitule" type="text"> </div> <div class="form-group"> <label for="type">Type</label> <input id="type" formControlName="type" type="text"> </div> <div class="form-group"> <label for="dateDebut">Date de début</label> <input id="dateDebut" formControlName="dateDebut" type="date"> </div> <div class="form-group"> <label for="dateFin">Date de fin</label> <input id="dateFin" formControlName="dateFin" type="date"> </div> <div class="form-group"> <label for="etat">État</label> <input id="etat" formControlName="etat" type="text"> </div> </form> <table mat-table [dataSource]="dataSource" class="mat-elevation-z8"> <ng-container matColumnDef="intitule"> <th mat-header-cell *matHeaderCellDef> Intitulé </th> <td mat-cell *matCellDef="let element"> {{element.intitule}} </td> </ng-container> <ng-container matColumnDef="type"> <th mat-header-cell *matHeaderCellDef> Type </th> <td mat-cell *matCellDef="let element"> {{element.type}} </td> </ng-container> <ng-container matColumnDef="dateDebut"> <th mat-header-cell *matHeaderCellDef> Date de début </th> <td mat-cell *matCellDef="let element"> {{element.dateDebut}} </td> </ng-container> <ng-container matColumnDef="dateFin"> <th mat-header-cell *matHeaderCellDef> Date de fin </th> <td mat-cell *matCellDef="let element"> {{element.dateFin}} </td> </ng-container> <ng-container matColumnDef="etat"> <th mat-header-cell *matHeaderCellDef> État </th> <td mat-cell *matCellDef="let element"> {{element.etat}} </td> </ng-container> <ng-container matColumnDef="action"> <th mat-header-cell *matHeaderCellDef> Action </th> <td mat-cell *matCellDef="let element"> <!-- Define your action buttons here --> <button (click)="performAction(element)">Action</button> </td> </ng-container> <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr> <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr> </table> </div>This HTML creates a simple form using Angular's reactive forms and displays a table with the data from the
dataSource. Don't forget to install and import Angular Material components if you plan to use Material tables, buttons, etc.You might also want to implement the
performActionmethod in your component to handle actions on each row. -