Search bar

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);
    });
  }

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);

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

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

Can you please give your array of this.items?

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

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);
}

thanks Ankit. its works