I see only two options: AJAX, and client side filtering. I think it’s easiest to do the filtering on the client side, but depending on how many polls you expect to have, it could mean transmitting and storing a lot of data. Doing this over AJAX is definitely not difficult, though exactly how you accomplish it will depend on which DBMS you’re using (I’ll assume MongoDB, because this is FCC, but if you’re also using Mongoose it shouldn’t be much different).
Conceptually, a “page” of polls is just X number of items from your database. Let’s set X to be 10. Imagine you have all of your data in an array and you want to get the first page’s worth of data. This is straightforward - all you need to do is get items 0 - 9 (because we’re using zero-based indices). It’s easy to imagine a function which does this with an array:
function getFirstTen() {
return arr.slice(0, 10);
}
That’s easy enough, but now we want to generalize this so that we can return an arbitrary range of data (20, 25, or 100 instead of just 10) and we want to be able to start from anywhere (this is how we do that pagination).
function getXResults(start, limit) {
return arr.slice(start, start + limit);
}
Now, if you simply send an array of every single poll to every client, this one function is all you’d need to do pagination on the client-side. But this is possible, maybe preferable, to do over AJAX. Now, MongoDB is of course much different from just manipulating an array, but the idea is the same. We want to pass in the starting index and have some optional limit that tells Mongo how many records we want. This is really easy to do with the cursor methods skip() and limit(). You’ll want a function sort of like this:
function getXPopularResults(start,limit) {
// use sort() to get a descending order based on the number of responses to the poll
return db.polls.find().sort({ responses: -1 }).skip(start).limit(limit).toArray();
}
You won’t be able to drop this code into your project and have it work, but I think you’ll get the idea. If you want, you can create a convenience function to return the top 10 most popular:
function get10MostPopular() {
return getXPopularResults(0, 10);
}
If you’re using Mongoose, the concept is the same, but I don’t know how to do skip
and limit
off the top of my head (easy to find, I’m just lazy). As a side note, if you do a search on the topic of pagination in MongoDB, you’ll see a prominent StackOverflow answer which says not to use skip
because it doesn’t scale well. You can ignore this for now since you’re probably not going to have millions of polls in your DB, but it’s an important point to keep in mind.