Load data on page scroll using jquery

preview_player
Показать описание
Link for all dot net and sql server video tutorial playlists

Link for slides, code samples and text version of the video

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

In this video we will discuss, how to load more data on page scroll using jQuery AJAX.

This is similar to Facebook. As you scroll down on the page more data will be loaded.

When the page is initially loaded we want to retrieve and display the first 50 rows from the database table tblEmployee. As we scroll down and when we hit the bottom of the page we want to load the next set of 50 rows.

Stored procedure

Create procedure spGetEmployees
@PageNumber int,
@PageSize int
as
Begin

Declare @StartRow int
Declare @EndRow int

Set @EndRow = @PageNumber * @PageSize;

WITH RESULT AS
(
SELECT Id, Name, Gender, Salary,
ROW_NUMBER() OVER (ORDER BY ID ASC) AS ROWNUMBER
From tblEmployee
)
SELECT *
FROM RESULT
WHERE ROWNUMBER BETWEEN @StartRow AND @EndRow
End

HTML page

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
$(document).ready(function () {
var currentPage = 1;
loadPageData(currentPage);
$(window).scroll(function () {
if ($(window).scrollTop() == $(document).height() - $(window).height()) {
currentPage += 1;
loadPageData(currentPage);
}
});

function loadPageData(currentPageNumber) {
$.ajax({
method: 'post',
dataType: "json",
data: { pageNumber: currentPageNumber, pageSize: 50 },
success: function (data) {
var employeeTable = $('#tblEmployee tbody');

$(data).each(function (index, emp) {
+ emp.Name + '</td><td>' + emp.Gender
+ '</td><td>' + emp.Salary + '</td></tr>');
});
},
error: function (err) {
alert(err);
}
});
}
});
</script>
</head>
<body style="font-family:Arial">
<h1>The data will be loaded on demand as you scroll down the page</h1>
<table id="tblEmployee" border="1" style="border-collapse:collapse; font-size:xx-large">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Gender</th>
<th>Salary</th>
</tr>
</thead>
<tbody></tbody>
</table>
</body>
</html>
Рекомендации по теме
Комментарии
Автор

Hi Venkat, I am very grateful to you for your tutorials. I didn't find any other tutorial useful like these. But I am unable to add the scroll load into my project as per your above tutorial because in your example you used table format. But in my project I have used repeater. The code is here :
<asp:Panel ID="pnlProduct" runat="server" Visible="false">
<div id="view-type" class="view-type">
<!-- PRODUCT LIST -->
<ul class="row product-list grid">
<%--
<asp:Repeater ID="rptrTopSeller" runat="server">
<ItemTemplate>
<li class="col-xs-6 col-sm-4 col-md-4 col-mobile-12">
<div class="product-container">
<div class="left-block">
<a
<asp:Image ID="ImgProduct" runat="server" CssClass="img-responsive" alt="product" Height="285px" Width="350px" ImageUrl='<%# Eval("ProductImage")%>' />
</a>
<div class="action">
<a title="Add to my wishlist" class="heart fa fa-heart" href="#"></a>
<a title="Add to compare" class="compare fa fa-retweet" href="#"></a>
<asp:button id="Button2" runat="server" text="Submit" xmlns:asp="#unknown" onclick="btnSubmit_Click" style="display: none" />
<a title="Quick view" class="search quickview fa fa-arrows" data-toggle="modal" href=""></a>
</div>
<div class="add-to-cart">
<a class="">
<asp:Button ID="btnAddtoCart" runat="server" Text="Add to Cart" OnClick="btnAddtoCart_Click" /></a>
<asp:HiddenField ID="HiddenField1" runat="server" />
</div>
<!-- <div class="swatch">
<span class="color current"></span>
<span class="color"></span>
<span class="color"></span>
<span class="color"></span>
</div> -->
<!-- <div class="group-price">
<span
<span
</div> -->
</div>
<div class="right-block">
<div class="left-p-info">
<h5 class="product-name"><a
<asp:LinkButton ID="lbProductName" runat="server" Text='<%#
<div class="product-star">
<i class="fa fa-star-o"></i>
<i class="fa fa-star-o"></i>
<i class="fa fa-star-o"></i>
<i class="fa fa-star-o"></i>
<i class="fa fa-star-o"></i>
</div>
</div>
<div class="content_price">
<span style="display:none " class="price old-price">
<asp:DropDownList ID="DropDownList1" Visible="true"
<%--<asp:Label ID="lblSize" runat="server" Text='<%# Eval("SizeName") ID="lblAvailableStock" runat="server" Visible="true" Font-Bold="True" ForeColor="#FF0066" Text="" ToolTip="Available Stock"></asp:Label></span>
<span class="price ID="lblPrice" runat="server" ID="hfProductID" runat="server" />
</span>
</div>
<div class="action">
<a title="Add to my wishlist" class="heart fa fa-heart" href="#"></a>
<a title="Add to compare" class="compare fa fa-retweet" href="#"></a>
<asp:button id="btnSubmit" runat="server" text="Submit" xmlns:asp="#unknown" onclick="btnSubmit_Click" style="display: none" />
<a title="Quick view" class="search quickview fa fa-arrows" data-toggle="modal" href=""></a>

</div>
<div class="description">
<p>
<asp:LinkButton ID="LinkButton1" runat="server" Text='<%#
</div>
</div>
</div>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
<!-- ./PRODUCT LIST -->
</div>

<%-- <div class="tool-page-bottom tool-page clearfix">
<div class="tool-left">
<div class="paginator">
<ul>
<li class="current"><a href="">1</a></li>
<li><a href="">2</a></li>
<li><a href=""></a></li>
</ul>
</div>
</div>
<div class="tool-right">
<div class="show-item"><label>Show 12/24 result</label>
</div>
</div>
</div>--%>
</asp:Panel>
How do add the json data with this repeater using jqery on success. Please suggest me . Thanks again.

banglatiger
Автор

can you show us how to integrate jquery in SSRS reports? I need to do this page scroll effect in an SSRS report. Thank you.

lazykitten
Автор

And, look at the code...It's pretty straight forward code. :)
Sir...your tutorial videos are insanely good.

neerajsaxena
Автор

Another excellent session! Here's a script to populate the SQL table with data for the next person.
DECLARE @RecCount AS INT = 500,
@i AS INT = 0,
@iChars AS VARCHAR(10)
WHILE @i < @RecCount
BEGIN
SET @i += 1
INSERT INTO tblEmployeeJQ70 (Name, Gender, Salary)
VALUES (CONCAT('Name ', @iChars), CONCAT('Male ', @iChars), @i * 100)
END

Thank you!

CodingSquid
Автор

Sir, Thank you very much for your clearly and simply explained videos. very cleverly organised. I like it!

alikemalkulful
Автор

Hi Venkat, it's really nice to watch all of your vides. Anybody can learn from your tutorial so easily. Really awesome videos. It will be really great help to all the beginners if you can come up with Angularjs tutorial as well. I am sure many folks are eagerly waiting for you to come up with Angularjs tutorial including me. Everybody is expecting some kind of tutorial from you and I am one of them. Hopefully you will do it very soon. Thanks in advance :)

bjdong
Автор

this was pretty sweet.  I did this with a button click but was wondering how might one implement this with a "previous" button.  Next was pretty straight forward

troybryantIII
Автор

Hi Venkat, The Tutorials are very helpful.Can you please create a Playlist on Design Patterns in C#

ranjithc
Автор

Hi Venkat,

Thanks for changing my life..I am able to get a job bcz of your efforts..

I was asked one question in an interview:
How to access/populate a HTML textbox from code-behind in ASP.NET?
Can you Please help,

I believe you ll make another small tutorial series like ASP.NET With JQUERY as you had made "Javascript with ASP.NET" to explain more usage of Jquery in real time ASP.NET projects.?

May God Ram Bless you and your loved ones!!

gardyroy
Автор

Appreciate your videos Sir. They are incredible

hanimv
Автор

Your videos are awesome as always. Thank you so much.

khoinguyenpham
Автор

How about for Webform? do you have video tutorial for that? thank you very much, i learned a lot from your videos.

vjaybuzarang
Автор

test stored procedure, nice technique, have to learn that -- fast testing after creation

got indian accent -- but not heavy, plan to watch more of your youtube channel to learn more stuff in VS

I think this is a good video -- with all the thing in your blog -- we hate typing !!

woodadmin-
Автор

u are great sir.. your all video's are superb... would you pls help me...i have done this example .. its working fine but now i want to attach this data in repeater..how to do that.. please help me sir..

shailesh
Автор

In data: option somewhere you have used json object and somewhere in previous videos used JSON.stringify ...why ?

dineshyd
Автор

Can't we do it without stored procedure ???

georgemasih
Автор

Hi, how can i use this with asp.repeater

ReklamVizyon
Автор

Hi teacher it was good on web but on mobile view is not working, could you make videos for mobile. thank best regards

dosokho
Автор

sir.. how to bind this data to datalist instead of table?

vishalgaware
Автор

how to make the trigger change to scrollY ?

mohammadhanifahnurshafrudi