import { Component, OnInit } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
name = 'Angular';
sortName = '';
employees: Array<any> = [
{ id: 1, name: 'hardik', age: 29, salary: '60000' },
{ id: 2, name: 'parth', age: 27, salary: '50000' },
{ id: 3, name: 'deepak', age: 29, salary: '50000' },
{ id: 4, name: 'john', age: 29, salary: '50000' }
];
filterValue: Array<any> = [];
order: boolean = false;
constructor() {
this.filterValue = this.employees;
}
ngOnInit() {
}
filterByText(initial: string) {
this.employees = this.filterValue;
this.employees = this.employees.filter(i => i.name.toLowerCase().indexOf(initial.toLocaleLowerCase()) !== -1);
console.log(this.employees);
}
sortData() {
if (this.order) {
this.employees = this.employees.sort((i, j) => (j.id > i.id ? -1 : 1));
}
else {
this.employees = this.employees.sort((i, j) => (j.id > i.id ? 1 : -1));
}
this.order = !this.order;
console.log(this.order);
}
}
<ul>
<li *ngFor="let e of employees">{{e.name}}
</li>
</ul>
filter : <input type="text" (keyup)="filterByText($event.target.value)">
<input type="button" (click)="sortData(order)" [value]="order ? 'Asc' : 'Desc'">