下面是工具类。
1 publicclassPagingHelper<T>where T :new() 2 { 3 privateint_PageIndex=1; 4 privateint_PageSize=10; 5 privateint_Total=0; 6 privateint_ShowNum=5; 7 privateobject_DataSource=null; 8 privateList<T>_PageData; 9 /// <summary> 10 /// 11 /// </summary> 12 /// <typeparam name="T"></typeparam> 13 /// <param name="dataSource"></param> 14 publicPagingHelper(object dataSource) 15 { 16 this._DataSource= dataSource; 17 this._Total=this.GetDataSource().Count; 18 this._PageData=GetCurrentPage(); 19 } 20 /// <summary> 21 /// 22 /// </summary> 23 /// <param name="dataSource">数据源</param> 24 /// <param name="index">当前页</param> 25 /// <param name="size">每页显示记录条数</param> 26 publicPagingHelper(object dataSource,int index,int size) 27 { 28 this._DataSource= dataSource; 29 this._PageIndex= index; 30 this._PageSize= size; 31 this._Total=this.GetDataSource().Count; 32 this._PageData=GetCurrentPage(); 33 } 34 /// <summary> 35 /// 直接传当前页数据。必须要把总记录条数一块儿传过来 36 /// </summary> 37 /// <param name="pageData">当前页数据</param> 38 /// <param name="index">当前页</param> 39 /// <param name="size">每页记录数</param> 40 /// <param name="total">总的记录数</param> 41 publicPagingHelper(List<T> pageData,int index,int size,int total) 42 { 43 this._PageIndex= index; 44 this._PageSize= size; 45 this._Total= total; 46 this._PageData= pageData; 47 } 48 /// <summary> 49 /// 每页数据 50 /// </summary> 51 publicList<T>PageData 52 { 53 get {return_PageData;} 54 set{_PageData= value;} 55 } 56 /// <summary> 57 /// 数据源,注意必须是List泛型 58 /// </summary> 59 publicobjectDataSource 60 { 61 get {return_DataSource;} 62 set{_DataSource= value;} 63 } 64 /// <summary> 65 /// 当前页码,默认1 66 /// </summary> 67 publicintPageIndex 68 { 69 get {return_PageIndex;} 70 set{_PageIndex= value;} 71 } 72 /// <summary> 73 /// 页大小,默认10 74 /// </summary> 75 publicintPageSize 76 { 77 get {return_PageSize;} 78 set{_PageSize= value;} 79 } 80 /// <summary> 81 /// 显示的页数个数,默认5 82 /// </summary> 83 publicintShowNum 84 { 85 get {return_ShowNum;} 86 set{_ShowNum= value;} 87 } 88 /// <summary> 89 /// 总记录数 90 /// </summary> 91 publicintTotal 92 { 93 get {return_Total;} 94 set{_Total= value;} 95 } 96 /// <summary> 97 /// 总页数 98 /// </summary> 99 publicintMaxIndex 100 { 101 get 102 { 103 return(int)Math.Ceiling(_Total/(_PageSize*1.0)); 104 } 105 } 106 /// <summary> 107 /// 是否最后一页 108 /// </summary> 109 public bool isLast 110 { 111 get 112 { 113 return_PageIndex>=MaxIndex?true:false; 114 } 115 } 116 /// <summary> 117 /// 是否首页,即第一页 118 /// </summary> 119 public bool isFirst 120 { 121 get 122 { 123 return_PageIndex==1||_PageIndex<1?true:false; 124 } 125 } 126 /// <summary> 127 /// 数据源类型转换 128 /// </summary> 129 /// <typeparam name="T"></typeparam> 130 /// <returns></returns> 131 privateList<T>GetDataSource() 132 { 133 return(List<T>)this._DataSource; 134 } 135 /// <summary> 136 /// 获取当前页数据 137 /// </summary> 138 /// <typeparam name="T"></typeparam> 139 /// <returns></returns> 140 publicList<T>GetCurrentPage() 141 { 142 returnGetCurrentPage(GetDataSource()); 143 } 144 /// <summary> 145 /// 获取当前页数据 146 /// </summary> 147 /// <typeparam name="T"></typeparam> 148 /// <param name="t"></param> 149 /// <returns></returns> 150 publicList<T>GetCurrentPage(List<T> t) 151 { 152 //ToList<T>()是重点 153 List<T> result = t.Skip(this._PageSize*(this._PageIndex-1)).Take(this._PageSize).ToList<T>(); 154 return result; 155 } 156 publicList<T>GetCurrentPage(int index) 157 { 158 _PageIndex= index; 159 returnGetCurrentPage(); 160 } 161 publicList<int>GetIndexList() 162 { 163 List<int> indexList =newList<int>(); 164 int endIndex =_PageIndex+ShowNum-1; 165 endIndex = endIndex >MaxIndex?MaxIndex: endIndex; 166 for(int i =1; i <= endIndex; i++) 167 { 168 indexList.Add(i); 169 } 170 return indexList; 171 } 172 publicList<int>GetCurrentIndexList() 173 { 174 List<int> indexList =newList<int>(); 175 if(MaxIndex<=ShowNum) 176 { 177 for(int ii =1; ii <=MaxIndex; ii++) 178 { 179 indexList.Add(ii); 180 } 181 } 182 else 183 { 184 int start =PageIndex-ShowNum/2<=1?1:PageIndex-ShowNum/2; 185 start = start +ShowNum-1>MaxIndex?MaxIndex-ShowNum+1: start; 186 for(int i = start; i <= start +ShowNum-1; i++) 187 { 188 indexList.Add(i); 189 } 190 } 191 return indexList; 192 } 193 } 194 publicclassPagingInf<T>where T :new() 195 { 196 /// <summary> 197 /// 是否成功获取数据 198 /// </summary> 199 public bool Result{ get;set;} 200 /// <summary> 201 /// 结果描述 202 /// </summary> 203 publicstringResultDes{ get;set;} 204 /// <summary> 205 /// 是否最后一页 206 /// </summary> 207 public bool IsLast{ get;set;} 208 /// <summary> 209 /// 是否第一页 210 /// </summary> 211 public bool IsFirst{ get;set;} 212 /// <summary> 213 /// 当前页码 214 /// </summary> 215 publicintPageNum{ get;set;} 216 /// <summary> 217 /// 每页显示数据数量 218 /// </summary> 219 publicintPageSize{ get;set;} 220 /// <summary> 221 /// 当前页的数据 222 /// </summary> 223 publicList<T>PageData{ get;set;} 224 /// <summary> 225 /// 页码列表 226 /// </summary> 227 publicList<int>IndexList{ get;set;} 228 /// <summary> 229 /// 记录总数 230 /// </summary> 231 publicintTotal{ get;set;} 232 publicPagingInf(object dataSource) 233 { 234 PagingHelper<T> page =newPagingHelper<T>(dataSource,1,10); 235 this.IndexList= page.GetCurrentIndexList(); 236 this.IsFirst= page.isFirst; 237 this.IsLast= page.isLast; 238 this.PageData= page.PageData; 239 this.PageNum= page.PageIndex; 240 this.PageSize= page.PageSize; 241 this.Total= page.Total; 242 this.Result=true; 243 } 244 publicPagingInf(object dataSource,int index,int size) 245 { 246 PagingHelper<T> page =newPagingHelper<T>(dataSource, index, size); 247 if(page.PageIndex> page.MaxIndex) 248 { 249 page =newPagingHelper<T>(dataSource, page.MaxIndex, size); 250 } 251 this.IndexList= page.GetCurrentIndexList(); 252 this.IsFirst= page.isFirst; 253 this.IsLast= page.isLast; 254 this.PageData= page.PageData; 255 this.PageNum= page.PageIndex; 256 this.PageSize= page.PageSize; 257 this.Total= page.Total; 258 this.Result=true; 259 } 260 publicPagingInf(List<T> pageData,int index,int size,int total) 261 { 262 PagingHelper<T> page =newPagingHelper<T>(pageData, index, size,total); 263 if(page.PageIndex> page.MaxIndex) 264 { 265 page =newPagingHelper<T>(pageData, page.MaxIndex, size, total); 266 } 267 this.IndexList= page.GetCurrentIndexList(); 268 this.IsFirst= page.isFirst; 269 this.IsLast= page.isLast; 270 this.PageData= page.PageData; 271 this.PageNum= page.PageIndex; 272 this.PageSize= page.PageSize; 273 this.Total= page.Total; 274 this.Result=true; 275 } 276 }
下面是使用方法:
方法一:查出所有记录,利用List泛型的skip和Take方法筛选数据
优点:使用简单只需要查询所有记录即可
缺点:数据量大的情况下,就不合理了
首先初始化分页类
1 publicPagingInf<RoleModel>GetPageInf(string index,string size) 2 { 3 List<RoleModel>list=GetModelList("");//获取所有记录 4 PagingInf<RoleModel> pageInf =newPagingInf<RoleModel>(list,string.IsNullOrEmpty(index)?1:int.Parse(index),string.IsNullOrEmpty(size)?10:int.Parse(size));//根据所有记录和当前页和页面数据显示条数初始化分页工具类。此处判断当前页和页面大小是否为空为空设为默认的1和10 5 return pageInf; 6 }
然后使用分页类
方法a:可以直接通过服务器代码<%%>(sapx)或者@(razor)直接循环显示
方法b:将分页工具类序列化传给前台json数据。前台利用js进行html标签的填充显示(减小服务器压力。。不过一般用不到)
方法b详解:
首先定义好数据和分页的位置
<tableclass="tablelist"style="text-align: center"> <thead> <tr> <thstyle="text-align: center"><inputtype="checkbox"onclick="checkAll(this)"/></th> <thstyle="text-align: center">角色名称</th> <thstyle="text-align: center">操作</th> </tr> </thead> <tbodyid="dataPanel"></tbody> </table> <divclass="pagin"id="paging"> <divclass="message">共<iclass="blue">0</i>条记录,当前显示第 <iclass="blue">1 </i>页</div> <ulclass="paginList"> <liclass="paginItem"><ahref="javascript:;"><spanclass="pagepre"><b><</b></span></a></li> <liclass="paginItem current"><ahref="javascript:;">1</a></li> <liclass="paginItem"><ahref="javascript:;"><spanclass="pagenxt"><b>></b></span></a></li> </ul> </div>
然后定义好每行的数据模版和分页模板
1 //数据模板 2 var _data ="<tr>"+ 3 "<td><input name="{0}" class="c_rid" rid="{1}" type="checkbox" /></td>"+ 4 "<td>{2}</td>"+ 5 "<td><a href="javascript:" class="tablelink" onclick="zUpdate({3})">编辑</a><a href="javascript:" class="tablelink" onclick="zDelete({4})"> 删除</a></td>"+ 6 "</tr>"; 7 //页码模板(前一页、后一页) 8 var _page1 ="<div class="message">共<i class="blue">{0}</i>条记录,当前显示第 <i class="blue">{1} </i>页</div>"+ 9 "<ul class="paginList">"+ 10 "<li class="paginItem"><a href="javascript:;" onclick="goPage({2})"><span class="{3}"> </span></a></li>"+ 11 "{4}"+ 12 "<li class="paginItem"><a href="javascript:;" onclick="goPage({5})"><span class="{6}" > </span></a></li>"+ 13 "</ul><input type="hidden" id="currentPage" value="{7}" />"; 14 //页码模板(每一页) 15 var _page2 ="<li class="paginItem{0}"><a href="javascript:;" onclick="goPage({1})">{2}</a></li>";
在通过ajax获取数据 并解析json,遍历json,拼接td标记。填充到tbody中进行显示
1 function getData(p, s){ 2 //显示数据的容器 3 var tbody = $('#dataPanel'); 4 //显示分页工具栏的容器 5 var paging = $("#paging"); 6 //先清空 7 tbody.empty(); 8 paging.empty(); 9 //ajax获取数据 10 $.post('/Role/GetRoleList',{'p': p,'s': s },function(data){ 11 var _temp =""; 12 var_temp_t=""; 13 var _temp_p =""; 14 if(data.Result){ 15 var op =""; 16 $.each(data.PageData,function(i, item){//数据 17 _temp += $.format(_data,'c_rid', item.rid, item.roleName, item.rid, item.rid); 18 }); 19 $.each(data.IndexList,function(i, item){//页码 20 _temp_p += $.format(_page2, item == data.PageNum?" current":"", item == data.PageNum?"no": item, item); 21 }); 22 _temp_t= $.format(_page1, data.Total, data.PageNum, data.IsFirst?"no": data.PageNum-1, data.IsFirst?"pagepre-d":"pagepre", _temp_p, data.IsLast?"no": data.PageNum+1, data.IsLast?"pagenxt-d":"pagenxt", data.PageNum);//页码 23 }else{ 24 _temp = $.format("<tr><td colspan="20" style="text-align:center;color:red;font-weight:bold">{0}</td></tr>", data.ResultDes); 25 } 26 //alert(_temp); 27 //alert(_temp_t); 28 //显示数据 29 tbody.html(_temp); 30 paging.html(_temp_t); 31 },'json'); 32 }
样式可以在模版中定义好。也可以动态根据返回数据先进性设置
方法二:从数据库直接查询出要现实的数据 和 数据总数 然后直接初始化工具类
优点:数据库直接筛选数据。可能会更加的快。。(同事给的建议,不知道具体到多大的数据量才能体现出来)
缺点:需要数据库来分页,我用的sql server 比较麻烦一点。mysql应该更简单一点吧(数据库基础好的就不算缺点了)
具体用法是一样的。只不过初始化的时候需要计算从哪里开始获取记录和从哪里结束。
1 publicPagingInf<PayModel>GetPageInf(string index,string size,string sqlWhere,string orderBy) 2 { 3 int index1 =string.IsNullOrEmpty(index)?1:int.Parse(index); 4 int size1 =string.IsNullOrEmpty(size)?10:int.Parse(size); 5 int start =((index1 -1)* size1)+1;//计算开始的索引 6 int end = start + size1-1;//计算结束的索引 7 int total =GetRecordCount("");//查询利济路总数 8 List<PayModel> list1 =GetModelListByPage(sqlWhere, orderBy, start, end); 9 PagingInf<PayModel> pageInf1 =newPagingInf<PayModel>(list1, index1, size1, total); 10 return pageInf1; 11 }