JPager.Net MVC好用的輕量級(jí)分頁(yè)控件,好用到你無(wú)法想象,輕量到你無(wú)法想象。
JPager.Net MVC好用的輕量級(jí)分頁(yè)控件,實(shí)現(xiàn)非常簡(jiǎn)單,使用也非常簡(jiǎn)單。
JPager.Net MVC好用的輕量級(jí)分頁(yè)控件,代碼精心推敲,經(jīng)多人反復(fù)建議修改,最終成型使用中。非常好用分享給大家。源代碼一共放出來(lái)。先上個(gè)效果圖:
JPager.Net MVC好用的輕量級(jí)分頁(yè)控件JPager.Net .dll核心代碼
PagerInBase.cs
namespace JPager.Net
{
/// summary>
/// 分頁(yè)基礎(chǔ)類
/// /summary>
public class PagerInBase
{
/// summary>
/// 當(dāng)前頁(yè)
/// /summary>
public int PageIndex { get; set; }
/// summary>
/// 頁(yè)數(shù)
/// /summary>
public int PageSize { get; set; }
//跳過(guò)序列中指定數(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)前頁(yè)和頁(yè)數(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>
/// 分頁(yè)核心代碼
/// /summary>
/// typeparam name="T">/typeparam>
public class PagerResultT>
{
public int Code { get; set; }
public int Total { get; set; }
public IEnumerableT> DataList { get; set; }
public int PageSize { get; set; }
public int PageIndex { get; set; }
public string RequestUrl { get; set; }
/// summary>
/// 分頁(yè)頁(yè)碼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}'> 首頁(yè) /a>", RequestUrl.GetUrl(PageIndex,1));
html.AppendFormat("a href='{0}'> 上頁(yè) /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}'> 下頁(yè) /a>", RequestUrl.GetUrl(PageIndex, (int)(PageIndex > pageLen - 1 ? pageLen : PageIndex + 1)));
html.AppendFormat("a href='{0}'> 尾頁(yè) /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)
{
//每頁(yè)顯示的條數(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();
//分頁(yè)數(shù)據(jù)
var data = query.Skip(param.Skip).Take(param.PageSize);
//總頁(yè)數(shù)
var count = query.Count;
var res = new PagerResultUser> { Code = 0, DataList = data, Total = count,
PageSize = param.PageSize,PageIndex = param.PageIndex,RequestUrl = param.RequetUrl};
return View(res);
}
//測(cè)試數(shù)據(jù)
public ListUser> PageContent()
{
var list = new ListUser>();
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.PagerResultJPager.Net.Web.Models.User>
@{
ViewBag.Title = "Index";
}
h2>JPager.Net MVC好用的輕量級(jí)分頁(yè)控件/h2>
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
您可能感興趣的文章:- Java簡(jiǎn)單實(shí)現(xiàn)SpringMVC+MyBatis分頁(yè)插件
- ASP.NET MVC 5使用X.PagedList.Mvc進(jìn)行分頁(yè)教程(PagedList.Mvc)
- MVC+jQuery.Ajax異步實(shí)現(xiàn)增刪改查和分頁(yè)
- MVC分頁(yè)之MvcPager使用詳解
- SpringMvc+Mybatis+Pagehelper分頁(yè)詳解
- springmvc 分頁(yè)查詢的簡(jiǎn)單實(shí)現(xiàn)示例代碼
- 基于SpringMVC+Bootstrap+DataTables實(shí)現(xiàn)表格服務(wù)端分頁(yè)、模糊查詢
- ASP.NET MVC4 HtmlHelper擴(kuò)展類,實(shí)現(xiàn)分頁(yè)功能
- ASP.NET MVC分頁(yè)和排序功能實(shí)現(xiàn)
- MVC生成頁(yè)碼選擇器返回HTML代碼詳解