A
A
aumarov1112014-06-24 15:39:48
MySQL
aumarov111, 2014-06-24 15:39:48

How to make a request when implementing the scroll-to-load functionality?

Hello. I'm trying to make a functional, scroll-load. Well, I'll write a script, in principle there is nothing difficult, we track the scrolling to the bottom and send an Ajax request. But how to query the database? Let's say we already have the first 10 items on the page, I need to load the next 10, if I send a simple request, it will insert the same 10

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Ilya Lesnykh, 2014-06-24
@aumarov111

This approach, as suggested by @IgorO2 , will be a bottleneck in your system if it suddenly becomes highly loaded. It is much better to send not an offset, but the last record ID and build a query like this:
Depending on the sort order, the sign will need to be reversed.
Well, it is natural that in systems that respect themselves and their clients, we received only JSON from the server, and we will do all the templating on the client (we implement MVC, for example, through the backbone).

1
1001001 111, 2014-06-24
@IgorO2

Well, keep a record count and, depending on the number of records, make a request with
LIMIT count, 10

Y
Yakov Akulov, 2014-06-24
@jakulov

There are many implementations, it can be conditionally divided into 2:
1. A client-side template engine (js) is used, then you get json like:

{
items: [..],
offset: 10
}

where offset comes in the response, and shows the last request used in offset, respectively, in js we will send a get-request offset + 10.
2. The templating takes place on the server and in ajax we get the finished html.
then you can stupidly count the number of downloaded pages. If the message looks like this:
<div id="content">
  <div class="page">...</div>
  <div class="page">...</div>
  ...
</div>

Those. each loaded portion of items is framed in a div with the page class and inserted into #content.
Here you can stupidly count the number of pages: $('#content').find('.page').length. And send this number in the get request as a pointer to the page to be loaded next. Those. it turns out the same as with the usual pagination

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question