Firestore + AngularFire2 pagination ( query items by range - .startAfter(lastVisible) )

后端 未结 3 436
借酒劲吻你
借酒劲吻你 2020-12-09 13:56

In a component I want to pull a range of items from FireStore, for ex. from 0 to 5, from 5 to 10 etc. I found this in FireStore\'s docs, but they dont use AngularFire2 so as

3条回答
  •  一向
    一向 (楼主)
    2020-12-09 14:09

    Source Tutorial Link

    AngularFire2 used for FireStore database operations

    using below methods:

    For Next Page

        nextPage() {
          this.disable_next = true;
          this.firestore.collection('People', ref => ref
            .limit(5)
            .orderBy('timestamp', 'desc')
            .startAfter(this.lastInResponse)
          ).get()
            .subscribe(response => {
    
              if (!response.docs.length) {
                this.disable_next = true;
                return;
              }
    
              this.firstInResponse = response.docs[0];
    
              this.lastInResponse = response.docs[response.docs.length - 1];
              this.tableData = [];
              for (let item of response.docs) {
                this.tableData.push(item.data());
              }
    
              this.pagination_clicked_count++;
    
              this.push_prev_startAt(this.firstInResponse);
    
              this.disable_next = false;
            }, error => {
              this.disable_next = false;
            });
        }
    

    For Previous Page

        prevPage() {
          this.disable_prev = true;
          this.firestore.collection('People', ref => ref
            .orderBy('timestamp', 'desc')
            .startAt(this.get_prev_startAt())
            .endBefore(this.firstInResponse)
            .limit(5)
          ).get()
            .subscribe(response => {
              this.firstInResponse = response.docs[0];
              this.lastInResponse = response.docs[response.docs.length - 1];
    
              this.tableData = [];
              for (let item of response.docs) {
                this.tableData.push(item.data());
              }
    
              //Maintaing page no.
              this.pagination_clicked_count--;
    
              //Pop not required value in array
              this.pop_prev_startAt(this.firstInResponse);
    
              //Enable buttons again
              this.disable_prev = false;
              this.disable_next = false;
            }, error => {
              this.disable_prev = false;
            });
        }
    

    Check complete tutorial and demo on this link

提交回复
热议问题