javascript - 如何使用 Firebase 对过滤后的结果进行分页?
问题描述
我有一个保存在这样构建的 firestore 集合中的客户列表:
客户_1234
- 姓名:客户1
- 类别:类别1
- 城市:city1
- 国家:国家1
客户_5678
- 姓名:客户2
- 类别:类别2
- 城市:city2
- 国家:国家2
其他客户...
现在,我想创建一个带有一些过滤器的客户存档页面(按名称、按类别、按城市、按国家/地区)。
我想使用无限滚动系统一次显示 20 个结果。
例如,我可以请求某个城市的所有客户,然后我可以添加第二个过滤器(按类别)并将它们混合在一起。
构建这个系统的最佳方法是什么?
谢谢!:)
解决方案
您只需使用文档中描述的技术对查询进行分页。
例如,在您的最后一个示例中:
var first = db.collection("customers")
.where('city', '==', 'city1')
.where('category', '==', 'category1')
.limit("20");
return first.get().then(function (documentSnapshots) {
// Get the last visible document
var lastVisible = documentSnapshots.docs[documentSnapshots.docs.length-1];
console.log("last", lastVisible);
// Construct a new query starting at this document,
// get the next 20 customers.
var next = db.collection("customers")
.where('city', '==', 'city1')
.where('category', '==', 'category1')
.startAfter(lastVisible)
.limit("20");
});
推荐阅读
- python - 字典形式的 Python Django 验证错误消息
- r - 如何在R中创建年龄范围表
- php - 带有 PHP 的菜单活动项
- c++ - 有没有办法根据函数参数值动态更改 C++ 中函数的返回类型?
- html - 即使尝试了各种方法也无法垂直居中文本
- javascript - 如何在 razzle express 服务器文件中使用@sentry/node
- apache-spark - 如何根据特定组的计数删除火花数据集中的行
- vb.net - 带有很多或条件的 if 语句的替代方法是什么?
- logging - Fluentd 总是创建目录而不是日志文件
- ios - 用户强制退出应用程序时如何处理推送通知?[iOS]