c# 后台分页 jqgrid
1 /// <summary> 2 /// 获取设备数量 3 /// </summary> 4 /// <param name="Organid">单位ID</param> 5 /// <param name="Name">模糊查询单位名称</param> 6 /// <returns></returns> 7 public IList<Devicelist> GetDeviceList(string Organid, string Name, int startIndex, int resultCount, ref int recordCount) 8 { 9 string sqlwhere = " and 1=1"; 10 if (!string.IsNullOrEmpty(Name)) 11 { 12 sqlwhere += " and s.organ_name like '%" + Name + "%' "; 13 } 14 if (!string.IsNullOrEmpty(Organid)) 15 { 16 sqlwhere += " and s.organ_id ='" + Organid + "'"; 17 } 18 19 string sql = string.Format("select decode(t.device_type,1,'电脑','平板') device_typeName,t.device_type,t.device_limit,s.organ_name,t.organ_id from sys_organization s,t_device_countlimit t where t.organ_id=s.organ_id "); 20 sql += sqlwhere+ "order by s.district_code"; 21 if (!DataAccess.IsOpened) 22 DataAccess.OpenConnection(); 23 DataTable dt = DataAccess.GetDataTable(sql.ToString()); 24 if (DataAccess.IsOpened) 25 DataAccess.CloseConnection(); 26 IList<Devicelist> list = new List<Devicelist>(); 27 if(dt.Rows.Count>0) 28 { 29 foreach(DataRow s in dt.Rows) 30 { 31 Devicelist model = new Devicelist(); 32 model.TypeName = s["device_typeName"].ToString(); 33 model.DeviceType = s["device_type"].ToString(); 34 model.limit = Convert.ToInt32(s["device_limit"]); 35 model.Organid = s["organ_id"].ToString(); 36 model.OrganName = s["organ_name"].ToString(); 37 list.Add(model); 38 } 39 } 40 recordCount = list.Count; 41 42 43 // 设置起始行和每页行数 44 IList<Devicelist> resultList = new List<Devicelist>(); 45 if (startIndex != 0 || resultCount != 0) 46 { 47 resultCount = recordCount > resultCount ? resultCount : recordCount; 48 for (int i = startIndex; i < resultCount; i++) 49 { 50 resultList.Add(list[i]); 51 } 52 } 53 else 54 { 55 for (int i = startIndex; i < recordCount; i++) 56 { 57 resultList.Add(list[i]); 58 } 59 } 60 61 return resultList; 62 63 64 }
控制器
1 /// <summary> 2 /// 设备管理列表 3 /// </summary> 4 /// <returns></returns> 5 [HttpPost] 6 public ActionResult List() 7 { 8 Devicelist model = JsonConvert.DeserializeObject<Devicelist>(Request.Form["queryJson"]); 9 10 string organid = model.Organid; 11 string OrganName = model.OrganName; 12 // 分页参数 13 int _RowsCount = 0; 14 int currentPage = Convert.ToInt32(Request.Form["page"]); //页码 15 int _PageSize = Convert.ToInt32(Request.Form["rows"]); //每页显示数 16 17 IList<Devicelist> devices = _DeviceManagerRepository.GetDeviceList(organid,OrganName, (currentPage - 1) * _PageSize, _PageSize * currentPage, ref _RowsCount); 18 19 IList<Devicelist> PageList = new List<Devicelist>(); 20 Devicelist listmodel = null; 21 22 foreach(var s in devices) 23 { 24 listmodel = new Devicelist(); 25 listmodel.Organid = s.Organid; 26 listmodel.OrganName = s.OrganName; 27 listmodel.TypeName = s.TypeName; 28 listmodel.DeviceType = s.DeviceType; 29 listmodel.limit = s.limit; 30 PageList.Add(listmodel); 31 } 32 33 PagedResult<Devicelist> page = new PagedResult<Devicelist> 34 { 35 rows = PageList.ToList(), 36 records = _RowsCount, 37 page = currentPage, 38 pagesize = _PageSize 39 }; 40 41 return Json(page); 42 }
PagedResult
1 /// <summary> 2 /// 分页结果 3 /// </summary> 4 /// <typeparam name="T"></typeparam> 5 public class PagedResult<T> 6 { 7 /// <summary> 8 /// ctor 9 /// </summary> 10 public PagedResult() 11 { 12 rows = new List<T>(); 13 } 14 15 /// <summary> 16 /// ctor with params 17 /// </summary> 18 /// <param name="pageIndex">当前页码</param> 19 /// <param name="pageSize">每页显示数量</param> 20 public PagedResult(int pageIndex, int pageSize) 21 { 22 page = pageIndex; 23 pagesize = pageSize; 24 } 25 26 /// <summary> 27 /// 总记录数 28 /// </summary> 29 public int records { set; get; } 30 /// <summary> 31 /// 当前页的所有项 32 /// </summary> 33 public IList<T> rows { set; get; } 34 /// <summary> 35 /// 当前页 36 /// </summary> 37 public int page { set; get; } 38 /// <summary> 39 /// 页大小 40 /// </summary> 41 public int pagesize { set; get; } 42 /// <summary> 43 /// 页总数 44 /// </summary> 45 public int total { get { return records.CeilingDivide(pagesize); } } 46 }