用jQuery中的ajax分頁(yè)實(shí)現(xiàn)代碼
具體實(shí)現(xiàn)請(qǐng)看源碼:
1、aspx頁(yè)面
<%@ 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分頁(yè)</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>
編號(hào):<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>
測(cè)試編號(hào)
</th>
<th>
地層滲透率K
</th>
<th>
井筒儲(chǔ)集常數(shù)C
</th>
<th>
表皮系數(shù)S
</th>
<th>
堵塞比
</th>
<th>
探測(cè)半徑
</th>
<th>
擬合地層壓力
</th>
<th>
邊界距離
</th>
<th>
壓力系數(shù)
</th>
<th>
復(fù)合儲(chǔ)能比
</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>條數(shù)據(jù) 第[<label id="lblCurent"></label>]頁(yè)/共[<label id="lblPageCount">0</label>]頁(yè)
</div>
<div style="text-align: right; float: right;">
<a id="first" href="#">首頁(yè)</a> <a id="previous" href="#">上一頁(yè)</a> <a id="next" href="#">
下一頁(yè)</a> <a id="last" href="#">末頁(yè)</a>
</div>
</div>
</div>
</form>
</body>
</html>
2、具體實(shí)現(xiàn)JS
var pageIndex = 1; //頁(yè)索引
var where = " where 1=1";
$(function() {
BindData();
// GetTotalCount(); //總記錄條數(shù)
//GetPageCount(); //總頁(yè)數(shù)綁定
//第一頁(yè)按鈕click事件
$("#first").click(function() {
pageIndex = 1;
$("#lblCurent").text(1);
BindData();
});
//上一頁(yè)按鈕click事件
$("#previous").click(function() {
if (pageIndex != 1) {
pageIndex--;
$("#lblCurent").text(pageIndex);
}
BindData();
});
//下一頁(yè)按鈕click事件
$("#next").click(function() {
var pageCount = parseInt($("#lblPageCount").text());
if (pageIndex != pageCount) {
pageIndex++;
$("#lblCurent").text(pageIndex);
}
BindData();
});
//最后一頁(yè)按鈕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方法取得數(shù)據(jù)并顯示到頁(yè)面上
function BindData() {
$.ajax({
type: "get", //使用get方法訪問(wèn)后臺(tái)
dataType: "json", //返回json格式的數(shù)據(jù)
url: "../AjaxService/JgcsService.ashx", //要訪問(wèn)的后臺(tái)地址
data: { "pageIndex": pageIndex, "where": where }, //要發(fā)送的數(shù)據(jù)
ajaxStart: function() { $("#load").show(); },
complete: function() { $("#load").hide(); }, //AJAX請(qǐng)求完成時(shí)隱藏loading提示
success: function(msg) {//msg為返回的數(shù)據(jù),在這里做數(shù)據(jù)綁定
var data = msg.table;
if (data.length != 0) {
var t = document.getElementById("tb_body"); //獲取展示數(shù)據(jù)的表格
while (t.rows.length != 0) {
t.removeChild(t.rows[0]); //在讀取數(shù)據(jù)時(shí)如果表格已存在行.一律刪除
}
}
$.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='查看詳細(xì)信息'" +
"id='btnInsert'style='border-width:0px;' /></a></td></tr>");
})
},
error: function() {
var t = document.getElementById("tb_body"); //獲取展示數(shù)據(jù)的表格
while (t.rows.length != 0) {
t.removeChild(t.rows[0]); //在讀取數(shù)據(jù)時(shí)如果表格已存在行.一律刪除
}
alert("加載數(shù)據(jù)失敗");
} //加載失敗,請(qǐng)求錯(cuò)誤處理
//ajaxStop:$("#load").hide()
});
GetTotalCount();
GetPageCount();
bindPager();
}
// 頁(yè)腳屬性設(shè)置
function bindPager() {
//填充分布控件信息
var pageCount = parseInt($("#lblPageCount").text()); //總頁(yè)數(shù)
if (pageCount == 0) {
document.getElementById("lblCurent").innerHTML = "0";
}
else {
if (pageIndex > pageCount) {
$("#lblCurent").text(1);
}
else {
$("#lblCurent").text(pageIndex); //當(dāng)前頁(yè)
}
}
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方法取得總頁(yè)數(shù)
function GetPageCount() {
var pageCount;
$.ajax({
type: "get",
dataType: "text",
url: "../AjaxService/JgcsService.ashx",
data: { "wherePageCount": where }, //"wherePageCount" + where,個(gè)人建議不用這種方式
async: false,
success: function(msg) {
document.getElementById("lblPageCount").innerHTML = msg;
}
});
}
//AJAX方法取得記錄總數(shù)
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 = "";
//記錄總條數(shù)
if (!string.IsNullOrEmpty(context.Request["whereCount"]))
{
string where = context.Request.Params["whereCount"].ToString();
result = Jgcs.GetToatlNum(where).ToString();
}
//總頁(yè)數(shù)
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;
}
//分頁(yè)數(shù)據(jù)
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">頁(yè)面索引</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、分頁(yè)查詢的方法可看可不看了,都會(huì)這個(gè)吧,做示例簡(jiǎn)單的開了個(gè)頭,應(yīng)用時(shí)在處理方面可不要這么去寫噢,貼下來(lái)僅做一個(gè)參考
分頁(yè)方法
/// <summary>
/// 分頁(yè)查詢的方法
/// </summary>
/// <param name="orderFile">排序字段</param>
/// <param name="where">查詢條件</param>
/// <param name="pageNumber">當(dāng)前頁(yè)</param>
/// <param name="pageSize">頁(yè)大小</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>
/// 結(jié)果參數(shù)總條數(shù)
/// </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();
}
}
好了,代碼就這么多
- JQuery+Ajax無(wú)刷新分頁(yè)的實(shí)例代碼
- jQuery Pagination Ajax分頁(yè)插件(分頁(yè)切換時(shí)無(wú)刷新與延遲)中文翻譯版
- jQuery DataTables插件自定義Ajax分頁(yè)實(shí)例解析
- jQuery實(shí)現(xiàn)分頁(yè)功能(含ajax請(qǐng)求、后臺(tái)數(shù)據(jù)、附完整demo)
- JS+Ajax+Jquery實(shí)現(xiàn)頁(yè)面無(wú)刷新分頁(yè)以及分組 超強(qiáng)的實(shí)現(xiàn)
- 使用PHP+JQuery+Ajax分頁(yè)的實(shí)現(xiàn)
- MVC+jQuery.Ajax異步實(shí)現(xiàn)增刪改查和分頁(yè)
- 使用Jquery+Ajax+Json如何實(shí)現(xiàn)分頁(yè)顯示附JAVA+JQuery實(shí)現(xiàn)異步分頁(yè)
- jQuery插件select2利用ajax高效查詢大數(shù)據(jù)列表(可搜索、可分頁(yè))
- jquery+Ajax實(shí)現(xiàn)簡(jiǎn)單分頁(yè)條效果
相關(guān)文章
jQuery+formdata實(shí)現(xiàn)上傳進(jìn)度特效遇到的問(wèn)題
這篇文章主要介紹了jQuery+formdata實(shí)現(xiàn)上傳進(jìn)度特效遇到的問(wèn)題的相關(guān)資料,需要的朋友可以參考下2016-02-02jquery改變disabled的boolean狀態(tài)的三種方法
改變disabled的boolean狀態(tài),下面為大家介紹三種比較不錯(cuò)的方法,大家可以參考下2013-12-12關(guān)于jQuery參考實(shí)例 1.0 jQuery的哲學(xué)
本文為大家介紹關(guān)于jQuery參考實(shí)例 1.0 jQuery的哲學(xué),有需要的朋友可以參考一下。2013-04-04jquery動(dòng)畫2.元素坐標(biāo)動(dòng)畫效果(創(chuàng)建一個(gè)圖片走廊)
今天文章的內(nèi)容是關(guān)于使用jquery的animate方法,修改html元素的position屬性,創(chuàng)建一個(gè)圖片走廊2012-08-08jquery用get實(shí)現(xiàn)ajax在ie里面刷新不進(jìn)入后臺(tái)解決方法
jquery用get實(shí)現(xiàn)ajax在ie里面刷新不進(jìn)入后臺(tái)的情況想必大家都有遇到過(guò)吧,下面與大家分享下具體的解決方法,希望對(duì)大家解決問(wèn)題有所幫助2013-08-08jQuery獲取頁(yè)面元素絕對(duì)與相對(duì)位置的方法
這篇文章主要介紹了jQuery獲取頁(yè)面元素絕對(duì)與相對(duì)位置的方法,涉及jQuery中offset、position等方法的使用技巧,需要的朋友可以參考下2015-06-06用jQuery中的ajax分頁(yè)實(shí)現(xiàn)代碼
去年的時(shí)候剛接觸Jquery,也就做界面特效用了下,對(duì)其很有興趣,迫于現(xiàn)在項(xiàng)目中不怎么用,對(duì)其甚是想念呀,這不沒抽點(diǎn)時(shí)間再來(lái)看看Juery中好玩的東西。2011-09-09