3
I am working on a script that makes it load more records from the database when the user arrives at the end of the page scrolling.
PHP/SQL is right, the problem is that jQuery does not correctly report the number of completed occurrences class
onscreen.
I imagine the solution is to implement the .on
somehow, but I don’t know much about jQuery and I’m hitting myself with it.
I explain my code below:
$(document).ready(function() {
var posicaoAtual = $(window).scrollTop();
var documentSize = $(document).height();
var sizeWindow = $(window).height();
$(window).scroll(function() {
posicaoAtual = $(window).scrollTop();
//Até aqui, apenas verifico se o usuário chegou ao final da página.
if (posicaoAtual >= (documentSize - sizeWindow)) {
//Agora que começa o sofrimento
// Conta a quantidade de vezes que a section.empresa aparece na tela
var qtdRegistros = $("#empresas section.empresa").length;
//Variável que utilizo no meu PHP
var idClientes = $("#idClientes").val();
// Faz requisição ajax
$.post("carrega-registros.php", {
qtdRegistros: qtdRegistros,
idClientes: idClientes
}, function(resposta) {
// Coloca o conteudo na DIV
$("#empresas").append(resposta);
});
}
});
$(window).resize(function() {
posicaoAtual = $(window).scrollTop();
documentSize = $(document).height();
sizeWindow = $(window).height();
});
});
[EDIT 1]
The whole code works right. My return are new section class='empresa'
within the div id="empresas"
.
BUT BY var qtdRegistros = $("#empresas section.empresa").length;
, the variable qtdRegistros
only identifies the number of times the class
empresa
in the original HTML. Disregarding what comes from append
. This makes my PHP bring duplicate results, since the qtdRegistros
is always the same.
I need to update the length
according to the new class
who comes in the append
. But that doesn’t happen.
Researching a lot I found something similar implementing the on
, but I just couldn’t use it. Someone can show me a way?
You can ask the question the content of
resposta
?– Sergio
Hi Sergio! The script has no errors. What happens is that the same PHP pro value of the qtdRegistros variable always goes. So instead of me bringing in new records in the append, they always come back the same.
– Aryana Valcanaia
@Aryanavalcanaia the return of the
post
is ahtml
?– RFL
That’s right :) The return is an append of a snippet like this "<Section class="company" id="11216"> (...) </Section>" in div #companies.
– Aryana Valcanaia
What seems to happen is that you are taking the same element and the
append
does not update the set of elements within it, but ensemble of his children. Try$("#empresas").children().length
and see if it solves your problem...– Felipe Avelar
I found a solution but for the version
1.10
jquery, what version Voce uses?– RFL
@Rafaelacioly v2.1.1. But I believe I have solved with Felipe’s solution :) soon return here. Thank you very much!
– Aryana Valcanaia
@Felipeavelar I believe it worked! It seems that my function to check if the user has reached the end of the page is that it is a bit buggy now :( I will do some tests and confirm if it is the solution! Thank you.
– Aryana Valcanaia
Hi Aryana. From what I understand you want the exact amount of elements before doing Ajax, that’s it?
– Sam