Fin du cours n°4 avec les EventEmitter, Input, Output entre les composants hierarchisés

This commit is contained in:
Vincent BENOIT
2022-03-24 17:36:18 +01:00
parent 12deab061d
commit 83d148593c
7 changed files with 77 additions and 29 deletions

View File

@@ -12,6 +12,7 @@ import { EmployeeAddComponent } from './components/employee-add/employee-add.com
import { EmployeeEditComponent } from './components/employee-edit/employee-edit.component';
import { EmployeesNavbarComponent } from './components/employees/employees-navbar/employees-navbar.component';
import { EmployeesListComponent } from './components/employees/employees-list/employees-list.component';
import { EmployeeItemComponent } from './components/employees/employees-list/employee-item/employee-item.component';
@NgModule({
declarations: [
@@ -22,7 +23,8 @@ import { EmployeesListComponent } from './components/employees/employees-list/em
EmployeeAddComponent,
EmployeeEditComponent,
EmployeesNavbarComponent,
EmployeesListComponent
EmployeesListComponent,
EmployeeItemComponent
],
imports: [
BrowserModule,

View File

@@ -0,0 +1,16 @@
<tr *ngIf="employee">
<td>{{employee.id}}</td>
<td>{{employee.nom}}</td>
<td>{{employee.prenom}}</td>
<td>{{employee.role}}</td>
<td>
<button type="button" class="btn btn-outline-danger" (click)="onDelete(employee)">
<span class="fa fa-trash"></span>
</button>
</td>
<td>
<button type="button" class="btn btn-outline-info" (click)="onEdit(employee)">
<span class="fa fa-edit"></span>
</button>
</td>
</tr>

View File

@@ -0,0 +1,25 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { EmployeeItemComponent } from './employee-item.component';
describe('EmployeeItemComponent', () => {
let component: EmployeeItemComponent;
let fixture: ComponentFixture<EmployeeItemComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ EmployeeItemComponent ]
})
.compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(EmployeeItemComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});

View File

@@ -0,0 +1,28 @@
import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { Employee } from '../../../../model/employee.model';
import { AppDataState, DataStateEnum, EmployeeActionsTypes, ActEvent } from '../../../../state/employee.state';
@Component({
selector: 'app-employee-item',
templateUrl: './employee-item.component.html',
styleUrls: ['./employee-item.component.css']
})
export class EmployeeItemComponent implements OnInit {
@Input() employee?:Employee;
@Output() evtEmit:EventEmitter<ActEvent>=new EventEmitter();
constructor() { }
ngOnInit(): void { }
onDelete(val:Employee) {
this.evtEmit.emit({
type:EmployeeActionsTypes.DELETE_EMPLOYEE, payload:val
});
}
onEdit(val:Employee) {
this.evtEmit.emit({
type:EmployeeActionsTypes.EDIT_EMPLOYEE, payload:val
});
}
}

View File

@@ -16,22 +16,8 @@
</tr>
</thead>
<tbody>
<tr *ngFor="let p of result.data">
<td>{{p.id}}</td>
<td>{{p.nom}}</td>
<td>{{p.prenom}}</td>
<td>{{p.role}}</td>
<td>
<button type="button" class="btn btn-outline-danger" (click)="onDelete(p)">
<span class="fa fa-trash"></span>
</button>
</td>
<td>
<button type="button" class="btn btn-outline-info" (click)="onEdit(p)">
<span class="fa fa-edit"></span>
</button>
</td>
</tr>
<app-employee-item [employee]="p" (evtEmit)="onActionEvent($event)" *ngFor="let p of result.data" style="display: contents">
</app-employee-item>
</tbody>
</table>
</ng-container>

View File

@@ -15,18 +15,9 @@ export class EmployeesListComponent implements OnInit {
constructor() { }
ngOnInit(): void {
}
ngOnInit(): void { }
onDelete(val:Employee) {
this.evtEmit.emit({
type:EmployeeActionsTypes.DELETE_EMPLOYEE, payload:val
});
}
onEdit(val:Employee) {
this.evtEmit.emit({
type:EmployeeActionsTypes.EDIT_EMPLOYEE, payload:val
});
onActionEvent($event:ActEvent) {
this.evtEmit.emit($event);
}
}