用jQuery中的ajax分頁實現代碼

來源:互聯網
上載者:User

功能簡介:主要功能就是分頁顯示資料了,可在設定檔中配置每頁要顯示的頁碼,可以做多條件聯集查詢,這裡只是做一個簡單的查詢。歡迎拍磚,有問題的還望大蝦們斧正哈。看看這個,無重新整理的噢!!

具體實現請看源碼:

1、aspx頁面

複製代碼 代碼如下:<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AjaxPage.aspx.cs" Inherits="MeasurementWellCurve.UI.AjaxPage" %>
<!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>
<title>ajax分頁</title>
<link href="../CSS/tb_Style.css" rel="stylesheet" type="text/css" />
<script src="../JS/jquery-1.4.2.min.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<div id="divLayer">
<div>
編號:<asp:TextBox ID="txtCSBH" runat="server"></asp:TextBox><input id="btnSearch" type="button"
value="查詢" />
</div>
<table id="jgcsTable" class="listTable" cellpadding="0" cellspacing="0">
<thead>
<tr>
<th>
測試編號
</th>
<th>
地層滲透率K
</th>
<th>
井筒儲集常數C
</th>
<th>
表皮係數S
</th>
<th>
堵塞比
</th>
<th>
探測半徑
</th>
<th>
擬合地層壓力
</th>
<th>
邊界距離
</th>
<th>
壓力係數
</th>
<th>
複合儲能比
</th>
<th>
操作
</th>
</tr>
</thead>
<tbody id="tb_body">
</tbody>
<tfoot id="load">
<tr>
<td align="center" colspan="11">
<img src="../images/loading.gif" />
</td>
</tr>
</tfoot>
</table>
<div class="navigation">
<div style="text-align: left; float: left; width: 260px;">
共<label id="lblToatl"></label>條資料 第[<label id="lblCurent"></label>]頁/共[<label id="lblPageCount">0</label>]頁
</div>
<div style="text-align: right; float: right;">
<a id="first" href="#">首頁</a> <a id="previous" href="#">上一頁</a> <a id="next" href="#">
下一頁</a> <a id="last" href="#">末頁</a>
</div>
</div>
</div>
</form>
</body>
</html>

2、具體實現JS 複製代碼 代碼如下:var pageIndex = 1; //頁索引
var where = " where 1=1";
$(function() {
BindData();
// GetTotalCount(); //總記錄條數
//GetPageCount(); //總頁數綁定
//第一頁按鈕click事件
$("#first").click(function() {
pageIndex = 1;
$("#lblCurent").text(1);
BindData();
});
//上一頁按鈕click事件
$("#previous").click(function() {
if (pageIndex != 1) {
pageIndex--;
$("#lblCurent").text(pageIndex);
}
BindData();
});
//下一頁按鈕click事件
$("#next").click(function() {
var pageCount = parseInt($("#lblPageCount").text());
if (pageIndex != pageCount) {
pageIndex++;
$("#lblCurent").text(pageIndex);
}
BindData();
});
//最後一頁按鈕click事件
$("#last").click(function() {
var pageCount = parseInt($("#lblPageCount").text());
pageIndex = pageCount;
BindData();
});
//查詢
$("#btnSearch").click(function() {
where = " where 1=1";
var csbh = $("#txtCSBH").val();
if (csbh != null && csbh != NaN) {
pageIndex = 1;
where += " and csbh like '%" + csbh + "%'";
}
BindData();
});
})
//AJAX方法取得資料並顯示到頁面上
function BindData() {
$.ajax({
type: "get", //使用get方法訪問後台
dataType: "json", //返回json格式的資料
url: "../AjaxService/JgcsService.ashx", //要訪問的後台地址
data: { "pageIndex": pageIndex, "where": where }, //要發送的資料
ajaxStart: function() { $("#load").show(); },
complete: function() { $("#load").hide(); }, //AJAX請求完成時隱藏loading提示
success: function(msg) {//msg為返回的資料,在這裡做資料繫結
var data = msg.table;
if (data.length != 0) {
var t = document.getElementById("tb_body"); //擷取展示資料的表格
while (t.rows.length != 0) {
t.removeChild(t.rows[0]); //在讀取資料時如果表格已存在行.一律刪除
}
}
$.each(data, function(i, item) {
$("#jgcsTable").append("<tr><td>" + item.CSBH + "</td><td>" + item.K + " </td><td>" + item.C +
" </td><td>" + item.S + " </td><td>" + item.DSB + " </td><td>" + item.TCBJ +
"</td><td>" + item.LHDCYL + " </td><td>" + item.BJJL + "</td><td>" + item.YLXS +
" </td><td>" + item.FCTH + " </td><td><a href='AjaxPaging.htm' target='blank'>" +
"<img src='../images/icon_06.gif' alt='查看詳細資料'" +
"id='btnInsert'style='border-width:0px;' /></a></td></tr>");
})
},
error: function() {
var t = document.getElementById("tb_body"); //擷取展示資料的表格
while (t.rows.length != 0) {
t.removeChild(t.rows[0]); //在讀取資料時如果表格已存在行.一律刪除
}
alert("載入資料失敗");
} //載入失敗,請求錯誤處理
//ajaxStop:$("#load").hide()
});
GetTotalCount();
GetPageCount();
bindPager();
}
// 頁尾屬性設定
function bindPager() {
//填充分布控制項資訊
var pageCount = parseInt($("#lblPageCount").text()); //總頁數
if (pageCount == 0) {
document.getElementById("lblCurent").innerHTML = "0";
}
else {
if (pageIndex > pageCount) {
$("#lblCurent").text(1);
}
else {
$("#lblCurent").text(pageIndex); //當前頁
}
}
document.getElementById("first").disabled = (pageIndex == 1 || $("#lblCurent").text() == "0") ? true : false;
document.getElementById("previous").disabled = (pageIndex <= 1 || $("#lblCurent").text() == "0") ? true : false;
document.getElementById("next").disabled = (pageIndex >= pageCount) ? true : false;
document.getElementById("last").disabled = (pageIndex == pageCount || $("#lblCurent").text() == "0") ? true : false;
}
//AJAX方法取得總頁數
function GetPageCount() {
var pageCount;
$.ajax({
type: "get",
dataType: "text",
url: "../AjaxService/JgcsService.ashx",
data: { "wherePageCount": where }, //"wherePageCount" + where,個人建議不用這種方式
async: false,
success: function(msg) {
document.getElementById("lblPageCount").innerHTML = msg;
}
});
}
//AJAX方法取得記錄總數
function GetTotalCount() {
var pageCount;
$.ajax({
type: "get",
dataType: "text",
url: "../AjaxService/JgcsService.ashx",
data: { "whereCount": where },
async: false,
success: function(msg) {
document.getElementById("lblToatl").innerHTML = msg;
}
});
}

3、一般處理常式ashx中的代碼 複製代碼 代碼如下:public class JgcsService : IHttpHandler
{
readonly int pageSize = 15;
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//不讓瀏覽器緩衝
context.Response.Buffer = true;
context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
context.Response.AddHeader("pragma", "no-cache");
context.Response.AddHeader("cache-control", "");
context.Response.CacheControl = "no-cache";
string result = "";
//記錄總條數
if (!string.IsNullOrEmpty(context.Request["whereCount"]))
{
string where = context.Request.Params["whereCount"].ToString();
result = Jgcs.GetToatlNum(where).ToString();
}
//總頁數
if (!string.IsNullOrEmpty(context.Request["wherePageCount"]))
{
string where = context.Request.Params["wherePageCount"].ToString();
int count = Jgcs.GetToatlNum(where);
string pageCount = Math.Ceiling((double)count / (double)pageSize).ToString();
result = pageCount;
}
//分頁資料
if (!string.IsNullOrEmpty(context.Request.Params["pageIndex"])
&& !string.IsNullOrEmpty(context.Request.Params["where"]))
{
string where = context.Request.Params["where"].ToString();
int pageIndex = Convert.ToInt32(context.Request.Params["pageIndex"]);
result = GetJsonString(where, pageIndex);
}
context.Response.Write(result);
}
/// <summary>
/// 返回json串
/// </summary>
/// <param name="where">查詢條件</param>
/// <param name="pageIndex">頁面索引</param>
/// <returns>json串</returns>
protected string GetJsonString(string where, int pageIndex)
{
DataTable dt = Jgcs.GetInfo("csbh", where, pageIndex, pageSize);
return JsonHelper.DataTable2Json(dt, "table");
}
public bool IsReusable
{
get
{
return false;
}
}
}

4、分頁查詢的方法可看可不看了,都會這個吧,做樣本簡單的開了個頭,應用時在處理方面可不要這麼去寫噢,貼下來僅做一個參考
分頁方法 複製代碼 代碼如下:/// <summary>
/// 分頁查詢的方法
/// </summary>
/// <param name="orderFile">排序欄位</param>
/// <param name="where">查詢條件</param>
/// <param name="pageNumber">當前頁</param>
/// <param name="pageSize">頁大小</param>
/// <returns></returns>
public static DataTable GetInfo(string orderFile, string where, int pageNumber, int pageSize)
{
DBHelper db = new DBHelper();
string str = @"with TestInfo as
(
select row_number() over(order by {0} desc) as rowNumber,* from
(select CSBH,K,C,S,DSB,TCBJ,LHDCYL,BJJL,BJLX,YLXS,FCTH,KHM1,KHM2,QKCS from YW_JGCS) temp {1}
)
select * from TestInfo
where rowNumber between (({2}-1)*{3}+1) and {2}*{3}";
string strSql = string.Format(str, orderFile, where, pageNumber, pageSize);
try
{
db.DBOpen();
return db.DbDataSet(strSql);
}
catch (Exception ex)
{
throw ex;
}
finally
{
db.DBClose();
}
}
/// <summary>
/// 結果參數總條數
/// </summary>
/// <param name="where"></param>
/// <returns></returns>
public static int GetToatlNum(string where)
{
DBHelper db = new DBHelper();
string strSql = string.Format(@"select count(*) from (select CSBH,K,C,S,DSB,TCBJ,LHDCYL,BJJL,BJLX,YLXS,FCTH,KHM1,KHM2,QKCS from YW_JGCS) temp {0}", where);
try
{
db.DBOpen();
return (int)db.ExecuteScalar(strSql);
}
catch (Exception ex)
{
throw ex;
}
finally
{
db.DBClose();
}
}

好了,代碼就這麼多

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.