在Ionic中对AngularFireList的搜索功能

时间:2018-03-26 22:16:38

标签: javascript angular firebase ionic-framework firebase-realtime-database

我在绕过这个搜索功能时遇到了一些麻烦。我现在已经设置了它,所以当我搜索它时它会得到正确的项目,但它的拼写必须是精确的,包括大写和标点符号。我希望它能够获得该项目,无论用户搜索术语的大小写是什么,如果他们只是键入了字母' b'它将包括所有具有' b'在项目字段中。

我知道我想查询对数据库的调用,因为在客户端进行调用会非常繁重但是你们怎么想?或者你将如何实现这一目标?

 setFilteredItems() { 

    this.employeeListRef = this.database.list('userProfile', 
    ref=> ref.orderByChild('lastName'));


    this.employeeList = this.employeeListRef.snapshotChanges()
    .map(
      changes => {
        return changes.map(c => ({
          key: c.payload.key, ...c.payload.val()
        }))
      }
    );

    //if searchterm is null it returns so it can set back the list to all values
    //searchterm is declared in constructor
    if(!this.searchTerm) {
      return;
    }

    //var term = this.searchTerm.toLowerCase();

    this.employeeListRef = this.database.list('userProfile', 
    ref => ref.orderByChild('lastName').equalTo(term));


      this.employeeList = this.employeeListRef.snapshotChanges()
      .map(
        changes => {
          return changes.map(c => ({
            key: c.payload.key, ...c.payload.val()
          }))
        }
      );


  }

1 个答案:

答案 0 :(得分:1)

如果您查看ASCII table,就可以了解Firebase如何存储记录以及orderByChild可能无法正常运行的原因。

b98B66。他们在ASCII表上的不同位置。

您可以尝试两种方法来帮助您访问所需表达式中的数据。

  1. 尝试使用database methods
  2. 用户将可搜索数据转换为lowercase
  3. 使用云功能和写入记录,在对象中保存该记录的lowercase版本,然后按该记录进行搜索。一个例子是;

    { lastName: 'Smith', lowercaseLastName: 'smith' }
    
  4. 然后你orderByChild('lowercaseLastName')