Utilizing a combination of Infinite Scroll, Isotope, and Firebase Firestore for seamless pagination functionality

Just starting out in the world of web development. Any help is appreciated!

I'm attempting to showcase items stored in Firestore by utilizing paginate queries. The aim is for each time a user reaches the bottom of the page, we trigger a call to Firestore, fetch more items, and display them.

I've come across Infinite Scroll () and Isotope () as potentially useful tools, but have been struggling to implement them successfully.

Currently, my code looks like this:

  var itemCollection = []
  recipeRef.get().then(function(querySnapshot) {
    var lastVisible = querySnapshot.docs[querySnapshot.docs.length - 1]
    querySnapshot.forEach(function(doc) {
      itemCollection.push(doc.data());
    })
    return res.render('user', {...})})
                  

I believe I need to create an event that retrieves more items from Firestore, adds them to the array, and displays them. However, I haven't found clear instructions on how to do this. Here's some sample code I discovered:

This example code appears to involve loading a preexisting page without querying a database, and then simply appending it as an object with Isotope. In my scenario, I'm working with an array and need to interact with Firestore, so I'm unsure about the approach. Any guidance would be greatly appreciated.

// Using Isotope & jQuery
// Initialize Isotope
var $grid = $('.grid').isotope({
  // Isotope options...
  itemSelector: '.grid__item', 
});

// Get Isotope instance
var iso = $grid.data('isotope');

// Initialize Infinite Scroll
$grid.infiniteScroll({
  // Infinite Scroll options...
  append: '.grid__item',
  outlayer: iso,
});

Answer №1

Using Firestore, you can utilize the orderBy and startAfter methods for pagination by keeping track of the last document retrieved.

let previousDoc = null
db.collection('collectionName').orderBy('orderingKey').limit(10).get()
.then(docs => {
    const data = docs.map(doc => doc.data());
    previousDoc = data[data.length - 1]
})

This allows you to paginate through the collection:

db.collection('collectionName').orderBy('orderingKey').startAfter(previousDoc)
.limit(10).get()
.then(docs => {
    const data = docs.map(doc => doc.data());
    previousDoc = data[data.length - 1]
})

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to include an ellipsis after a couple of lines of

Is there a way to add an ellipsis after two lines of text? I've been able to successfully implement the ellipsis in one line, but what if the text spans across two lines? And what if it's even longer than that — can we show an ellipsis after th ...

transfer data between JavaScript and PHP

I have set up a dynamic table structure in my project: <form method="POST" action="recieve.php"> <table> <thead> <th>Value</th> <th>Action</th> </thead> <tbody> <tr> ...

Ways to prompt the user to upload a file and integrate it into my program

Hello everyone, I need some help figuring out how to replace a JSON file with another JSON file that a user uploads. Here is my JavaScript code: var app = angular.module('miApp', []); app.controller('mainController', function ($scope ...

JavaScript: Unusual behavior discovered in forEach iteration

Here's the code snippet I'm having trouble with: someArray.forEach(x => { // do something console.log(‘calling api for ‘ + x); callAnHttpApiAsync(...); sleep(10); }); The issue lies in the asynchronous nature of the HTTP API call within ...

Learning the ins and outs of Node.js: Creating a client to connect to a Node.js server and receive broadcast messages

In implementing my nodeJS Server, everything seems to be running smoothly. However, now I am looking to create a client that can receive messages from the server and trigger specific JavaScript functions based on those messages. The process involves: Us ...

What techniques does Twitter use to insert labels into text boxes?

When I visited the Twitter login page, I noticed something interesting - the labels for input fields were inside the input fields themselves. It seemed to be accomplished using a common trick involving javascript/jquery. Intrigued, I delved into the Twitte ...

Using Mongoose to increment a sub-document key

I could really use some assistance with the following issue. I'm trying to increment the keys like 'eagle', 'bear', etc. by one in my data model: { "_id" : ObjectId("57e134097a578b11eruogf0a2cb"), "email" : "<a href="/cdn-cgi/l ...

Is it possible to invoke Bootstrap modal functions without using jQuery?

I'm in the process of removing jQuery dependencies from my app, but I still rely on Bootstrap. Is there a way to trigger modal functions like $('#myModal').modal('show') without using jQuery now? ...

Issues with running externally imported JavaScript code within an EJS file

I created an express Nodejs server and configured the static folder. The CSS is working fine on the page, but the JS is unable to locate any elements. Everything seems to be NULL. app.use(express.static(__dirname + '/static')) app.get('/ ...

Tips for locating the li element with the .multiselect class that comes before the one with the active

I have a unique situation where I am working with a list of select boxes. Let's say that the parent element of all the elements in the list is .multiselect, and when a user selects an option, the .active class is added to it. $(".multiselect-contai ...

Tips for incorporating PHP variables into Javascript code

Working with JavaScript: var counter = 1; var limit = 5; function addInput(divName){ if (counter == limit) { alert("You have reached the limit of adding " + counter + " inputs"); } else { var newdiv = document.createEle ...

Express.js fails to recognize the HTML meta viewport tag

I am currently facing an issue with the code I have written. I am testing it on Chrome 67 on Android 8.1 from two different sites. One site, served by Apache, has the following URL: The other site, served by express.js, has the following URL: Both sites ...

Is there a way to "stream" a specific part of my website to a separate window?

Is there a way to stream a specific container from my webpage to another window? The scenario is similar to a Point of Sale system, where there is an operator-facing display and a second customer-facing display. The operator-facing display will show all ...

What is the best way to transfer information from a component to the routing module in Angular version 16?

Currently, I have been developing a Single Page Application (SPA) using Angular 16, TypeScript, and integrating The Movie Database (TMDB). One of the components I've built displays movies based on genre: import { Component } from '@angular/core& ...

Enhancing the node module of a subpackage within Lerna: A step-by-step guide

I recently integrated lerna into my workflow to streamline the installation of all node modules for multiple sub packages with just one command. Currently, I'm only utilizing the lerna bootstrap feature. Here's a snippet from my lerna.json: { & ...

Tips on creating a responsive absolute div

I'm currently working on creating a profile component with Material UI and React.js. I'm having trouble making the Avatar/profile photo and profile name div responsive. Here are some screenshots to illustrate my issue: The specific div that need ...

What are the applications of client-side libraries within node.js?

Recently, I started exploring node.js and came across npm, which has proven to be quite useful. One thing that caught my attention is the fact that many libraries I have previously used in client-side JavaScript development, such as jquery and d3, can be ...

What is the method to define YAML array indices in JavaScript?

I apologize for my previous post. I have successfully integrated a RapidAPI for Covid-19 updates, which is now outputting the results as an array JSON in the console. You can view the YAML Array Output here. I am now facing a challenge in listing specifi ...

Tips for setting up Reaction Roles in discord.js?

Having some trouble implementing this functionality, especially with my reaction role. Wondering if I am using the correct functions/methods. Any help would be greatly appreciated. New to discord bot development and might have a simple question, but any a ...

Master the art of string slicing in JavaScript with these simple steps

I am attempting to utilize the slice function to remove the first three characters of a string within a JSON object. $(document).ready(function() { $.ajaxSetup({ cache: false }); setInterval(function() { $.getJSON("IOCounter.html", functio ...