自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

jQuery 插件-使用jQuery Pagination實(shí)現(xiàn)無(wú)刷新分頁(yè)

開發(fā) 前端
今天,我們主要看看簡(jiǎn)單的使用jQuery pagination這個(gè)插件來(lái)實(shí)現(xiàn)無(wú)刷新分頁(yè),使用jQuery pagination主要作用在于分頁(yè)樣式的顯示,而無(wú)刷新的主要原理還是使用Ajax。下邊我們一步一步看看使用jQuery、JSON、Ajax和微 軟jQuery Template插件組合實(shí)現(xiàn)jQuery 無(wú)刷新分頁(yè),希望能給你一些幫助。

今天,我們主要看看簡(jiǎn)單的使用jQuery pagination這個(gè)插件來(lái)實(shí)現(xiàn)無(wú)刷新分頁(yè),使用jQuery pagination主要作用在于分頁(yè)樣式的顯示,而無(wú)刷新的主要原理還是使用Ajax。下邊我們一步一步看看使用jQuery、JSON、Ajax和微 軟jQuery Template插件組合實(shí)現(xiàn)jQuery 無(wú)刷新分頁(yè),希望能給你一些幫助。

首先,我們引入jQuery文件、jQuery pagination文件和jQuery Templates文件,很多人可能還對(duì)jQuery Templates不太熟悉,你可以看看jQuery插件-微軟 jQuery Templates微軟 jQuery Templates插件的使用這兩篇文章。

  1. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script> 
  2. <script src="http://www.jquery001.com/js/jquery.tmpl.js" type="text/javascript"></script> 
  3. <script src="http://www.jquery001.com/js/jquery.pagination.js" type="text/javascript"></script> 

接下來(lái),先看看HTML標(biāo)記,分別用來(lái)呈現(xiàn)數(shù)據(jù)項(xiàng)和分頁(yè)樣式顯示,如下:

  1. <!--顯示列表--> 
  2. <div id="content-left"></div> 
  3. <!--分頁(yè)樣式顯示--> 
  4. <div id="Pagination" class="pagination"></div> 
  5. <!--jQuery Templates--> 
  6. <script id="Template" type="text/html"> 
  7. <div class="item"> 
  8.     <h3><a href="${Url}" target="_blank">${Title}</a></h3> 
  9.     <p>${Subject}</p> 
  10. </div> 
  11. </script> 

下邊就需要取得我們所需要的數(shù)據(jù)了,如果你看過(guò)前兩篇jQuery Templates的文章,你就會(huì)意識(shí)到我們需要返回javascript對(duì)象來(lái)進(jìn)行數(shù)據(jù)的綁定,這里我們還是返回JSON。主要還是用拼串的形式最終返回如下JSON結(jié)構(gòu):

  1. var req={ 
  2.         "articlelist": 
  3.         [ 
  4.             {"Title":"文章標(biāo)題1","Url":"文章Url1","Subject":"文章概要1"}, 
  5.             {"Title":"文章標(biāo)題2","Url":"文章Url2","Subject":"文章概要2"}, 
  6.             {"Title":"文章標(biāo)題3","Url":"文章Url3","Subject":"文章概要3"} 
  7.         ] 
  8.     }; 

為了方便很多新學(xué)習(xí)JSON的朋友,在這里給出點(diǎn)后臺(tái)輸出JSON的C#代碼,如下:

  1. if (dt != null && dt.Rows.Count > 0) 
  2.     StringBuilder strResult = new StringBuilder(); 
  3.     strResult.Append("{\"articlelist\":["); 
  4.     foreach (DataRow dr in dt.Rows) 
  5.     { 
  6.         strResult.Append("{\"Title\":\"" + dr["Title"].ToString() + "\","); 
  7.         strResult.Append("\"Url\":\"" + dr["Url"].ToString() + "\","); 
  8.         strResult.Append("\"Subject\":\"" + dr["Subject"].ToString() + "\"},"); 
  9.     } 
  10.     //移除末尾',' 
  11.     strResult.Remove(strResult.Length - 1, 1); 
  12.     strResult.Append("]}"); 
  13.     //輸出json 
  14.     Response.Write(strResult.ToString()); 
  15.     Response.End(); 

關(guān)于JSON本站中已經(jīng)有很多次提到了,如果有不了解的可以找找。這樣當(dāng)我們使用"articlelist"時(shí),它就相當(dāng)于一個(gè)對(duì)象數(shù)組,使得我們很容易的能綁定數(shù)據(jù)。下邊是前臺(tái)代碼:

  1. $(document).ready(function () { 
  2.     //TotalNum 總數(shù) 這里寫死了 
  3.     var TotalNum = 200
  4.     //首次加載 
  5.     pageselectCallback(0); 
  6.     //分頁(yè)事件 
  7.     $("#Pagination").pagination(200, { 
  8.         prev_text: "上一頁(yè)", 
  9.         next_text: "下一頁(yè)", 
  10.         num_edge_entries: 2, 
  11.         num_display_entries: 8, 
  12.         //回調(diào) 
  13.         callback: pageselectCallback 
  14.     }); 
  15.  
  16.     function pageselectCallback(page) { 
  17.         var result = ""
  18.         $.ajax({ 
  19.             type: "post", 
  20.             dataType: "json", 
  21.             url: "getdata.aspx", //請(qǐng)求的url 
  22.             data: { "page": parseInt(page + 1) }, 
  23.             success: function (req) { 
  24.                 //使用微軟jQuery Templates綁定數(shù)據(jù)列表,實(shí)現(xiàn)了HTML與js分離,使得頁(yè)面整潔 
  25.                 $("#content-left").html($("#Template").render(req.articlelist)); 
  26.             } 
  27.         }); 
  28.     } 
  29. }); 

這樣,我們就使用jQuery pagination實(shí)現(xiàn)了無(wú)刷新分頁(yè),文中并沒有過(guò)多的介紹jQuery pagination,而是將側(cè)重點(diǎn)置于jQuery Templates的使用。因?yàn)楸救擞X得jQuery pagination還是很容易使用的,最終的效果可在Demo中看看。

原文鏈接:http://www.jquery001.com/use-jquery-pagination-to-fulfill-pagination.html

責(zé)任編輯:陳四芳 來(lái)源: jquery001.com
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)