扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
轻量级MVC分页控件JPager.Net是怎样的,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
创新互联主营淳安网站建设的网络公司,主营网站建设方案,APP应用开发,淳安h5微信小程序定制开发搭建,淳安网站营销推广欢迎淳安等地区企业咨询JPager.Net MVC好用的轻量级分页控件,好用到你无法想象,轻量到你无法想象。
JPager.Net MVC好用的轻量级分页控件,实现非常简单,使用也非常简单。
JPager.Net MVC好用的轻量级分页控件,代码精心推敲,经多人反复建议修改,最终成型使用中。非常好用分享给大家。源代码一共放出来。先上个效果图:
JPager.Net MVC好用的轻量级分页控件JPager.Net .dll核心代码
PagerInBase.cs
namespace JPager.Net { ////// 分页基础类 /// public class PagerInBase { ////// 当前页 /// public int PageIndex { get; set; } ////// 页数 /// public int PageSize { get; set; } //跳过序列中指定数量的元素 public int Skip => (PageIndex - 1) * PageSize; ////// 请求URL /// public string RequetUrl => System.Web.HttpContext.Current.Request.Url.OriginalString; ////// 构造函数给当前页和页数初始化 /// public PagerInBase() { if (PageIndex == 0) PageIndex = 1; if (PageSize == 0) PageSize = 10; } } }
PagerResult.cs
using System; using System.Collections.Generic; using System.Text; namespace JPager.Net { ////// ULR拼装 /// internal static class Exts { public static string GetUrl(this string url, int curIndex, int reps) { return url.Replace("pageindex=" + curIndex.ToString(), "pageindex=" + reps.ToString()); } } ////// 分页核心代码 /// ///public class PagerResult { public int Code { get; set; } public int Total { get; set; } public IEnumerable DataList { get; set; } public int PageSize { get; set; } public int PageIndex { get; set; } public string RequestUrl { get; set; } /// /// 分页页码Html /// /// 默认样式:jpager ///public string PagerHtml(string cssClass="jpager") { if (PageIndex == 0) PageIndex = 1; if (RequestUrl.IndexOf("?", StringComparison.Ordinal) == -1) RequestUrl += "?pageindex=1"; else if (RequestUrl.IndexOf("&pageindex", StringComparison.Ordinal) == -1&& RequestUrl.IndexOf("?pageindex", StringComparison.Ordinal) == -1) RequestUrl += "&pageindex=1"; var html = new StringBuilder(); html.AppendFormat("", cssClass); var pageLen = Math.Ceiling((double)Total / PageSize); html.AppendFormat(" 首页 ", RequestUrl.GetUrl(PageIndex,1)); html.AppendFormat(" 上页 ", RequestUrl.GetUrl(PageIndex, PageIndex < 2 ? 1 : PageIndex - 1)); var si = PageIndex <= 6 ? 1 : PageIndex - 5; var ei = si + 9; while (si <= pageLen && si <= ei) html.AppendFormat( si == PageIndex ? " {1} " : " {1} ", RequestUrl.GetUrl(PageIndex, si), si++); html.AppendFormat(" 下页 ", RequestUrl.GetUrl(PageIndex, (int)(PageIndex > pageLen - 1 ? pageLen : PageIndex + 1))); html.AppendFormat(" 尾页 ", Math.Abs(Total) <= 0 ? RequestUrl.GetUrl(PageIndex, 1) : RequestUrl.GetUrl(PageIndex, (int) pageLen)); html.Append(@""); return html.ToString(); } } }
使用方法:
HomeController.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web.Mvc; using JPager.Net; using JPager.Net.Web.Models; namespace JPager.Net.Web.Controllers { public class HomeController : Controller { public ActionResult Index(UserParams param) { //每页显示的条数默认10 //param.PageSize = 10; //保存搜索条件 ViewBag.SearchName = param.Name; ViewBag.SearchAge = param.Age; //获取数据集合 var list = PageContent(); //根据条件检索 var query = param.Name!=null ? list.Where(t=>t.Name.Contains(param.Name)).ToList() : list.ToList(); //分页数据 var data = query.Skip(param.Skip).Take(param.PageSize); //总页数 var count = query.Count; var res = new PagerResult{ Code = 0, DataList = data, Total = count, PageSize = param.PageSize,PageIndex = param.PageIndex,RequestUrl = param.RequetUrl}; return View(res); } //测试数据 public List PageContent() { var list = new List (); for (var t = 0; t < 10000; t++) { list.Add(new User { Id = t, Name = "Joye.net"+t.ToString(), Age = t + 10, Score = t, Address = "http://yinrq.cnblogs.com/", AddTime = DateTime.Now }); } return list; } } }
Models文件夹建User.cs和UserParams.cs
User.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace JPager.Net.Web.Models { public class UserParams:JPager.Net.PagerInBase { public int Id { get; set; } public string Name { get; set; } public int ? Age { get; set; } public int Score { get; set; } public string Address { get; set; } public DateTime AddTime { get; set; } } }
UserParams.cs
using System; namespace JPager.Net.Web.Models { public class User { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } public int Score { get; set; } public string Address { get; set; } public DateTime AddTime { get; set; } } }
view显示
@model JPager.Net.PagerResult@{ ViewBag.Title = "Index"; } JPager.Net MVC好用的轻量级分页控件
@foreach (JPager.Net.Web.Models.User item in Model.DataList) { ID Name Age Score Address AddTime } @item.Id @item.Name @item.Age @item.Score @item.Address @item.AddTime @Html.Raw(Model.PagerHtml()) 共 @Model.Total 条
github:https://github.com/decadestory/JPager.Net
关于轻量级MVC分页控件JPager.Net是怎样的问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流