Utilizing the Ionic 2 searchbox to search for data within my list has raised a question in my mind. I am wondering how to console.log my data from that searchbox, specifically when I click on the search icon located within the searchbar.
https://i.sstatic.net/biG36.png
Alternatively, is there another method to filter the data, such as using an ion-input for the searchbox and then consoling the data upon clicking the search icon?
This is the TypeScript file I am working with:
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
@Component({
templateUrl: 'home.html',
})
export class HomePage {
private searchQuery: string = '';
private items: string[];
listitme:string= '' ;
constructor(private navCtrl: NavController) {
this.initializeItems();
}
initializeItems() {
this.items = [
'Harvey Burton',
'Barnett Crosby',
'Peck Brock',
'Rachel Robinson',
'Suzette Frazier',
'Bettie Maddox',
'Haley Bates',
'Tania Chandler',
'Woods Nicholson'
]
}
getItems(ev: any) {
this.initializeItems();
let val = ev.target.value;
if (val && val.trim() != '') {
this.items = this.items.filter((item) => {
return (item.toLowerCase().indexOf(val.toLowerCase()) > -1);
})
}
}
setitem(item){
this.listitme = item;
}
}
This is the HTML code structure:
<ion-header>
<ion-navbar>
<ion-title>search</ion-title>
</ion-navbar>
<ion-toolbar primary >
<ion-searchbar (ionInput)="getItems($event)" [(ngModel)]="listitme" ></ion-searchbar>
</ion-toolbar>
</ion-header>
<ion-content padding>
<ion-list>
<ion-item *ngFor="let item of items">
<div (click)=setitem(item) >
{{ item }}
</div>
</ion-item>
</ion-list>
</ion-content>