I have taken the search bar demo docs and changed the code to use this.items as a json array and not strings. I keep getting an EXCEPTION: Error during evaluation of “input”. yet if I go back to string there are no problems. i am just experimenting at this time to see what can be done with Ionic 2 search. Here is a sample of the code
import {Page} from 'ionic/ionic';
@Page({
templateUrl: 'build/pages/status/status.html',
})
export class Status {
constructor() {
this.searchQuery = '';
this.initializeItems();
}
initializeItems() {
this.items = [
{"name":"Moby Dick","position":"Big ass whale"},
{"name":"Jaws","position":"Fish with anger issues"}
];
}
getItems(searchbar) {
// Reset items back to all of the items
this.initializeItems();
// set q to the value of the searchbar
var q = searchbar.value;
// if the value is an empty string don't filter the items
if (q.trim() == '') {
return;
}
this.items = this.items.filter((v) => {
if (v.toLowerCase().indexOf(q.toLowerCase()) > -1) {
return true;
}
return false;
})
}
}
Thanks, that really helped as I was reading up on pipes as filters etc… It is people like yourself and luchillo17 that help out the rest of us new guys.
did that work for you? Because for me the indexof throws error.
Edit:
ok for some reason now it doesn’t throws an error (I think it was the changes I did to the tconfigjson or maybe is VSCode being crappy) but it doesn’t give me do anything to the array. It does inside the method but after the method does its thing I check the array again (from a button with a method with an alert) and it says the array is the same as originally.
Edit:
Got it to work. The html was fine (it is a little different from the example as I used the one they taught at angular.io) but the code is a little bit different.
What I did was the array initializer I put it inside the
if(q.trim()== ’ '){
initializeArray();
return;
}
The thing is that everytime I made changes it was initializing the array all the time. So what I did was I only initialized when the ion-searchbar is empty/whitespace/null (call it whatever.)
getItems(ev: any) {
// Reset items back to all of the items
//if(ev.target.value === “”){
this.initialize();
//}
// set val to the value of the searchbar
let val = ev.target.value;
// if the value is an empty string don’t filter the items
if (val && val.trim() != ‘’) {
this.items = this.items.filter((item) => {
let name: any = item;
return (name.firstName.toLowerCase().indexOf(val.toLowerCase()) > -1);
})
}
}
This highlighted part is the modification I made. Hope this will help you
hai i am now working in search filter.I am using json filter in search filter.But in that i am having one problem in that.in my page i getting all value.But when i type anything in search bar means it occur error.can you pls help me
HOME.ts
import { Component } from ‘@angular/core’;
import { NavController } from ‘ionic-angular’;
import {PeopleServiceProvider} from ‘…/…/providers/people-service/people-service’; @Component({
selector: ‘page-home’,
templateUrl: ‘home.html’,
providers: [PeopleServiceProvider]
})
export class HomePage {
data;
public users: any;
getItems(ev) {
// Reset items back to all of the items
this.loadPeople();
// set val to the value of the ev target
var val = ev.target.value;
// if the value is an empty string don't filter the items
if (val && val.trim() != '') {
this.users = this.users.filter((item) => {
return (item.toUpperCase().indexOf(val.toUpperCase()) > -1);
})
}