[英]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:现在您需要转到上一页,您需要做的是:
[F,E,D,C,B,A]
[F,E,D,C,B,A]
E
and that is [D,C]
E
之后开始,即[D,C]
[C,D]
[C,D]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.