ionic2: How to display the selected result in the search bar when using ion searchbar with REST API?

送分小仙女□ 提交于 2020-01-07 03:29:06

问题


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 ?

来源:https://stackoverflow.com/questions/42649745/ionic2-how-to-display-the-selected-result-in-the-search-bar-when-using-ion-sear

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!