2011-02-25 14 views
17

मान लीजिए कि मेरे पास डेटाबेस में 10,000 रिकॉर्ड हैं लेकिन मैं ग्रिडव्यू के माध्यम से पृष्ठ में 100 रिकॉर्ड दिखाना चाहता हूं और मैं चाहता हूं कि जब उपयोगकर्ता स्क्रॉल करें और पृष्ठ में अंतिम रिकॉर्ड तक पहुंच जाए तो बाकी 100 रिकॉर्ड लोड होंगे jquery के माध्यम से ग्रिडव्यू में। इस तरह उपयोगकर्ता स्क्रॉल करते समय डेटा लोड हो जाएगा। तो मेरे मन में कुछ सवाल है जैसे।jQuery अनंत स्क्रॉल और ग्रिडव्यू

1) उस उपयोगकर्ता को अंतिम रिकॉर्ड पर कैसे पहुंचाया जाए जब पेज लोड होने पर मैं 100 रिकॉर्ड दिखा रहा हूं।

2) अगर मैं पता लगा सकता हूं तो मैं अगले 100 रिकॉर्ड लाने के लिए JQuery AJAX कॉल शुरू कर सकता हूं और नीचे 100 ग्रिडव्यू पर नए 100 रिकॉर्ड जोड़ सकता हूं। तो मैं jquery द्वारा ग्रिडव्यू में डेटा कैसे निर्दिष्ट कर सकता हूं या डेटा जोड़ सकता हूं।

कृपया विस्तार से चर्चा करें ... नमूना कोड अधिक उपयोगी होगा। धन्यवाद

उत्तर

12

मैं इसे MVC 2 और jQuery के साथ इस तरह से किया है:

नियंत्रक:

/// <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); 
    } 
} 

दृश्य:

<% 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> 
    <% } %> 

स्क्रिप्ट:

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>"); 
         }      
        } 
       }); 
      } 
     } 

    } 
} 
1

मैं अनुमान लगा रहा हूँ आप 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, 
} 
1

आप jQuery इस्तेमाल कर सकते हैं कितनी दूर उपयोगकर्ता पता लगाने के लिए स्क्रॉल किया गया है, और अपने 100 रिकॉर्ड वाले div के निचले भाग की तुलना करें। फिर डीबी से अगली 100 पंक्तियां प्राप्त करें।

How can you use jQuery measure how far down the user has scrolled?

वैकल्पिक रूप से, आप सभी 10,000 रिकॉर्ड prefetch और jQuery का उपयोग उन्हें 100 पंक्तियों का समूहों में विभाजित करने के लिए कर सकता है। यह उपयोगकर्ता को अगले 100 आइटम तुरंत डेटा को वापस आने के लिए इंतजार किए बिना देखने की अनुमति देगा।

संबंधित मुद्दे