我尝试在HTML表格上实现无限滚动,或者说是“滚动加载”。
数据存储在数据库中,我通过代码后台访问它。
我按照MSDN上的示例实现了它,如下:
JS
$(document).ready(function () {
function lastRowFunc() {
$('#divDataLoader').html('<img src="images/ajax-Loader.gif">');
//send a query to server side to present new content
$.ajax({
type: "POST",
url: "updates.aspx/GetRows",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
if (data != "") {
$('.divLoadedData:last').before(data.d);
}
$('#divDataLoader').empty();
}
})
};
//When scroll down, the scroller is at the bottom with the function below and fire the lastRowFunc function
$(window).scroll(function () {
if ($(window).scrollTop() == $(document).height() - $(window).height()) {
lastRowFunc();
}
});
// Call to fill the first items
lastRowFunc();
});
代码后台并不是很有趣,它只是以这种格式(每行20个)从数据库返回数据(每行一个):
<tr><td>Cell 1 data</td><td>Cell 2 data</td><td>Cell 3 data</td></tr>
ASPX
<table>
<thead>
<tr><th>Header 1</th><th>Header 2</th><th>Header 3</th></tr>
</thead>
<tbody>
<div class="divLoadedData">
</div>
</tbody>
</table>
<div id="divDataLoader">
</div>
问题是,当数据被加载并插入到页面上(即使在第一次加载时),表头也会跟随数据。我看到了我加载的所有行,但表头在页面底部(在我加载的20行后)。 我尝试了几种方法来插入加载的数据:
$('.divLoadedData:last').before(data.d);
或者
$('.divLoadedData:last').append(data.d);
或者
$('.divLoadedData:last').after(data.d);
但是它们都不起作用。 非常希望能听到有关如何使用HTML表格正确实现并使其工作的任何建议。