javascript - React Native Firebase 搜索 ListView

标签 javascript listview firebase react-native firebase-realtime-database

我的 React Native 应用程序中有一个 ListView ,它是从我的 Firebase 中提取的。我遇到了一些关于人们如何使用获取功能搜索 ListView 的教程,但没有找到 Firebase 的教程。

我的 ListView 的标题处有一个搜索栏,我如何按关键字搜索并使用 Firebase 过滤结果?

我按照 Firebase 网站上的指南设置 ListView 和数据源。我希望能够在每个字段中按关键字进行搜索

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)
  });

});
}

最佳答案

我终于明白了。此代码根据特定项目名称进行搜索。

数据库

 {
  "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

  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>
    );
  }

搜索功能

  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)
    });

  });
}

}

这通过使用 orderbychild.startat().endat() firebase 函数替换 ListView 数据源来搜索 firebase。它是最接近“SELECT * BY %LIKE%”sql 搜索功能的东西。

我修改了我的代码以也按第一个字母进行搜索。因此,如果有人搜索“Macbuk”,它仍会显示“Macbook”结果。它只搜索 M。我在我的数据库中添加了另一个字段到要搜索的标题的第一个字母,并将其设为小写。

关于javascript - React Native Firebase 搜索 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39991639/

相关文章:

javascript - Three.js - 一次加载 JSON 模型并多次添加

javascript - 从 Owl Carousel 创建多行轮播

android - 列出所有可见的页眉、页脚和空的 Activity

android - Android Studio 中带有自定义适配器的自定义 ListView

javascript - C# WPF 应用程序 webbrowser 无法运行 angularjs

android - 当用户单击按钮时,我如何通过 android 应用程序永远增加 firebase 数据库中的计数器

javascript - 如何在 AngularJS 中测试 Service 与 IndexedDB 的配合

javascript - Angular 火 : undefined is not a function ($on method does not exist)

android - 列表适配器的notifyDataSetChanges,只修改部分 View

java - 如何在 Firestore 中进行批量更新