In my Ionic application, I have implemented a search bar to filter and search through a list. The filtering process is triggered as soon as I start typing in the search bar. However, the updated results are not displayed on the screen until I manually hide the keyboard by clicking the arrow down button. Let me illustrate this with the following images:
Here is an image of the list before initiating a search: https://i.stack.imgur.com/WIlUw.jpg
Next, here is an image of me attempting to search for "abc". Even though the searching process has already occurred in the background, the updated results are not visible yet (I want the search to happen with every keystroke). https://i.stack.imgur.com/7VYhB.jpg
Only after tapping the arrow down button from the previous image, the filtered/searched results finally appear. https://i.stack.imgur.com/I3GA2.jpg
HTML:
<ion-content padding>
<ion-list>
<div *ngFor="let period of periodsClone; let j = index">
<ion-item-divider>{{ period.month | monthNameFormat }} {{ period.year }}
<div item-end><b>€{{ period.sum | number : '1.2-2' }}</b></div>
</ion-item-divider>
<div *ngFor="let declaration of period.declarations;let i = index;">
<button ion-item text-wrap detail-none (click)="openDeclaration(period.number,declaration.id);">
<ion-icon item-start style="color: grey;" name="time" *ngIf="declaration.status == '1';"></ion-icon>
<ion-icon item-start style="color: red;" name="close" *ngIf="declaration.status == '2';"></ion-icon>
<ion-icon item-start style="color: lightgreen;" name="checkmark" *ngIf="declaration.status == '3';"></ion-icon>
<h2>{{ declaration.name }}</h2>
<p>{{ declaration.date | date:'dd-MM-yyyy' }}</p>
<p style="color: red;">{{ declaration.comment }}</p>
<div item-end>€ {{ declaration.amount | number : '1.2-2'}}</div>
<ion-icon name="arrow-forward" item-end></ion-icon>
</button>
</div>
<br>
<br>
</div>
</ion-list>
</ion-content>
<ion-footer>
<ion-searchbar [(ngModel)]="searchInput" (ionInput)="search($event)">
</ion-searchbar>
</ion-footer>
Typescript:
search(ev){
this.periodsClone = this.periods.map(x => JSON.parse(JSON.stringify(x)));
let value = ev.target.value.toLowerCase();
for (let i = 0; i < this.periodsClone.length; i++) {
for (let j = 0; j < this.periodsClone[i].declarations.length; j++) {
let toRemove = false;
(this.periodsClone[i].declarations[j].name.toLowerCase().includes(value) || this.periodsClone[i].declarations[j].description.toLowerCase().includes(value) ? toRemove = false : toRemove = true);
(toRemove ? (this.periodsClone[i].declarations.splice(j,1) , j = 0) : false);
}
}
}
And:
private periods: Period[] = [];
private periodsClone: Period[] = [];
private searchInput: string;
And:
ionViewWillEnter() {
this.periods = this.declarationService.getPeriods();
this.periodsClone = this.declarationService.periodsDuplicate;
}
How can I ensure that the search functionality triggers immediately upon entering a character in the search field?
PS: This issue is observed on both my Nexus 6 and Huawei P10 devices.
EDIT:
I am still grappling with this problem, and meanwhile, I made some modifications to the search function code:
search(event) {
this.periodsClone = this.periods.map(x => JSON.parse(JSON.stringify(x)));
let value = event.target.value.toLowerCase();
if (value != '') {
this.periodsClone = this.periodsClone.filter((period) => {
return (
period.declarations.filter((declaration) => {
return (
declaration.name.toLowerCase().includes(value) ||
declaration.description.toLowerCase().includes(value)
)
}).length > 0
)
})
}
console.log(this.periodsClone);
}
Period:
//Entities
import { Declaration } from './declaration.entity';
export class Period {
constructor(
public id: number,
public status: string,
public year: number,
public month: number,
public sum: number,
public orderNumber: string,
public userId: string,
public submitDate: Date,
public comment: string,
public declarations: Declaration[]
) { }
}
Declaration:
//Entities
import { FileCustom } from './file.entity';
import { Period } from './period.entity';
import { Project } from './project.entity';
export class Declaration {
constructor(
public id: number,
public status: string,
public name: string,
public description: string,
public amount: number,
public date: Date,
public period: Period,
public userId: string,
public files: FileCustom[],
public comment: string,
public project: Project
) { }
}
Currently, the challenge lies in reproducing the issue consistently, as it occurs intermittently. I have captured both scenarios:
EDIT 2:
I also attempted using a regular input method like so:
<ion-input (keyup)="search($event)" placeholder="Search" clearInput></ion-input>
However, the same issue persists despite trying this approach.