在开始做mysql分页功能组件前,便设定的是要有一定可复用性。先在项目里Views文件夹下右键新建名为_PaginationComponent.cshtml,这里html及css我采用的bootstrap分页组件,这可以参考。
先将生成项目效果截图呈上:
这里有需要预先知道的,是mysql分页查询与mssql分页查询实现不同点在于,mysql支持limit语句,limit格式为 limit pageIndex*pageSize,pageSize,pageIndex--为页数,pageSize--为页面包含数据量。limit具体用法可查询mysql手册。然后需要预先定义好pageSize,pageIndex,pageCount(分页总数)三个量。这里预设pageSize为30,即: int pageSize = 30。
首先来实现获取pageCount:思路是,先将要获取的所有数据集从数据库中取出,根据pageSize = 30得出总页数,这里会遇到数据集数量为30的倍数与否问题,解决是:
MySqlCommand comm_1 = new MySqlCommand(sqlSearch, connection); MySqlDataAdapter da_1 = new MySqlDataAdapter(sqlSearch, connection); da_1.SelectCommand = comm_1; //初始化da.fill的命令 DataSet set_1 = new DataSet(); da_1.Fill(set_1); DataTable dt_1 = set_1.Tables[0]; //使用datatable装所得多张表数据,并获取里面的第一张表 Count = (double)(dt_1.Rows.Count) / 30;//分页总页数 if (Count > (int)(dt_1.Rows.Count) / 30) { pageCount = (dt_1.Rows.Count) / 30 + 1; } else if (Count == (dt_1.Rows.Count) / 30) { pageCount = (dt_1.Rows.Count) / 30; } else if (Count < (int)(dt_1.Rows.Count) / 30) { pageCount = 1; }
这里用到判断,大于30的pageCount均往上加1,小于30的pageCount为1。
接下来要实现的是用pageIndex传页数获取对应的数据集,思路是利用limit语句特性:
public List<Model> GetDormitoryBottleRecycleListPagination(int pageIndex, int pageSize) { Get_Connection(); List<Model> list = null;string sqlPaginatioSearch = "SELECT * FROM table ORDER BY file_1 ASC LIMIT " + (pageIndex - 1) * 30 + "," + pageSize + ""; //填充分页后的数据 MySqlCommand comm_2 = new MySqlCommand(sqlPaginatioSearch, connection); MySqlDataAdapter da_2 = new MySqlDataAdapter(sqlPaginatioSearch, connection); da_2.SelectCommand = comm_2; //初始化da.fill的命令 DataSet set_2 = new DataSet(); da_2.Fill(set_2); DataTable dt_2 = set_2.Tables[0]; //使用datatable装所得多张表数据,并获取里面的第一张表 if (dt_2 != null && dt_2.Rows.Count > 0) { list = new List<Model>(); foreach (DataRow row in dt_2.Rows) { Model entity = sqlhelper.CreateItem(row); list.Add(entity); } } Close_Connection(); return list; }
string sqlPaginatioSearch = "SELECT * FROM table ORDER BY file_1 ASC LIMIT " + (pageIndex - 1) * 30 + "," + pageSize + ""; //填充分页后的数据
这是核心sql语句,通过pageIndex传入页面数,从(pageIndex - 1) * 30处开始取pageSize量的数据。
在控制器的action中实现也是关键:
public ActionResult DormitoryBottleRecycleSort(int id = 1) { int pageIndex = id;//传递分页数 int pageSize = 30; int pageCount = 0; List<BottleRecycleModel> list_1; list_1 = pbsAccess.GetDormitoryBottleRecycleListPagination(pageIndex, pageSize, pageCount);//获取分页列表 ViewBag.ListCount = list_1.Count; BottleRecycleList viewBottleRecycleList = new BottleRecycleList(); viewBottleRecycleList.bottleRecycleList = new List<BottleRecycleModel>();//要实例化对象,相当重要 //这里是为显示分页数据的功能代码 if (list_1 != null) { foreach (var item in list_1) { BottleRecycleModel viewBottleRecycle = new BottleRecycleModel(); viewBottleRecycle.Id = item.Id; viewBottleRecycle.DormitoryNumber = item.DormitoryNumber; viewBottleRecycle.SmallBottleNumber = item.SmallBottleNumber; viewBottleRecycle.BigBottleNumber = item.BigBottleNumber; viewBottleRecycle.TotalBottleNumber = item.TotalBottleNumber; viewBottleRecycle.PublishTime = item.PublishTime; viewBottleRecycleList.bottleRecycleList.Add(viewBottleRecycle); } ViewBag.DormitoryBottleRecycleSort = viewBottleRecycleList.bottleRecycleList; } else { ViewBag.DormitoryBottleRecycleSort = null; } ViewBag.pageCount = pbsAccess.getDormitoryBottlePaginationPageCount(); ViewBag.pageIndex = pageIndex; return View(ViewBag); }
这里使用ViewBag进行传值,这里的getDormitoryBottlePaginationPageCount()就是上面pageCount得出的方法。这是后台的方法。
现在说一下_PaginationComponent.cshtml里该如何运用这些值。
@{ string Controllers = ViewContext.RouteData.Values["controller"].ToString(); string Actions = ViewContext.RouteData.Values["Action"].ToString(); } <li><a href="#">«</a></li> @for (int i = 1; i < @ViewBag.pageCount + 1; i++) { <li><a href="https://www.jb51.net/@Controllers/@Actions/@i">@i</a></li> } <li><a href="#">»</a></li>