简体   繁体   English

如何根据所选选项从Firebase接收数据?

[英]How to receive data from firebase according to selected option?

I developing an ionic 2 app where people can add their blood types to firebase. 我开发了ionic 2应用程序,人们可以在其中添加自己的血型。

Now I want to implement a search-page to receive that data from firebase, For instance, somebody wants to search for B Rh+ blood type, simply selects that radio button and when they click search I want to search B Rh+ bloods in Firebase and list them. 现在,我想实现一个搜索页面以从Firebase接收该数据,例如,某人想要搜索B Rh +血型,只需选择该单选按钮,然后当他们单击搜索时,我想在Firebase中搜索B Rh +血型并列出他们。

here is my search html: 这是我的搜索HTML:

  <ion-list>
  <ion-item>
    <ion-label>Blood Type</ion-label>
    <ion-select [(ngModel)]="bloodtype">
      <ion-option value="ap">A Rh+</ion-option>
      <ion-option value="an">A Rh-</ion-option>
      <ion-option value="bp">B Rh+</ion-option>
      <ion-option value="bn">B Rh-</ion-option>
      <ion-option value="abp">AB Rh+</ion-option>
      <ion-option value="abn">AB Rh-</ion-option>
      <ion-option value="zp">0 Rh+</ion-option>
      <ion-option value="zn">0 Rh-</ion-option>
    </ion-select>
  </ion-item>

  <ion-item>
  <ion-range [(ngModel)]="range">
    <ion-icon range-left small name>0km</ion-icon>
    <ion-icon range-right small name>100km</ion-icon>
  </ion-range>
</ion-item>

<button ion-button full icon-left (click)="presentLoading()">
  <ion-icon name="search"></ion-icon>
  Search
</button>
</ion-list>

and here is my search.ts: 这是我的search.ts:

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { LoadingController } from 'ionic-angular';




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

  constructor(public navCtrl: NavController,public loadingCtrl: LoadingController) {}

  ionViewDidLoad() {
    console.log('Hello SearchPage Page');
  }

  presentLoading() {
    let loader = this.loadingCtrl.create({
      content: "Please wait...",
      duration: 3000
    });
    loader.present();
  }

}

I tried to use search bar and receive data with this reference this.bloodRef = firebase.database().ref('/bloods'); 我试图使用搜索栏并使用此引用this.bloodRef = firebase.database().ref('/bloods');接收数据this.bloodRef = firebase.database().ref('/bloods'); but using search bar is not functional since people can type "brh+" or "b" etc. 但是使用搜索栏不起作用,因为人们可以键入“ brh +”或“ b”等。

my firebase hierarchy: 我的firebase层次结构:

---bloods (id)

          ------ blood-type: "B Rh+"

You can filter items with orderBy and equalTo functions: 您可以使用orderBy和equalTo函数过滤项目:

var results = [];
this.bloodRef.orderByChild('blood-type').equalTo(this.bloodtype).once("value")
  .then(function(snapshot) {
    //snapshot.val() is the object
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM