DIV CSS 佈局教程網

jquery.pagination.js 無刷新分頁實作步驟完整分享
編輯:JQuery特效代碼     

無刷新分頁能大幅提升使用者瀏覽列表資料的體驗,無需重新載入整個頁面即可切換頁碼。本文以 jquery.pagination.js 插件為例,完整分享從前端到後端的實作步驟。

1. 引用插件

首先引入分頁用的 jquery.pagination.js 與樣式檔 pagination.css。

2. 頁面 JS 程式碼

<script type="text/javascript">
var pageIndex = 0; //頁面索引初始值
var pageSize = 15; //每頁顯示條數初始化,修改顯示條數,修改這裡即可
$(function () {
InitTable(0); //Load事件,初始化表格數據,頁面索引為0(第一頁)
//分頁,PageCount是總條目數,這是必選參數,其它參數都是可選
$("#Pagination").pagination(<%=pcount%>, {
callback: PageCallback, //PageCallback() 為翻頁調用次函數。
prev_text: "&laquo; 上一頁",
next_text: "下一頁 &raquo;",
items_per_page:pageSize,
num_edge_entries: 2, //兩側首尾分頁條目數
num_display_entries: 6, //連續分頁主體部分分頁條目數
current_page: pageIndex, //當前頁索引
});
//翻頁調用
function PageCallback(index, jq) {
InitTable(index);
}
//請求數據
function InitTable(pageIndex) {
$.ajax({
type: "POST",
dataType: "text",
url: 'https://www.jb51.net/tool/Reserver/ManageBuyBatchManage.ashx',
data: "pageIndex=" + (pageIndex) + "&pageSize=" + pageSize,
success: function(data) {
$("#Result tr:gt(0)").remove(); //移除Id為Result的表格裡的行,從第二行開始
$("#Result").append(data); //將返回的數據追加到表格
}
});
}
});
</script>

3. 頁面 body 程式碼

<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="60" align="right">商品名:</td>
<td width="200" align="left"><input type="text" id="txtKeywords" class="keyword" /></td>
<td width="200" align="left"><input id="search" type="button" value=" 查 找 " class="submit" /></td>
<td > </td>
</tr>
</table>
<table id="Result" cellspacing="0" cellpadding="0">
<tr>
<th>商品編號</th>
<th>商品名稱</th>
</tr>
</table>
<div id="Pagination" class="right flickr"></div>

4. 後台程式碼

protected int pcount = 0; //總條數
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BLL.TbGoods bll = new BLL.TbGoods();
pcount = bll.GetRecordCount("Status='" + (int)Enum.RecordStatus.Normal + "'"); //獲取頁面總條數
}
}

5. 一般處理程序 ashx 程式碼

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Data;
namespace EShop.Web.Admin.tool.Reserver
{
public class ListBuyBatchManage : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
String str = string.Empty;
if (context.Request["pageIndex"] != null && context.Request["pageIndex"].ToString().Length > 0)
{
int pageIndex;
int.TryParse(context.Request["pageIndex"], out pageIndex);
if(context.Request["pageSize"]!=null&&context.Request["pageSize"].ToString().Length > 0)
{
int size = Convert.ToInt32(context.Request["pageSize"]);
string data= BindSource(size,pageIndex);
context.Response.Write(data);
context.Response.End();
}
}
}
#region 無刷新分頁
public string BindSource(int pagesize,int page)
{
BLL.TbGoods bll=new BLL.TbGoods();
DataSet ds = bll.GetListByPage("Status='" + (int)Enum.RecordStatus.Normal + "'", "", pagesize * page + 1, pagesize * (page + 1));
StringBuilder sb = new StringBuilder();
if (ds!=null)
{
foreach (DataRow row in ds.Tables[0].Rows)
{
sb.Append("<tr><td>");
sb.Append(row["GoodsUid"]);
sb.Append("</td><td>");
sb.Append(row["GoodsName"]);
sb.Append("</td></tr>");
}
}
return sb.ToString();
}
#endregion
public bool IsReusable
{
get
{
return false;
}
}
}
}

整體流程是:前端透過 Ajax 向 ashx 傳送 pageIndex 與 pageSize 參數,後端依這兩個參數查詢對應區間的資料並以 HTML 片段回傳,前端再將其動態附加到表格,从而实现无刷新分页。這種前後端分離的做法能有效降低伺服器負擔並提升使用者體驗。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved