您好,登錄后才能下訂單哦!
輕量級(jí)MVC分頁控件JPager.Net是怎樣的,針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。
JPager.Net MVC好用的輕量級(jí)分頁控件,好用到你無法想象,輕量到你無法想象。
JPager.Net MVC好用的輕量級(jí)分頁控件,實(shí)現(xiàn)非常簡單,使用也非常簡單。
JPager.Net MVC好用的輕量級(jí)分頁控件,代碼精心推敲,經(jīng)多人反復(fù)建議修改,最終成型使用中。非常好用分享給大家。源代碼一共放出來。先上個(gè)效果圖:
JPager.Net MVC好用的輕量級(jí)分頁控件JPager.Net .dll核心代碼
PagerInBase.cs
namespace JPager.Net { /// <summary> /// 分頁基礎(chǔ)類 /// </summary> public class PagerInBase { /// <summary> /// 當(dāng)前頁 /// </summary> public int PageIndex { get; set; } /// <summary> /// 頁數(shù) /// </summary> public int PageSize { get; set; } //跳過序列中指定數(shù)量的元素 public int Skip => (PageIndex - 1) * PageSize; /// <summary> /// 請(qǐng)求URL /// </summary> public string RequetUrl => System.Web.HttpContext.Current.Request.Url.OriginalString; /// <summary> /// 構(gòu)造函數(shù)給當(dāng)前頁和頁數(shù)初始化 /// </summary> 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 { /// <summary> /// ULR拼裝 /// </summary> internal static class Exts { public static string GetUrl(this string url, int curIndex, int reps) { return url.Replace("pageindex=" + curIndex.ToString(), "pageindex=" + reps.ToString()); } } /// <summary> /// 分頁核心代碼 /// </summary> /// <typeparam name="T"></typeparam> public class PagerResult<T> { public int Code { get; set; } public int Total { get; set; } public IEnumerable<T> DataList { get; set; } public int PageSize { get; set; } public int PageIndex { get; set; } public string RequestUrl { get; set; } /// <summary> /// 分頁頁碼Html /// </summary> /// <param name="cssClass">默認(rèn)樣式:jpager</param> /// <returns></returns> 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("<span class='{0}'>", cssClass); var pageLen = Math.Ceiling((double)Total / PageSize); html.AppendFormat("<a href='{0}'> 首頁 </a>", RequestUrl.GetUrl(PageIndex,1)); html.AppendFormat("<a href='{0}'> 上頁 </a>", 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 ? "<a style='color:black;border:none;' href='{0}'> {1} </a>" : "<a href='{0}'> {1} </a>", RequestUrl.GetUrl(PageIndex, si), si++); html.AppendFormat("<a href='{0}'> 下頁 </a>", RequestUrl.GetUrl(PageIndex, (int)(PageIndex > pageLen - 1 ? pageLen : PageIndex + 1))); html.AppendFormat("<a href='{0}'> 尾頁 </a>", Math.Abs(Total) <= 0 ? RequestUrl.GetUrl(PageIndex, 1) : RequestUrl.GetUrl(PageIndex, (int) pageLen)); html.Append(@"</span>"); 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) { //每頁顯示的條數(shù)默認(rèn)10 //param.PageSize = 10; //保存搜索條件 ViewBag.SearchName = param.Name; ViewBag.SearchAge = param.Age; //獲取數(shù)據(jù)集合 var list = PageContent(); //根據(jù)條件檢索 var query = param.Name!=null ? list.Where(t=>t.Name.Contains(param.Name)).ToList() : list.ToList(); //分頁數(shù)據(jù) var data = query.Skip(param.Skip).Take(param.PageSize); //總頁數(shù) var count = query.Count; var res = new PagerResult<User> { Code = 0, DataList = data, Total = count, PageSize = param.PageSize,PageIndex = param.PageIndex,RequestUrl = param.RequetUrl}; return View(res); } //測(cè)試數(shù)據(jù) public List<User> PageContent() { var list = new List<User>(); 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<JPager.Net.Web.Models.User> @{ ViewBag.Title = "Index"; } <h3>JPager.Net MVC好用的輕量級(jí)分頁控件</h3> <div> <div> <form method="get">Name: <input name="Name" id="Name" /> Age: <input name="Age" id="Age"/> <input type="submit" value="查詢" /> </form> </div> <table> <tr> <th>ID</th> <th>Name</th> <th>Age</th> <th>Score</th> <th>Address</th> <th>AddTime</th> </tr> @foreach (JPager.Net.Web.Models.User item in Model.DataList) { <tr> <td>@item.Id</td> <td>@item.Name</td> <td>@item.Age</td> <td>@item.Score</td> <td><a href="@item.Address" target="_target">@item.Address</a></td> <td>@item.AddTime</td> </tr> } </table> </div> <div> @Html.Raw(Model.PagerHtml()) 共 @Model.Total 條 </div> <script type="text/javascript"> //保持搜索條件 $(function () { $('#Name').val('@ViewBag.SearchName'); $('#Age').val('@ViewBag.SearchAge'); }); </script>
github:https://github.com/decadestory/JPager.Net
關(guān)于輕量級(jí)MVC分頁控件JPager.Net是怎樣的問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。