最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Load more table rows on scroll down - Stack Overflow

programmeradmin3浏览0评论

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
Add a ment  | 

2 Answers 2

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.

发布评论

评论列表(0)

  1. 暂无评论