标签:
使用bootstrap分页插件,如下代码是html代码
<title>分页</title>
<link href="page/bootstrap-3.3.5-dist/css/bootstrap.min.css" rel="stylesheet" />
<script src="page/jquery-1.9.1.min.js"></script>
<script src="page/bootstrap-3.3.5-dist/js/bootstrap.min.js"></script>
<script src="page/bootstrap-paginator.js"></script>
</head>
<body>
<div id="list">
<table id="data_table" class="table table-striped">
<thead>
<tr>
<th>编号</th><th>标题</th><th>用户</th>
</thead>
<tbody id="tbody">
</tbody>
</table>
</div>
<div><ul id=‘bp-3-element-test‘ style="cursor:hand;"></ul></div>
<script>
$(function () {
var str = "";
var carId = 1;
$.ajax({
url: "page.ashx",
data:{id:carId},
dataType: ‘json‘,
type: ‘post‘,
success: function (data) {
if (data != null) {
for (var i = 0; i < data.dt.length; i++) {
str += "<tr><td>" + data.dt[i]["New_Id"] + "</td><td>" + data.dt[i]["New_Title"] + "</td><td>" + data.dt[i]["New_UserId"] + "</td></tr>";
}
var pageCount = data.pageCount; //取到pageCount的值(把返回数据转成object类型)
var currentPage = data.CurrentPage; //得到urrentPage
var element = $("#bp-3-element-test");
var options = {
bootstrapMajorVersion: 3,
currentPage: currentPage,
totalPages: pageCount,
numberOfPages: 5,
itemTexts: function (type, page, current) {
switch (type) {
case "first":
return "首页";
case "prev":
return "上一页";
case "next":
return "下一页";
case "last":
return "末页";
case "page":
return page;
}
}, onPageClicked: function (event, originalEvent, type, page) {
var str1 = "";
$("#tbody").html("");
$.ajax({
url: "page.ashx",
data: { id: page },
type: ‘post‘,
success: function (data1) {
//console.log(data1);
if (data1 != null) {
var jsonobj = eval(‘(‘ + data1 + ‘)‘); //将json字符串转换成json数组
console.log(jsonobj.dt[0]["New_Id"]);
for (var i = 0; i < jsonobj.dt.length; i++) {
str1 += "<tr><td>" + jsonobj.dt[i]["New_Id"] + "</td><td>" + jsonobj.dt[i]["New_Title"] + "</td><td>" + jsonobj.dt[i]["New_UserId"] + "</td></tr>";
}
console.log(str1);
$("#tbody").html(str1);
}
}
})
}
}
element.bootstrapPaginator(options);
}
$("#tbody").html(str);
}
})
})
</script>
</body>
一般处理程序
context.Response.ContentType = "text/plain";
int id =Convert.ToInt32(context.Request["id"]);
const int pageSize = 5;
string sql = "select count(*) from news";
int rowCount =(int)SQLHelper.ExecuteSclar(sql);
if (rowCount % pageSize != 0)
{
rowCount = rowCount / pageSize + 1;
}
else
{
rowCount = rowCount / pageSize;
}
DataTable dt = GetListByPageByWhere("news", "*", "new_id", "", pageSize, id, rowCount);
var strResult = "{\"pageCount\":" + rowCount + ",\"CurrentPage\":" + id + ",\"dt\":" + JsonConvert.SerializeObject(dt) + "}";
context.Response.Write(strResult);
return;
}
private static DataTable GetListByPageByWhere(string tabname, string ReFieldsStr, string order, string strWhere, int PageSize, int PageIndex, int TotalRecord)
{
SqlParameter[] parameters = new SqlParameter[]{
new SqlParameter{ ParameterName="@TableName" ,SqlDbType=SqlDbType.VarChar,Value=tabname},
new SqlParameter{ParameterName="@ReFieldsStr",SqlDbType=SqlDbType.VarChar,Value=ReFieldsStr},
new SqlParameter{ParameterName="@OrderString",SqlDbType=SqlDbType.VarChar,Value=order},
new SqlParameter{ParameterName="@WhereString", SqlDbType=SqlDbType.VarChar, Value=strWhere},
new SqlParameter{ ParameterName="@PageSize", SqlDbType=SqlDbType.Int,Value=PageSize},
new SqlParameter{ParameterName="@PageIndex", SqlDbType=SqlDbType.Int,Value=PageIndex},
new SqlParameter{ParameterName="@TotalRecord",SqlDbType= SqlDbType.Int,Value=TotalRecord},
};
return SQLHelper.ExecuteDataTable("PROCE_PAGECHANGE", CommandType.StoredProcedure, parameters);
数据库的分页存储过程
USE [ActivaOnlineSupport] GO /****** Object: StoredProcedure [dbo].[PROCE_PAGECHANGE] Script Date: 08/13/2014 13:45:17 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[PROCE_PAGECHANGE] ( @TableName varchar(50), --表名 @ReFieldsStr varchar(200) = ‘*‘, --字段名(全部字段为*) @OrderString varchar(200), --排序字段(必须!支持多字段不用加order by) @WhereString varchar(500) =N‘‘, --条件语句(不用加where) @PageSize int, --每页多少条记录 @PageIndex int = 1 , --指定当前为第几页 @TotalRecord int output --返回总记录数 ) AS BEGIN --处理开始点和结束点 Declare @StartRecord int; Declare @EndRecord int; Declare @TotalCountSql nvarchar(500); Declare @SqlString nvarchar(2000); set @StartRecord = (@PageIndex-1)*@PageSize + 1 set @EndRecord = @StartRecord + @PageSize - 1 SET @TotalCountSql= N‘select @TotalRecord = count(*) from ‘ + @TableName;--总记录数语句 SET @SqlString = N‘(select row_number() over (order by ‘+ @OrderString +‘) as rowId,‘+@ReFieldsStr+‘ from ‘+ @TableName;--查询语句 -- IF (@WhereString! = ‘‘ or @WhereString!=null) BEGIN SET @TotalCountSql=@TotalCountSql + ‘ where ‘+ @WhereString; SET @SqlString =@SqlString+ ‘ where ‘+ @WhereString; END --第一次执行得到 --IF(@TotalRecord is null) -- BEGIN EXEC sp_executesql @totalCountSql,N‘@TotalRecord int out‘,@TotalRecord output;--返回总记录数 -- END ----执行主语句 set @SqlString =‘select * from ‘ + @SqlString + ‘) as t where rowId between ‘ + ltrim(str(@StartRecord)) + ‘ and ‘ + ltrim(str(@EndRecord)); Exec(@SqlString) END
标签:
原文地址:http://www.cnblogs.com/huangguojin/p/5427875.html