简体   繁体   中英

React Native Firebase search listview

I have a listview that is hydrated from my Firebase in my React Native app. I have came across a few tutorials on how people search listview's with the fetch function but nothing for Firebase.

I have a search bar at the header of my listview, how can I search by keyword and filter the results using Firebase?

I followed the guide on Firebase's website to setup my listview and datasource. I want to be able to search by keyword in each of those fields

listenForItems(itemsRef) {

itemsRef.on('value', (snap) => {

  var items = [];
  snap.forEach((child) => {
    items.push({
      title: child.val().title,
      category: child.val().category,
      description: child.val().description,
    });
  });

  this.setState({
    dataSource: this.state.dataSource.cloneWithRows(items)
  });

});
}

I finally figured this out. This code searches based on a specific item name.

Database

 {
  "directory" : {
    "macintosh" : {
      "address" : "117 East 11th Street, Hays, KS 67601",
      "firstLetter" : "m",
      "title" : "Macintosh",
    },
   "apple" : {
      "address" : "117 East 11th Street, Hays, KS 67601",
      "firstLetter" : "a",
      "title" : "apple",
    },
  },

ListView w/Search Bar

  render() {
    return (
      <View style={styles.container}>
      <ScrollView>
      <SearchBar
          returnKeyType='search'
          lightTheme
          placeholder='Search...'
          onChangeText={(text) => this.setState({searchText:text})}
          onSubmitEditing={() => this.firstSearch()}
      />
        {
          this.state.loading &&

          <ActivityIndicator
            size="large"
            color="#3498db"
            style={styles.activityStyle}
          />

        }
        <ListView
            dataSource={this.state.dataSource}
            renderRow={this._renderItem.bind(this)}
            enableEmptySections={true}
        />
        </ScrollView>
      </View>
    );
  }

Search Function

  firstSearch() {
    this.searchDirectory(this.itemsRef);
  }



searchDirectory(itemsRef) {

var searchText = this.state.searchText.toString();

if (searchText == ""){
  this.listenForItems(itemsRef);
}else{
  itemsRef.orderByChild("searchable").startAt(searchText).endAt(searchText).on('value', (snap) => {

    items = [];
    snap.forEach((child) => {
      items.push({
        address: child.val().address,
        firstLetter: child.val().firstLetter,
        title: child.val().title,
      });
    });


    this.setState({
      dataSource: this.state.dataSource.cloneWithRows(items)
    });

  });
}

}

This searches the firebase by replacing the listview datasource by using the orderbychild.startat().endat() firebase function. It's the closest thing to a "SELECT * BY %LIKE%" sql search functions.

I modified my code to search by the first letter as well. So incase someone searches for "Macbuk", it will still show the "Macbook" result. It only searches the M. I added another field in my database to the first letter of the title to search and made it lowercase.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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