0

I want to create a dropdown list with searchbar with the list containing data from a REST API. Initially in the searchbar I created I could just select the item but couldn't display that in the searchbar. I want the selected item to be displayed in the searchbar.How can I display that selected item.

I need to display it because I'm building a cascading dropdown list where the input of the first list is served to the second list.

I'll be thankful if someone can provide me the code by putting mind that the data is being rendered from a REST API using POST method.This is my function which gets called in the html page. userData is an array of type object since I need to get JSON data.I get an error saying property toLowerCase cannot exist on of the type Object.

    loadusers(ev:any){
  this.UserService.post('search/getusers',{}).subscribe(resp=>{
     this.userData = resp.data;
     console.log(this.userData); 
    },
    err=>{console.log(err);},
    ()=>{});

     let val = ev.target.value;

    // if the value is an empty string don't filter the items
    if (val && val.trim() != '') {
      this.userData = this.userData.filter((item) => {
        return (item.toLowerCase().indexOf(val.toLowerCase()) > -1);
      })
    }
  }

This is the HTML file where I'm trying to display the data along with a searchabar.I also don't know what to include in the isChanged function.

   <ion-searchbar  [(ngModel)]="mySearchInput" (ionInput)="loadusers($event)"></ion-searchbar>
<ion-content >
<ion-list radio-group [(ngModel)]="selectedValue">
<ion-item class="border" *ngFor="let val of userData">
<ion-label>{{val.name}}</ion-label>
<ion-radio class="resolvedState" value="{{val.name}}" (ionSelect)="isChanged(val)"></ion-radio>
</ion-item>
</ion-list> 

What is the best practise to create a searchbar with these features ?

Impromptu_Coder
  • 425
  • 3
  • 7
  • 27
  • What does console.log(this.userData) give? – Suraj Rao Mar 07 '17 at 13:49
  • That is invoking the service and displaying it on the console. – Impromptu_Coder Mar 08 '17 at 05:01
  • `property toLowerCase cannot exist on of the type Object` it means you need to check what in userData you are trying to convert to lowercase – Suraj Rao Mar 08 '17 at 05:03
  • I don't know if that is the correct code. I used it because this is given in the ionic2 website for searchbar :https://ionicframework.com/docs/v2/components/#searchbar – Impromptu_Coder Mar 08 '17 at 05:35
  • All I need to do is to select an item by searching using the searchbar and I want that selected item to be displayed in the searchbar. Could you also tell me what is autocomplete ? Will that help me select an item and display it in the searchbar ? – Impromptu_Coder Mar 08 '17 at 05:36
  • http://stackoverflow.com/questions/42427915/ionic-2-google-places-and-autocomplete/42607334#42607334 could help – Suraj Rao Mar 08 '17 at 06:34
  • I tried implementing it but could you please tell me how do I do it for normal data and not for places. I'm actually quite new to ionic2. – Impromptu_Coder Mar 08 '17 at 12:38

0 Answers0