Reputation: 45
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?
Upvotes: 4
Views: 8098
Reputation: 1540
$(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.com/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.
Upvotes: 2
Reputation: 1900
$('#container').on('scroll', function() {
if($(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight) {
alert('end reached');
}
});
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/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
Upvotes: 4