Firestore pagination - Is there any query compatible with firebase's limitToLast?

前端 未结 5 1429
执念已碎
执念已碎 2020-12-18 09:46

Is there a way to implement back pagination with firestore? I am struggling to implement pagination with firestore, and there are limited firestore queries for it. Forward p

5条回答
  •  情书的邮戳
    2020-12-18 10:02

    Yes. Building upon Frank's answer...

    Have something like this in your query...

        if (this.next) {
          // if next, orderBy field descending, start after last field
          q.orderBy('field', 'desc');
          q.startAfter(this.marker);
        } else if (this.prev) {
          // if prev, orderBy field ascending, start after first field
          q.orderBy('field', 'asc');
          q.startAfter(this.marker);
        } else {
          // otherwise just display first page results normally
          q.orderBy('field', 'desc');
        }
        q.limit(this.pageSize);
    

    and then reverse it when you get the query...

        this.testsCollection
                .valueChanges({ idField: 'id' })
                .pipe(
                  tap(results => {
                    if (this.prev) {
                      // if previous, need to reverse the results...
                      results.reverse();
                    }
                  })
                )
    

提交回复
热议问题