Good day everyone!
I've been looking around the web on how to load more rows in the table on scroll down reached the bottom, but unfortunately, I only see divs tutorial. Can anyone please enlighten me how to execute this? or a sample code or tutorial?
It's like this, I will run a SELECT query and LIMIT the showing to 500 records only in the html table, but when the user scrolls down to the bottom of the table, the record will load another 500 records on the table. Is this possible?
Good day everyone!
I've been looking around the web on how to load more rows in the table on scroll down reached the bottom, but unfortunately, I only see divs tutorial. Can anyone please enlighten me how to execute this? or a sample code or tutorial?
It's like this, I will run a SELECT query and LIMIT the showing to 500 records only in the html table, but when the user scrolls down to the bottom of the table, the record will load another 500 records on the table. Is this possible?
Share Improve this question asked Mar 13, 2018 at 4:10 DarwinDarwin 451 silver badge6 bronze badges 1-
1
what you ask is not trivial... Not to do it right, which includes cashing some of the results.. And yes its possible it's called
append
to the table, the difference between a div and this is minor. – ArtisticPhoenix Commented Mar 13, 2018 at 4:15
2 Answers
Reset to default 4
$('#container').on('scroll', function() {
if($(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight) {
alert('end reached');
}
});
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<style type="text/css">
#container{
overflow: scroll;
max-height: 500px;
border: 2px solid;
}
</style>
</head>
<body>
<div id="container">
<table>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
<tr>
<td>hi</td>
<td>hello</td>
</tr>
</table>
</div>
</body>
</html>
The code above will detect when the scroll will reach the end of the table. now you can replace alert()
with an $.post()
or $.ajax()
to fetch more results from database and append
the response in the table
$(document).on('click','#load',function(){
$('#loading').append('<tr><td>3:1</td><td>3:2</td><td>3:3</td></tr>');
})
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="loading">
<tr><td>1:1</td><td>1:2</td><td>1:3</td></tr>
<tr><td>2:1</td><td>2:2</td><td>2:3</td></tr>
</table>
<input type="button" value="click to append" id="load"/>
Zainul described about Creating an event handler.
This answer about appending to table.
Merge this two answers.