1.頁面
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Porschev----無刷新翻頁</title>
<script src="~/Scripts/Test/jquery-1.4.1.min.js"></script>
<script src="~/Scripts/Test/jquery.pagination.js"></script>
<script src="~/Scripts/Test/tablecloth.js"></script>
<link href="~/Scripts/Test/pagination.css" rel="stylesheet" />
<link href="~/Scripts/Test/tablecloth.css" rel="stylesheet" />
<script type="text/javascript">
var pageIndex = 0; //頁面索引初始值
var pageSize = 10; //每頁顯示條數初始化,修改顯示條數,修改這里即可
$(function () {
InitTable(0); //Load事件,初始化表格數據,頁面索引為0(第一頁)
//分頁,PageCount是總條目數,這是必選參數,其它參數都是可選
$("#Pagination").pagination(PageCount, {
callback: PageCallback,
prev_text: '上一頁', //上一頁按鈕里text
next_text: '下一頁', //下一頁按鈕里text
items_per_page: pageSize, //顯示條數
num_display_entries: 6, //連續分頁主體部分分頁條目數
current_page: pageIndex, //當前頁索引
num_edge_entries: 2 //兩側首尾分頁條目數
});
//翻頁調用
function PageCallback(index, jq) {
InitTable(index);
}
//請求數據
function InitTable(pageIndex) {
$.ajax({
type: "Get",
dataType: "text",
url: '/Test/GetData', //提交到一般處理程序請求數據
data: "pageIndex=" + (pageIndex + 1) + "&pageSize=" + pageSize, //提交兩個參數:pageIndex(頁面索引),pageSize(顯示條數)
success: function (data) {
$("#Result tr:gt(0)").remove(); //移除Id為Result的表格里的行,從第二行開始(這里根據頁面布局不同頁變)
$("#Result").append(data); //將返回的數據追加到表格
}
});
}
});
</script>
</head>
<body>
<div align="center">
<h1>Posrchev----無刷新分頁</h1>
</div>
<div id="container">
<table id="Result" cellspacing="0" cellpadding="0">
<tr>
<th>編號</th>
<th>名稱</th>
</tr>
</table>
<div id="Pagination"></div>
</div>
</body>
</html>
public ActionResult GetData()
{
string str = string.Empty;
//具體的頁面數
int pageIndex;
int.TryParse(Request["pageIndex"], out pageIndex);
//頁面顯示條數
int size = Convert.ToInt32(Request["pageSize"]);
if (pageIndex == 0)
{
pageIndex = 1;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10; i++)
{
sb.Append("<tr><td>");
sb.Append(i.ToString());
sb.Append("</td><td>");
sb.Append((i+1).ToString());
sb.Append("</td></tr>");
}
str = sb.ToString();
return Content(str);
}
數據量大的時候最好使用json格式返回數據