简体   繁体   English

jQuery Infinite Scroll和Gridview

[英]jQuery Infinite Scroll and Gridview

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. 假设我在数据库中有10,000条记录,但我想通过gridview在页面中显示100条记录,我希望当用户向下滚动并到达页面中的最后一条记录时,100条记录的其余部分将通过jquery加载到gridview中。 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. 1)当我在页面加载时显示100条记录时,如何检测用户到达最后一条记录。

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. 2)如果我能检测到那么我可以启动JQuery ajax调用来获取下一个100记录并在底部gridview再次追加新的100条记录。 so how i can assign data or append data into gridview by jquery. 所以我如何通过jquery分配数据或将数据附加到gridview中。

please discuss in detail...sample code will be more helpful. 请详细讨论......示例代码会更有帮助。 thanks 谢谢

I have done it this way with MVC 2 and jQuery: 我用MVC 2和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>");
                        }                     
                    }
                });
            }
        }

    }
}

I am guessing you have the basics of jquery and then this is what you could do ... 我猜你有jquery的基础知识然后这就是你能做的......

var h = $('body,html').height();// gives u the height of the document .

var s = $('body,html').scrollTop(); // gives you the length the document has been scrolled,

//so 

if(s> (h-40)){//40 px tolerance 
 //do ajax here to load the it on the fly, then dump them at the bottom,
}

You could use jQuery to detect how far the user has scrolled, and compare that to the bottom of the div containing your 100 records. 您可以使用jQuery来检测用户滚动的距离,并将其与包含100条记录的div的底部进行比较。 Then fetch the next 100 rows from the DB. 然后从DB中获取接下来的100行。

How can you use jQuery measure how far down the user has scrolled? 如何使用jQuery测量用户滚动的距离?

Alternatively, you could prefetch all 10,000 records and use jQuery to divide them into groups of 100 rows. 或者,您可以预取所有10,000条记录并使用jQuery将它们分成100行的组。 This would allow the user to instantly see the next 100 items without have to wait for the data to return. 这将允许用户立即查看接下来的100个项目,而无需等待数据返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM