简体   繁体   English

如何使用 AngularFire、Firestore 和 Firebase 分页到上一页

[英]How to paginate to previous page using AngularFire, Firestore and Firebase

Sorry I have seen this question has been asked many times in different ways here such as:对不起,我看到这个问题在这里以不同的方式被问过很多次,例如:

But NONE of the answers really explain the solution or are understandable.但是没有一个答案能真正解释解决方案或可以理解。

Also I went though many tutorials such as:我还学习了许多教程,例如:

To details:详情:

So here is what I have done so far所以这是我到目前为止所做的

let query = ref.orderBy(orderBy, asc ? 'asc' : 'desc').limit(limit);
        if (startAfter) {
          // Thiis works as it should
          query = query.startAfter(startAfter);
        }
        if (endBefore) {
          // This here does not work or give the correct results. I get the first page. 
          query = query.endBefore(endBefore);
        }
        return query;

So:所以:

query = query.startAfter(startAfter); works as expected.按预期工作。

However:然而:

query = query.endBefore(endBefore) does not end before that document I think it ends up to the limit. query = query.endBefore(endBefore)不会在该文档之前结束我认为它最终达到了限制。

As of firebase@7.3.0 you can use the Query.limitToLast(n: number) method - makes it much easier to move backward with paginated data.作为firebase@7.3.0可以使用Query.limitToLast(n: number)的方法-使得它更容易与分页数据向后移动。

Your implementation details might look something like this:您的实现细节可能如下所示:

function nextPage(last) {
   return ref.orderBy('age').startAfter(last.age).limit(3);
}

function prevPage(first) {
   return ref.orderBy('age').endBefore(first.age).limitToLast(3);
}

So I think I solved it by a comment inspired by the github issue mentioned above:所以我想我是通过一个受上面提到的 github 问题启发的评论来解决它的:

Supposing you have this array and you are sorting ascending假设你有这个数组并且你正在按升序排序

A,
B, 
C,
D,
E,
F, 

And you have a page limnit of 2 results并且您的页面限制为 2 个结果

Then when you are in the third page you should have然后当你在第三页时,你应该有

E,
F

Now you need to go to previous page and what you need todo is:现在您需要转到上一页,您需要做的是:

  1. Reverse the sorting order and our data should be [F,E,D,C,B,A]颠倒排序顺序,我们的数据应该是[F,E,D,C,B,A]
  2. startAfter the first document of the currently viewed page (in our case E) startAfter 当前查看页面的第一个文档(在我们的例子中)
  3. Query the firestore to get the results (eg with your limit of 2).查询 firestore 以获取结果(例如,您的限制为 2)。 You should get with reverse order and starting after E and that is [D,C]您应该以相反的顺序并在E之后开始,即[D,C]
  4. Reverse the above array so it will be [C,D]反转上面的数组所以它会是[C,D]
  5. Done完毕

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

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