1.先写持久化类,传两个数就行,卸载数据访问层
public List<NewInfo> GetPageList(int start,int end)
{
string sqlstr="select * from(select row_number() OVER(ORDER BY id) as num,* from in T_News) as t where t.num)=@start and t.num<=@end";
SqlParameter[] sqlPara={new SqlParameter("@start",start),new SqlParameter("@end",end)};
DataTable dt=SqlHelper.GetTable(sqlstr,sqlpara);
//接下来将取到的数据持久化成对象
//返回该类的对象就行了,代码省略
//return list
}
public int RecordCount()
{
string str="select count(1) from T_News";
return //调用sqlhelper中的方法
}
2.将分页写在逻辑业务层
public List<NewInfo> GetPageList(int pageIndex,int PageSize)
{
int start=(pageIndex-1)*pageSize+1;
int end=pageIndex*pageSize;
List<NewInfo> list=NewInfoDal.GetPageList(start,end);
return list;
}
public int GetPageCount(int pageSize)
{
int numcount=NewInfoDal.RecordCount();
return Math.Ceiling((double)numcount/pageSize);
}
3MVC或者业务逻辑
public ActionResult Index()
{
int pageIndex=Request["pageIndex"] !=null? int.parse(Request["page"]);
int pageSize=10;
int pageCount=NewInfoService.GetPageCount(pageSize);
pageIndex=pageIndex<1 ? 1 :pageIndex;
pageIndex=pageIndex>pageCount> ? pageCount: pageIndex;
List<NewInfo> list=NewInoService.getPageList(pageIndex,pageSize);
//下面是数据渲染到前台
}
3.下面利用pageindex 和pageCount来做页码条
public static string(int pageIndex,int pageCount)
{
//页码显示10条数据,算出开始显示的数start,和end
if(pageCount==1)
{
return string.Empty;
}
int start=pageIndex-5;//起始位置
if(start<1)
{
start=1
}
int end=start+9;
if(end>pageCount)
{
end=pageCount;
}
StringBuilder sb=new stringBuilder();
for(int i=start,i<=end;i++)
{
//当前页没有超链接
if(i==pageIndex)
{
sb.Append(i);
}
else
{
sb.Append(string.Format("<a href=?pageIndex={0}'>{0}</a>",i));
}
}
return sb.ToString();
}
4.前台视图页调用,或者使用@Html.Raw,用原始方法输出
@MvcHtmlString.Create(PageBar.GetPageBar(ViewData["pageIndex"],(int)ViewData["PageCount"]))
5在jquery esayui加上样式就搞定,输出记录时,把该条数据从当前的表格中移出
6.请参考另外一个MVC分页方法
public List<NewInfo> GetPageList(int start,int end)
{
string sqlstr="select * from(select row_number() OVER(ORDER BY id) as num,* from in T_News) as t where t.num)=@start and t.num<=@end";
SqlParameter[] sqlPara={new SqlParameter("@start",start),new SqlParameter("@end",end)};
DataTable dt=SqlHelper.GetTable(sqlstr,sqlpara);
//接下来将取到的数据持久化成对象
//返回该类的对象就行了,代码省略
//return list
}
public int RecordCount()
{
string str="select count(1) from T_News";
return //调用sqlhelper中的方法
}
2.将分页写在逻辑业务层
public List<NewInfo> GetPageList(int pageIndex,int PageSize)
{
int start=(pageIndex-1)*pageSize+1;
int end=pageIndex*pageSize;
List<NewInfo> list=NewInfoDal.GetPageList(start,end);
return list;
}
public int GetPageCount(int pageSize)
{
int numcount=NewInfoDal.RecordCount();
return Math.Ceiling((double)numcount/pageSize);
}
3MVC或者业务逻辑
public ActionResult Index()
{
int pageIndex=Request["pageIndex"] !=null? int.parse(Request["page"]);
int pageSize=10;
int pageCount=NewInfoService.GetPageCount(pageSize);
pageIndex=pageIndex<1 ? 1 :pageIndex;
pageIndex=pageIndex>pageCount> ? pageCount: pageIndex;
List<NewInfo> list=NewInoService.getPageList(pageIndex,pageSize);
//下面是数据渲染到前台
}
3.下面利用pageindex 和pageCount来做页码条
public static string(int pageIndex,int pageCount)
{
//页码显示10条数据,算出开始显示的数start,和end
if(pageCount==1)
{
return string.Empty;
}
int start=pageIndex-5;//起始位置
if(start<1)
{
start=1
}
int end=start+9;
if(end>pageCount)
{
end=pageCount;
}
StringBuilder sb=new stringBuilder();
for(int i=start,i<=end;i++)
{
//当前页没有超链接
if(i==pageIndex)
{
sb.Append(i);
}
else
{
sb.Append(string.Format("<a href=?pageIndex={0}'>{0}</a>",i));
}
}
return sb.ToString();
}
4.前台视图页调用,或者使用@Html.Raw,用原始方法输出
@MvcHtmlString.Create(PageBar.GetPageBar(ViewData["pageIndex"],(int)ViewData["PageCount"]))
5在jquery esayui加上样式就搞定,输出记录时,把该条数据从当前的表格中移出
6.请参考另外一个MVC分页方法
http://www.cnblogs.com/xiaofengfeng/p/3705632.html
下面这段引用了已经分好页的方法
public static string ShowPageNavigate(int currentPage, int pageSize, int totalCount)
{
var redirectTo = HttpContext.Current.Request.Url.AbsolutePath;
pageSize = pageSize <= 0 ? 3 : pageSize;
var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //总页数
var output = new StringBuilder();
if (totalPages > 1)
{
//if (currentPage != 1)
{//处理首页连接
output.AppendFormat("<a class='pageLink' href='{0}?pageIndex=1&pageSize={1}'>首页</a> ", redirectTo, pageSize);
}
if (currentPage > 1)
{//处理上一页的连接
output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>上一页</a> ", redirectTo, currentPage - 1, pageSize);
}
else
{
// output.Append("<span class='pageLink'>上一页</span>");
}
output.Append(" ");
int currint = 5;
for (int i = 0; i <= 10; i++)
{//一共最多显示10个页码,前面5个,后面5个
if ((currentPage + i - currint) >= 1 && (currentPage + i - currint) <= totalPages)
{
if (currint == i)
{//当前页处理
//output.Append(string.Format("[{0}]", currentPage));
output.AppendFormat("<a class='cpb' href='{0}?pageIndex={1}&pageSize={2}'>{3}</a> ", redirectTo, currentPage, pageSize, currentPage);
}
else
{//一般页处理
output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>{3}</a> ", redirectTo, currentPage + i - currint, pageSize, currentPage + i - currint);
}
}
output.Append(" ");
}
if (currentPage < totalPages)
{//处理下一页的链接
output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>下一页</a> ", redirectTo, currentPage + 1, pageSize);
}
else
{
//output.Append("<span class='pageLink'>下一页</span>");
}
output.Append(" ");
if (currentPage != totalPages)
{
output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>末页</a> ", redirectTo, totalPages, pageSize);
}
output.Append(" ");
}
output.AppendFormat("第{0}页 / 共{1}页", currentPage, totalPages);//这个统计加不加都行
return output.ToString();
}
}
分页css样式
body {
}
.paginator {
font: 12px Arial, Helvetica, sans-serif;
padding: 10px 20px 10px 0;
margin: 0px;
}
.paginator a {
border: solid 1px #ccc;
color: #0063dc;
cursor: pointer;
text-decoration: none;
}
.paginator a:visited {
padding: 1px 6px;
border: solid 1px #ddd;
background: #fff;
text-decoration: none;
}
.paginator .cpb {
border: 1px solid #F50;
font-weight: 700;
color: #F50;
background-color: #ffeee5;
}
.paginator a:hover {
border: solid 1px #F50;
color: #f60;
text-decoration: none;
}
.paginator a, .paginator a:visited, .paginator .cpb, .paginator a:hover {
float: left;
height: 16px;
line-height: 16px;
min-width: 10px;
_width: 10px;
margin-right: 5px;
text-align: center;
white-space: nowrap;
font-size: 12px;
font-family: Arial,SimSun;
padding: 0 3px;
}
分页后台,这里是先把所有数据取出来,传到客户端然后再进行分页,这样会降低性能,不过可以参考,一般分页都写在服务端
public ActionResult loadjson()
{
int pageSize = Request["pageSize"] == null ? 10 : int.Parse(Request["pageSize"]);
int pageIndex = Request["pageIndex"] == null ? 1 : int.Parse(Request["pageIndex"]);
int totalCount = shop.tbl_admin.Count();
//给前台userinfo所有的数据,并且是json格式
var allorder = shop.tbl_admin.OrderBy(u=>u.id)
.Skip(pageSize*(pageIndex-1))
.Take(pageSize)
.ToList();
//接受一个对像,内部把此对象使用javaScript序列化类对象志字符串,发送到前台
var data = from u in allorder select new { u.id,u.realname,u.sex};
string strNav = PageNavHelper.ShowPageNavigate(pageIndex,pageSize,totalCount);
var result = new {Data=data, NavStr=strNav };
return Json(result, JsonRequestBehavior.AllowGet);
}
ajax异步加载
<script type="text/javascript">
$(function () {
//页面加载完成后从后如加载当前页数据
initTable();
});
//初始化表格数据
function initTable(queryData)
{
$.getJSON("/AjaxUser/loadjson",queryData, function (data) {
var tb = $("#tbList");
//先移除旧的,添加新的
$("#tbList tr[type=data]").remove();
for (var i = 0; i < data.Data.length; i++)
{
var strTr = "<tr type='data'>";
strTr += "<td>" + data.Data[i].id + "</td>";
strTr += "<td>" + data.Data[i].realname + "</td>";
strTr += "<td>" + data.Data[i].sex + "</td>";
strTr += "<td><a UId='" + data.Data[i].id + "' href='javascript:void(0)'>修改</a>" +
"<a UId='" + data.Data[i].ID + "' href='javascript:void(0)'>删除</a></td>";
strTr += "</tr>";
tb.append(strTr);
}
$("#Nav").html(data.NavStr);
//绑定分页标签的点击事件
$(".pageLink").click(function () {
//把页码弹出来
var strHref = $(this).attr("href");
var queryStr = strHref.substr(strHref.indexOf('?') + 1);
//alert(queryStr);
initTable(queryStr);
return false;
});
});
}
</script>
</head>
<body>
<div>
<table id="tbList">
<tr>
<td>id</td>
<td>姓名</td>
<td>性别</td>
<td>操作</td>
</tr>
</table>
<div id="Nav" class="paginator">
</div>
</div>
</body>
</html>