【问题标题】:Ionic 3 search is not showing list after clearing search bar清除搜索栏后,Ionic 3 搜索未显示列表
【发布时间】:2018-07-21 15:05:09
【问题描述】:

您好,请帮助我正在尝试从列表中进行搜索,单词显示在列表中,但清除搜索栏后没有显示任何内容,请帮助

这是我的 home.html

<ion-searchbar (ionInput)="getItems($event)"[showCancelButton]="shouldShowCancel" (ionCancel)="onCancel($event)"></ion-searchbar>
</ion-header>
<ion-content padding>
<ion-list>
  <button ion-item *ngFor="let item of items" (click)="eXplain()">
    {{ item.word }}
  </button>
</ion-list>
</ion-content>

而我的 home.ts 是:

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

searchQuery: string = '';
  items:any;
  word:any;
  meaning:any;

  constructor(public navCtrl: NavController,  private http: Http, public alertCtrl: AlertController, public loading: LoadingController,public modalCtrl : ModalController) {

   this.initializeItems();

  }

 ngOnInit(){   

    var headers = new Headers();
    headers.append("Accept", 'application/json');
    headers.append('Content-Type', 'application/json' );
    let options = new RequestOptions({ headers: headers });

     let loader = this.loading.create({
        content: 'Processing please wait...',
      });

     loader.present().then(() => {

    this.http.post('**MY URL IS HERE**',options)
    .map(res => res.json())
    .subscribe(res => {

     loader.dismiss()
    this.items=res.server_response;

    console.log(this.items);
    });
    });
     }

initializeItems() {

 this.items;

  }

  getItems(ev) {
    // Reset items back to all of the items
    this.initializeItems();

    // 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.items = this.items.filter((item: any) => {
        return (item.word.toLowerCase().indexOf(val.toLowerCase()) > -1);
      })
    }
  }
}

单词显示在列表中,但清除搜索栏后未显示任何内容,请帮助

【问题讨论】:

  • @Golden Mashego 你没有返回任何物品,如果输入值为空,你应该返回所有物品
  • 感谢回复,我还是 ionic 新手,该怎么做?

标签: angular search ionic-framework ionic2 ionic3


【解决方案1】:

当您收到响应时,在 ngOnInit 内部,将其深复制到另一个变量:

this.initialItems = res.server_response;
this.items = JSON.parse(JSON.stringify(this.initialItems));

不要在构造函数中调用 initializeItems,并将您的 initializeItems 方法更新为:

initializeItems() {
 this.items = JSON.parse(JSON.stringify(this.initialItems));
}

保持所有其他代码相同。

【讨论】:

  • 它的工作就像一个魅力:) 谢谢你,你是最好的@Shantanu
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-25
  • 1970-01-01
相关资源
最近更新 更多