上一节我们讲了,DataGrid获取数据的一些用法,这一节讲DataGrid的分页
- DataGrid将传递“当前页码”“每页条数”
- 控制器获取参数之后交给BLL层处理逻辑和分页,返回总页数和当前页的数据
- 最后交给DataGrid处理
课外:从此次前端的处理速度我看出,没有以前的JQGrid控件的处理数据快。
首先在让DataGrid支持分页,我们需要加入几个属性
- 是否启用分页:pagination 默认是false
- 每页数量:pageSize 默认10
- 可选择每页数量:pageList 默认[10,20,30,40,50]
- 排序字段:sortName 默认null
- 排序类型:sortOrder 默认asc
OK加入后的代码变成这样
$(function () { $('#List').datagrid({ url: '/SysSample/GetList', $(window).width() - 10, methord: 'post', height: $(window).height() - 35, fitColumns: true, sortName: 'Id', sortOrder: 'desc', idField: 'Id', pageSize: 15, pageList: [15, 20, 30, 40, 50], pagination: true, striped: true, //奇偶行是否区分 singleSelect: true,//单选模式 rownumbers: true,//行号 columns: [[ { field: 'Id', title: 'ID', 80 }, { field: 'Name', title: '名称', 120 }, { field: 'Age', title: '年龄', 80, align: 'right' }, { field: 'Bir', title: '生日', 80, align: 'right' }, { field: 'Photo', title: '照片', 250 }, { field: 'Note', title: '说明', 60, align: 'center' }, { field: 'CreateTime', title: '创建时间', 60, align: 'center' } ]] }); }); Index
$(function () { $('#List').datagrid({ url: '/SysSample/GetList', $(window).width() - 10, methord: 'post', height: $(window).height() - 35, fitColumns: true, sortName: 'Id', sortOrder: 'desc', idField: 'Id', pageSize: 15, pageList: [15, 20, 30, 40, 50], pagination: true, striped: true, //奇偶行是否区分 singleSelect: true,//单选模式 rownumbers: true,//行号 columns: [[ { field: 'Id', title: 'ID', 80 }, { field: 'Name', title: '名称', 120 }, { field: 'Age', title: '年龄', 80, align: 'right' }, { field: 'Bir', title: '生日', 80, align: 'right' }, { field: 'Photo', title: '照片', 250 }, { field: 'Note', title: '说明', 60, align: 'center' }, { field: 'CreateTime', title: '创建时间', 60, align: 'center' } ]] }); });
预览一下
实际已经分页,但是不正确的,每一页的数据一样。我们要根据分页的参数去取
我们打开谷歌浏览器,看他传递了什么
OK我们控制器要根据他传递的参数写同样的参数名称,否则获取不到
看我们的SysSampleController 下的GetList方法,加入参数
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using App.BLL; using App.IBLL; using App.Models; using App.Models.Sys; using Microsoft.Practices.Unity; namespace App.Admin.Controllers { public class SysSampleController : Controller { // // GET: /SysSample/ /// <summary> /// 业务层注入 /// </summary> [Dependency] public ISysSampleBLL m_BLL { get; set; } public ActionResult Index() { return View(); } [HttpPost] public JsonResult GetList(int page = 1, int rows = 10, string sort = "Id", string order = "desc") { int total = 0; List<SysSampleModel> list = m_BLL.GetList(page, rows, sort, order, ref total); var json = new { total = total, rows = (from r in list select new SysSampleModel() { Id = r.Id, Name = r.Name, Age = r.Age, Bir = r.Bir, Photo = r.Photo, Note = r.Note, CreateTime = r.CreateTime, }).ToArray() }; return Json(json, JsonRequestBehavior.AllowGet); } } } SysSampleController
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using App.BLL; using App.IBLL; using App.Models; using App.Models.Sys; using Microsoft.Practices.Unity; namespace App.Admin.Controllers { public class SysSampleController : Controller { // // GET: /SysSample/ /// <summary> /// 业务层注入 /// </summary> [Dependency] public ISysSampleBLL m_BLL { get; set; } public ActionResult Index() { return View(); } [HttpPost] public JsonResult GetList(int page = 1, int rows = 10, string sort = "Id", string order = "desc") { int total = 0; List<SysSampleModel> list = m_BLL.GetList(page, rows, sort, order, ref total); var json = new { total = total, rows = (from r in list select new SysSampleModel() { Id = r.Id, Name = r.Name, Age = r.Age, Bir = r.Bir, Photo = r.Photo, Note = r.Note, CreateTime = r.CreateTime, }).ToArray() }; return Json(json, JsonRequestBehavior.AllowGet); } } }
BLL代码修改(IBLL也要修改参数 List<SysSampleModel> GetList(int page, int rows, string sort, string order,ref int total);)
using System; using System.Collections.Generic; using System.Linq; using Microsoft.Practices.Unity; using App.Models; using App.Common; using App.Models.Sys; using App.IBLL; using App.IDAL; namespace App.BLL { public class SysSampleBLL : ISysSampleBLL { DBContainer db = new DBContainer(); [Dependency] public ISysSampleRepository Rep { get; set; } /// <summary> /// 获取列表 /// </summary> /// <param name="pager">JQgrid分页</param> /// <param name="queryStr">搜索条件</param> /// <returns>列表</returns> public List<SysSampleModel> GetList(int page, int rows, string sort, string order, ref int total) { IQueryable<SysSample> queryData = null; queryData = Rep.GetList(db); //排序 if (order == "desc") { switch (order) { case "Id": queryData = queryData.OrderByDescending(c => c.Id); break; case "Name": queryData = queryData.OrderByDescending(c => c.Name); break; default: queryData = queryData.OrderByDescending(c => c.CreateTime); break; } } else { switch (order) { case "Id": queryData = queryData.OrderBy(c => c.Id); break; case "Name": queryData = queryData.OrderBy(c => c.Name); break; default: queryData = queryData.OrderBy(c => c.CreateTime); break; } } return CreateModelList(ref queryData,page,rows,ref total); } private List<SysSampleModel> CreateModelList(ref IQueryable<SysSample> queryData,int page,int rows,ref int total) { total = queryData.Count(); if (total > 0) { if (page <= 1) { queryData = queryData.Take(rows); } else { queryData = queryData.Skip((page - 1) * rows).Take(rows); } } List<SysSampleModel> modelList = (from r in queryData select new SysSampleModel { Id = r.Id, Name = r.Name, Age = r.Age, Bir = r.Bir, Photo = r.Photo, Note = r.Note, CreateTime = r.CreateTime, }).ToList(); return modelList; } /// <summary> /// 创建一个实体 /// </summary> /// <param name="errors">持久的错误信息</param> /// <param name="model">模型</param> /// <returns>是否成功</returns> public bool Create( SysSampleModel model) { try { SysSample entity = Rep.GetById(model.Id); if (entity != null) { return false; } entity = new SysSample(); entity.Id = model.Id; entity.Name = model.Name; entity.Age = model.Age; entity.Bir = model.Bir; entity.Photo = model.Photo; entity.Note = model.Note; entity.CreateTime = model.CreateTime; if (Rep.Create(entity) == 1) { return true; } else { return false; } } catch (Exception ex) { //ExceptionHander.WriteException(ex); return false; } } /// <summary> /// 删除一个实体 /// </summary> /// <param name="errors">持久的错误信息</param> /// <param name="id">id</param> /// <returns>是否成功</returns> public bool Delete(string id) { try { if (Rep.Delete(id) == 1) { return true; } else { return false; } } catch (Exception ex) { return false; } } /// <summary> /// 修改一个实体 /// </summary> /// <param name="errors">持久的错误信息</param> /// <param name="model">模型</param> /// <returns>是否成功</returns> public bool Edit(SysSampleModel model) { try { SysSample entity = Rep.GetById(model.Id); if (entity == null) { return false; } entity.Name = model.Name; entity.Age = model.Age; entity.Bir = model.Bir; entity.Photo = model.Photo; entity.Note = model.Note; if (Rep.Edit(entity) == 1) { return true; } else { return false; } } catch (Exception ex) { //ExceptionHander.WriteException(ex); return false; } } /// <summary> /// 判断是否存在实体 /// </summary> /// <param name="id">主键ID</param> /// <returns>是否存在</returns> public bool IsExists(string id) { if (db.SysSample.SingleOrDefault(a => a.Id == id) != null) { return true; } return false; } /// <summary> /// 根据ID获得一个实体 /// </summary> /// <param name="id">id</param> /// <returns>实体</returns> public SysSampleModel GetById(string id) { if (IsExist(id)) { SysSample entity = Rep.GetById(id); SysSampleModel model = new SysSampleModel(); model.Id = entity.Id; model.Name = entity.Name; model.Age = entity.Age; model.Bir = entity.Bir; model.Photo = entity.Photo; model.Note = entity.Note; model.CreateTime = entity.CreateTime; return model; } else { return new SysSampleModel(); } } /// <summary> /// 判断一个实体是否存在 /// </summary> /// <param name="id">id</param> /// <returns>是否存在 true or false</returns> public bool IsExist(string id) { return Rep.IsExist(id); } } } SysSampleBLL
我们要在BLL层返回当前查询的全部条数,还要返回当前页得数据
这里我们用到“ref”关键字:传递引用,简单理解下,我们把一个参数count传递给BLL,BLL再把它传递给DAL,假设count=0加入ref在BLL处理是10,那么返回就是
10如果没有加ref那么count还是0,这等于一个持久的可赋值的变量,现在可以分页和返回查询条件的条数了
到DataGrid浏览一下,OK没问题。
大家看出这样太麻烦了,代码太不漂亮了,我们把常用的参数给封装了。这个是我封装类库
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace App.Common { public class GridPager { public int rows { get; set; }//每页行数 public int page { get; set; }//当前页是第几页 public string order { get; set; }//排序方式 public string sord { get; set; }//排序列 public int totalRows { get; set; }//总行数 public int totalPages //总页数 { get { return (int)Math.Ceiling((float)totalRows / (float)rows); } } } } GridPager
把这个类放到App.Common, 我们的App.Common终于用到了
再次修改Controller的GetList
[HttpPost] public JsonResult GetList(GridPager pager) { List<SysSampleModel> list = m_BLL.GetList(ref pager); var json = new { total = pager.totalRows, rows = (from r in list select new SysSampleModel() { Id = r.Id, Name = r.Name, Age = r.Age, Bir = r.Bir, Photo = r.Photo, Note = r.Note, CreateTime = r.CreateTime, }).ToArray() }; return Json(json, JsonRequestBehavior.AllowGet); }
和修改BLL的GetList 还要修改IBLL --List<SysSampleModel> GetList(ref GridPager pager);
using System; using System.Collections.Generic; using System.Linq; using Microsoft.Practices.Unity; using App.Models; using App.Common; using App.Models.Sys; using App.IBLL; using App.IDAL; namespace App.BLL { public class SysSampleBLL : ISysSampleBLL { DBContainer db = new DBContainer(); [Dependency] public ISysSampleRepository Rep { get; set; } /// <summary> /// 获取列表 /// </summary> /// <param name="pager">JQgrid分页</param> /// <param name="queryStr">搜索条件</param> /// <returns>列表</returns> public List<SysSampleModel> GetList(ref GridPager pager) { IQueryable<SysSample> queryData = null; queryData = Rep.GetList(db); //排序 if (pager.order == "desc") { switch (pager.order) { case "CreateTime": queryData = queryData.OrderByDescending(c => c.CreateTime); break; case "Name": queryData = queryData.OrderByDescending(c => c.Name); break; default: queryData = queryData.OrderByDescending(c => c.CreateTime); break; } } else { switch (pager.order) { case "CreateTime": queryData = queryData.OrderBy(c => c.CreateTime); break; case "Name": queryData = queryData.OrderBy(c => c.Name); break; default: queryData = queryData.OrderBy(c => c.CreateTime); break; } } return CreateModelList(ref pager, ref queryData); } private List<SysSampleModel> CreateModelList(ref GridPager pager, ref IQueryable<SysSample> queryData) { pager.totalRows = queryData.Count(); if (pager.totalRows > 0) { if (pager.page <= 1) { queryData = queryData.Take(pager.rows); } else { queryData = queryData.Skip((pager.page - 1) * pager.rows).Take(pager.rows); } } List<SysSampleModel> modelList = (from r in queryData select new SysSampleModel { Id = r.Id, Name = r.Name, Age = r.Age, Bir = r.Bir, Photo = r.Photo, Note = r.Note, CreateTime = r.CreateTime, }).ToList(); return modelList; } /// <summary> /// 创建一个实体 /// </summary> /// <param name="errors">持久的错误信息</param> /// <param name="model">模型</param> /// <returns>是否成功</returns> public bool Create( SysSampleModel model) { try { SysSample entity = Rep.GetById(model.Id); if (entity != null) { return false; } entity = new SysSample(); entity.Id = model.Id; entity.Name = model.Name; entity.Age = model.Age; entity.Bir = model.Bir; entity.Photo = model.Photo; entity.Note = model.Note; entity.CreateTime = model.CreateTime; if (Rep.Create(entity) == 1) { return true; } else { return false; } } catch (Exception ex) { //ExceptionHander.WriteException(ex); return false; } } /// <summary> /// 删除一个实体 /// </summary> /// <param name="errors">持久的错误信息</param> /// <param name="id">id</param> /// <returns>是否成功</returns> public bool Delete(string id) { try { if (Rep.Delete(id) == 1) { return true; } else { return false; } } catch (Exception ex) { return false; } } /// <summary> /// 修改一个实体 /// </summary> /// <param name="errors">持久的错误信息</param> /// <param name="model">模型</param> /// <returns>是否成功</returns> public bool Edit(SysSampleModel model) { try { SysSample entity = Rep.GetById(model.Id); if (entity == null) { return false; } entity.Name = model.Name; entity.Age = model.Age; entity.Bir = model.Bir; entity.Photo = model.Photo; entity.Note = model.Note; if (Rep.Edit(entity) == 1) { return true; } else { return false; } } catch (Exception ex) { //ExceptionHander.WriteException(ex); return false; } } /// <summary> /// 判断是否存在实体 /// </summary> /// <param name="id">主键ID</param> /// <returns>是否存在</returns> public bool IsExists(string id) { if (db.SysSample.SingleOrDefault(a => a.Id == id) != null) { return true; } return false; } /// <summary> /// 根据ID获得一个实体 /// </summary> /// <param name="id">id</param> /// <returns>实体</returns> public SysSampleModel GetById(string id) { if (IsExist(id)) { SysSample entity = Rep.GetById(id); SysSampleModel model = new SysSampleModel(); model.Id = entity.Id; model.Name = entity.Name; model.Age = entity.Age; model.Bir = entity.Bir; model.Photo = entity.Photo; model.Note = entity.Note; model.CreateTime = entity.CreateTime; return model; } else { return new SysSampleModel(); } } /// <summary> /// 判断一个实体是否存在 /// </summary> /// <param name="id">id</param> /// <returns>是否存在 true or false</returns> public bool IsExist(string id) { return Rep.IsExist(id); } } }
代码漂亮了不少,预览一下,还是正确的效果了。
好,这一讲我们就讲到这里,我不知道这种讲解大家是否看得很反感,因为这很循序渐进,是要这样好呢?还是直接上好代码就可以了?大家响应一下