Search bar


#1

hi guys,
facing problem related to search function
showing error when i m typing a value in search bar
code working when searching by StudentName. bt when searching by StudentId
ERROR TypeError: data.StudentID.toLowerCase is not a function

return (data.StudentID.toLowerCase().indexOf(item.trim().toLowerCase()) > -1); ----> showing error bcoz id contains number
return (data.StudentName.toLowerCase().indexOf(item.trim().toLowerCase()) > -1); ----> working

any idea show to search by id
here is my code:
html:

<ion-header>
    <ion-navbar hideBackButton>
      
      <ion-searchbar *ngIf="toggled" [(ngModel)]="searchstudent" [showCancelButton]="true" cancelButtonText="close" (ionCancel)="toggleSearch()"
        (ionInput)="getFilteredItem($event)"></ion-searchbar>

  </ion-navbar>
</ion-header>

and here is my .ts

  getFilteredItem(searchbar) {
    var item = searchbar.target.value;
    this.attendenceList = this.items.filter((data) => {
      //let myItem = JSON.parse(JSON.stringify(data))
      return (data.StudentID.toLowerCase().indexOf(item.trim().toLowerCase()) > -1);
    });
  }

#2

As you know that you can’t perform indexOf and toLowerCase method on number.
So, First you’ll have to convert it to string.

return (data.StudentID.toString().toLowerCase().indexOf(item.trim().toLowerCase()) > -1);


#3

already tried showing error ERROR TypeError: Cannot read property ‘toString’ of undefined in this case :frowning:


#4

Then your this.items variable doesn’t have property called “StudentID”

Can you please give your array of this.items?


#5

sure ,
{
“StudentID”: 15141,
“StudentName”: “PRAVESH SINGH”,
“RollNo”: “160511”,
“AttendanceDate”: “2017-08-09T00:00:00”,
“SubjectID”: 599,
“SubjectCode”: “MSO203B”,
“Remarks”: “A”,
“AttendanceTime”: “”
}


#6

Looking at your array it should work.
but, In your case ‘toString’ of undefined can only come when your array does not have property StudentID. (or maybe check for mistype :grinning: )
Please check all your entries for StudentID property.

and if you are not sure that all the entry will have StudentID property then,

if(data.StudentID){
     return (data.StudentID.toString().toLowerCase().indexOf(item.trim().toLowerCase()) > -1);
}

#7

thanks Ankit. its works