簡體   English   中英

如何使用 Firebase 對過濾后的結果進行分頁?

[英]How to paginate filtered results with 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");

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM