suppose i have 10,000 records in database but i want to show 100 record in the page through gridview and i want when user scroll down and reach the last record in the page then rest of the 100 record will load in the gridview through jquery. in this way data will load when user scroll down. so i have some question in my mind like.
1) how to detect that user reach at the last record when i am showing 100 record when page loads.
2) if i could detect then i can initiate JQuery ajax call to fetch next 100 record and append the new 100 records again at the bottom gridview. so how i can assign data or append data into gridview by jquery.
please discuss in detail...sample code will be more helpful. thanks
I have done it this way with MVC 2 and jQuery:
Controller:
/// <summary>
/// GET: /Widget/Search/
/// Displays search results.
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public ActionResult Search(SearchType searchType, string s, [DefaultValue(1)]int page)
{
try
{
int batch = 20;
int fromRecord = 1;
int toRecord = batch;
if(page != 1)
{
toRecord = (batch * page);
fromRecord = (toRecord - (batch-1));
}
var widgets= _repos.Search(searchType, s, fromRecord, toRecord );
if (widgets.Count == 0)
{
InfoMsg("No widgets were found.");
}
if (Request.IsAjaxRequest())
{
if(widgets.Count > 0)
{
return View("SearchResultsLineItems", widgets);
}
else
{
return new ContentResult
{
ContentType = "text/html",
Content = "noresults",
ContentEncoding = System.Text.Encoding.UTF8
};
}
}
return View("SearchResults", widgets);
}
catch (Exception ex)
{
return HandleError(ex);
}
}
View:
<% if (Model.Count > 0) { %>
<table id="tblSearchResults">
<tr>
<th></th>
<th>Col1</th>
<th>Col2</th>
<th>Col3</th>
<th>Col4</th>
<th>Col5</th>
<th>Col6</th>
</tr>
<% Html.RenderPartial("SearchResultsLineItems", Model); %>
</table>
<div id="loadingSearchResults" style="text-align:center;height:24px;"></div>
<div id="actionModal" class="modal"></div>
<% } %>
Script:
function initAutoPaging() {
$(window).scroll(function () {
if ($(window).scrollTop() == $(document).height() - $(window).height()) {
loadMore()
}
});
}
var current = 1;
function loadMore() {
if (current > -1) {
if (!_isShowingDetails)
{
if (!$('#loadingSearchResults').html()) {
current++;
$('#loadingSearchResults').show();
$('#loadingSearchResults').html("<img src='/content/images/loading.gif' />");
$.ajax({
async: true,
url: document.URL + "?&page=" + current,
contentType: "application/x-www-form-urlencoded",
dataType: "text",
success: function(data) {
if (data != 'noresults') {
$('#tblSearchResults tr:last').after(data);
$('#loadingSearchResults').hide();
$('#loadingSearchResults').html('');
highlightSearch();
} else {
current = -1;
$('#loadingSearchResults').show();
$('#loadingSearchResults').html("<h3><i>-- No more results -- </i></h3>");
}
}
});
}
}
}
}
look here : jQuery: detecting reaching bottom of scroll doesn't work, only detects the top
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With