employee.ts (parent)
export interface IEmployee {
id: number;
code: string;
name: string;
salary: number;
starRating: number;
}
employee-component.component.ts (parent)
import { Component, OnInit } from '@angular/core';
import { IEmployee } from './employee';
@Component({
selector: 'app-employee-component',
templateUrl: './employee-component.component.html',
styleUrls: ['./employee-component.component.css']
})
export class EmployeeComponentComponent implements OnInit {
constructor() {}
ngOnInit() {
this.listFilter = 'Patel';
}
private _listFilter: string = '';
get listFilter(): string {
return this._listFilter;
}
set listFilter(value: string) {
this._listFilter = value;
console.log('In Setter:', value);
this.filteredEmployees = this.performFilter(value);
}
filteredEmployees: IEmployee[] = [];
employees: IEmployee[] = [
{
id: 1,
code: 'VOD1410',
name: 'Akshay Patel',
salary: 3000,
starRating:3.5
},
{
id: 2,
code: 'VOD1710',
name: 'Panth Patel',
salary: 1500,
starRating:4
},
{
id: 2,
code: 'VOD0408',
name: 'Satish Patel',
salary: 5000,
starRating:4.5
}
];
performFilter(filterBy: string): IEmployee[] {
filterBy = filterBy.toLocaleLowerCase();
return this.employees.filter((employee: IEmployee) =>
employee.name.toLocaleLowerCase().includes(filterBy)
);
}
}
employee-component.component.html (parent)
<div>
<div>
<input type="text" [(ngModel)]="listFilter" />
</div>
<br />
<div>
Filter By: {{listFilter}}
</div>
<div>
<table>
<thead>
<td>Id</td>
<td>Code</td>
<td>Name</td>
<td>Salary</td>
<td>Rating</td>
</thead>
<tr *ngFor="let employee of filteredEmployees">
<td>{{employee.id}}</td>
<td>{{employee.code}}</td>
<td>{{employee.name}}</td>
<td>{{employee.salary}}</td>
<td><app-star [rating]="employee.starRating"></app-star></td>
</tr>
</table>
</div>
</div>
star.component.ts (child)
import { Component, Input, OnChanges, OnInit } from '@angular/core';
@Component({
selector: 'app-star',
templateUrl: './star.component.html',
styleUrls: ['./star.component.css']
})
export class StarComponent implements OnChanges {
constructor() {}
@Input() rating: number = 0;
cropWidth: number = 75;
ngOnChanges(): void {
this.cropWidth = this.rating * (75 / 5);
}
}
star.component.html (child)
<div class="crop" [style.width.px]="cropWidth" title="rating">
<div style="width:75px">
<span class="fa fa-star"></span>
<span class="fa fa-star"></span>
<span class="fa fa-star"></span>
<span class="fa fa-star"></span>
<span class="fa fa-star"></span>
</div>
</div>
star.component.css (child)
.crop {
overflow: hidden;
}
div {
cursor: pointer;
}
style.css
Install bootstrap font-awesome and import in style.css
@import '~bootstrap/dist/css/bootstrap.min.css';
@import 'font-awesome/css/font-awesome.min.css';
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { EmployeeComponentComponent } from './employee-component/employee-component.component';
import { StarComponent } from './shared/star/star.component';
@NgModule({
imports: [BrowserModule, FormsModule],
declarations: [AppComponent, EmployeeComponentComponent, StarComponent],
bootstrap: [AppComponent]
})
export class AppModule {}
app.component.html
<app-employee-component></app-employee-component>