I am trying to filter an array of object on basis on last_name.
filterItems(searchTerm) {
return this.EmpData.filter(emp => {
return emp.lastName.toLowerCase().indexOf(searchTerm.toLowerCase()) > -1 || driver.firstName.toLowerCase().indexOf(searchTerm.toLowerCase()) > -1;
});
}
and in html template
<ion-list *ngIf="showEmpList">
<ion-item *ngFor="let item of EmpData" button (click)="selectEmp(item.lastName)">
<ion-label>{{item.lastName}}, {{item.middle}}, {{item.firstName}}</ion-label>
</ion-item>
</ion-list>
this is working when i am typing name in search box but when i am deleting characters from name that result is not refreshing with new data.
for example-
i have data=[{name:'Amir','age':28},{name:'AAmir', age:26},{name:'AAAmir', age:30}]
now i if put AA in search box it will display last two result and if i put AAA then it will display only last emp data now if i delete two or more characters from input field suppose new input is A then result is displaying as only last employee data
please help.Thanks in advance
May be your filtered array is pointing to same array as original, thats why you are altering original Array. I have done some code for same :-
Stackblitz URL :- https://stackblitz.com/edit/ionic-gqingy
HTML :-
<input type="text" [(ngModel)]="filterText" (ngModelChange)="filterValues()"/>
<ion-list>
<ion-item *ngFor="let item of filteredData" button (click)="selectEmp(item.lastName)">
<ion-label>{{item.name}}, {{item.age}}</ion-label>
</ion-item>
</ion-list>
TS :-
import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { TabsPage } from '../pages/tabs/tabs';
@Component({
templateUrl: 'app.html'
})
export class MyApp {
rootPage:any = TabsPage;
EmpData = [{name:'Amir',age:28},{name:'AAmir', age:26},{name:'AAAmir', age:30}];
filteredData = [{name:'Amir',age:28},{name:'AAmir', age:26},{name:'AAAmir', age:30}];
public filterText: string = '';
constructor(platform: Platform) {
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
});
}
public filterValues() {
this.filteredData = this.EmpData.filter((data) => data.name.toLowerCase().includes(this.filterText? this.filterText.toLowerCase():''));
}
}