最新文章專(zhuān)題視頻專(zhuān)題問(wèn)答1問(wèn)答10問(wèn)答100問(wèn)答1000問(wèn)答2000關(guān)鍵字專(zhuān)題1關(guān)鍵字專(zhuān)題50關(guān)鍵字專(zhuān)題500關(guān)鍵字專(zhuān)題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專(zhuān)題關(guān)鍵字專(zhuān)題tag2tag3文章專(zhuān)題文章專(zhuān)題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專(zhuān)題3
問(wèn)答文章1 問(wèn)答文章501 問(wèn)答文章1001 問(wèn)答文章1501 問(wèn)答文章2001 問(wèn)答文章2501 問(wèn)答文章3001 問(wèn)答文章3501 問(wèn)答文章4001 問(wèn)答文章4501 問(wèn)答文章5001 問(wèn)答文章5501 問(wèn)答文章6001 問(wèn)答文章6501 問(wèn)答文章7001 問(wèn)答文章7501 問(wèn)答文章8001 問(wèn)答文章8501 問(wèn)答文章9001 問(wèn)答文章9501
當(dāng)前位置: 首頁(yè) - 科技 - 知識(shí)百科 - 正文

MVC HtmlHelper擴(kuò)展類(lèi)(PagingHelper)實(shí)現(xiàn)分頁(yè)功能

來(lái)源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-27 22:35:43
文檔

MVC HtmlHelper擴(kuò)展類(lèi)(PagingHelper)實(shí)現(xiàn)分頁(yè)功能

MVC HtmlHelper擴(kuò)展類(lèi)(PagingHelper)實(shí)現(xiàn)分頁(yè)功能:MVC HtmlHelper擴(kuò)展類(lèi)PagingHelper實(shí)現(xiàn)分頁(yè)功能,供大家參考,具體內(nèi)容如下 using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; using System.T
推薦度:
導(dǎo)讀MVC HtmlHelper擴(kuò)展類(lèi)(PagingHelper)實(shí)現(xiàn)分頁(yè)功能:MVC HtmlHelper擴(kuò)展類(lèi)PagingHelper實(shí)現(xiàn)分頁(yè)功能,供大家參考,具體內(nèi)容如下 using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; using System.T

MVC HtmlHelper擴(kuò)展類(lèi)PagingHelper實(shí)現(xiàn)分頁(yè)功能,供大家參考,具體內(nèi)容如下

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace HtmlHelperMvc.Models
{
 /// <summary>
 /// 分頁(yè)類(lèi)如果一個(gè)頁(yè)面顯示兩個(gè)列表只需要復(fù)制該類(lèi)到項(xiàng)目中重命名一個(gè)就可以
 /// </summary>
 public static class PagingHelper
 {
 #region 屬性Property
 /// <summary>
 /// 當(dāng)前頁(yè)碼
 /// </summary>
 private static int? _currentPage = null;
 /// <summary>
 /// 當(dāng)前頁(yè)碼
 /// </summary>
 public static int CurrentPage
 {
 get
 {
 return _currentPage ?? 1;
 }
 set
 {
 _currentPage = value;
 }
 }
 /// <summary>
 /// 每頁(yè)記錄條數(shù)
 /// </summary>
 private static int? _pageSize = null;
 /// <summary>
 /// 每頁(yè)記錄條數(shù)
 /// </summary>
 public static int PageSize
 {
 get
 {
 return _pageSize ?? 15;
 }
 set
 {
 _pageSize = value;
 }
 }
 /// <summary>
 /// 是否顯示上一頁(yè)
 /// </summary>
 public static bool HasPreviousPage
 {
 get
 {
 return (CurrentPage > 1);
 }
 }
 /// <summary>
 /// 是否顯示下一頁(yè)
 /// </summary>
 public static bool HasNextPage
 {
 get
 {
 return (CurrentPage < TotalPages);
 }
 }
 /// <summary>
 /// 當(dāng)前頁(yè):
 /// </summary>
 public static string CurrentPageDisplayName { get; set; }
 /// <summary>
 /// 每頁(yè)顯示:
 /// </summary>
 public static string PageSizeDisplayName { get; set; }
 public static string FirstDisplayName { get; set; }
 public static string PreDisplayName { get; set; }
 public static string NextDisplayName { get; set; }
 public static string LastDisplayName { get; set; }
 public static string TotalCountDisplayName { get; set; }
 public static string TotalPagesDisplayName { get; set; }
 /// <summary>
 /// 總條數(shù)
 /// </summary>
 public static int TotalCount
 {
 get;
 set;
 }
 public static int TotalPages
 {
 get
 {
 return (int)Math.Ceiling(TotalCount / (double)PageSize);
 //return (TotalCount % PageSize == 0 ? TotalCount / PageSize : TotalCount / PageSize + 1);
 }
 }
 /// <summary>
 /// 設(shè)置分頁(yè)url eg:/Admin/Product/Index
 /// </summary>
 public static string PagingUrl
 {
 get;
 set;
 }
 /// <summary>
 /// 默認(rèn)page,設(shè)置分頁(yè)參數(shù)名 eg:/Admin/Product/Index?PagingParamName=1
 /// </summary>
 public static string PagingParamName
 {
 get;
 set;
 }
 #endregion
 #region Paging String
 /// <summary>
 /// MVC分頁(yè) 如果用jquery分頁(yè)只需要class不需要href,用以下實(shí)現(xiàn):
 /// $(".class值").live("click", function () {
 /// var page = $(this).attr("pagingParamName值");
 /// $("#order").html("").load("/Customer/Order?page="+page);
 /// });live自動(dòng)給遍歷增加事件
 /// </summary>
 /// <param name="html"></param>
 /// <param name="htmlAttributes">new {@class="grey",pagingParamName="page",href="/Admin/Product/Index" rel="external nofollow" } pagingParamName默認(rèn)page,匿名類(lèi)添加控件屬性</param>
 /// <returns></returns>
 public static MvcHtmlString Paging(this System.Web.Mvc.HtmlHelper html, object htmlAttributes)
 {
 RouteValueDictionary values = new RouteValueDictionary(htmlAttributes);
 #region 屬性賦值
 if (values["href"] != null)
 {
 PagingUrl = values["href"].ToString();
 }
 if (values["pagingParamName"] != null)
 {
 PagingParamName = values["pagingParamName"].ToString();
 values.Remove("pagingParamName");
 }
 else
 {
 PagingParamName = "page";
 }
 #endregion
 #region 分頁(yè)最外層div/span
 TagBuilder builder = new TagBuilder("div");//span
 //創(chuàng)建Id,注意要先設(shè)置IdAttributeDotReplacement屬性后再執(zhí)行GenerateId方法. 
 //builder.IdAttributeDotReplacement = "_";
 //builder.GenerateId(id);
 //builder.AddCssClass("");
 //builder.MergeAttributes(values);
 builder.InnerHtml = PagingBuilder(values);
 #endregion
 return MvcHtmlString.Create(builder.ToString(TagRenderMode.Normal));//解決直接顯示html標(biāo)記
 }
 private static string PagingBuilder(RouteValueDictionary values)
 {
 #region 條件搜索時(shí)包括其他參數(shù)
 StringBuilder urlParameter = new StringBuilder();
 NameValueCollection collection = HttpContext.Current.Request.QueryString;
 string[] keys = collection.AllKeys;
 for (int i = 0; i < keys.Length; i++)
 {
 if (keys[i].ToLower() != "page")
 {
 urlParameter.AppendFormat("&{0}={1}", keys[i], collection[keys[i]]);
 }
 }
 #endregion
 //CurrentPage = Convert.ToInt32(HttpContext.Current.Request.QueryString["page"] ?? "0");
 StringBuilder sb = new StringBuilder();
 #region 分頁(yè)統(tǒng)計(jì)
 sb.AppendFormat("Total  {0}   Records Page  {1} of  {2}   ", TotalCount, CurrentPage, TotalPages);
 #endregion
 #region 首頁(yè) 上一頁(yè)
 sb.AppendFormat(TagBuilder(values, 1, " First"));
 //sb.AppendFormat("<a href={0}?page=1{1}>First</a> ",url,urlParameter);
 if (HasPreviousPage)
 {
 sb.AppendFormat(TagBuilder(values, CurrentPage - 1, " Prev "));
 //sb.AppendFormat("<a href={0}?page={1}{2}>Prev</a> ", url, CurrentPage - 1, urlParameter);
 }
 #endregion
 #region 分頁(yè)邏輯
 if (TotalPages > 10)
 {
 if ((CurrentPage + 5) < TotalPages)
 {
 if (CurrentPage > 5)
 {
 for (int i = CurrentPage - 5; i <= CurrentPage + 5; i++)
 {
 sb.Append(TagBuilder(values, i, i.ToString()));
 }
 }
 else
 {
 for (int i = 1; i <= 10; i++)
 {
 sb.Append(TagBuilder(values, i, i.ToString()));
 }
 }
 sb.Append("... ");
 }
 else
 {
 for (int i = CurrentPage - 10; i <= TotalPages; i++)
 {
 sb.Append(TagBuilder(values, i, i.ToString()));
 }
 }
 }
 else
 {
 for (int i = 1; i <= TotalPages; i++)
 {
 sb.Append(" " + TagBuilder(values, i, i.ToString()) + " ");
 }
 }
 #endregion
 #region 下一頁(yè) 末頁(yè)
 if (HasNextPage)
 {
 sb.AppendFormat(TagBuilder(values, CurrentPage + 1, "Next"));
 //sb.AppendFormat("<a href={0}?page={1}{2}>Next</a> ", url, CurrentPage + 1, urlParameter);
 }
 sb.AppendFormat(TagBuilder(values, TotalPages, "Last"));
 //sb.AppendFormat("<a href={0}?page={1}{2}>Last</a>",url,TotalPages,urlParameter);
 #endregion
 return sb.ToString();
 }
 private static string TagBuilder(RouteValueDictionary values, int i, string innerText)
 {
 values[PagingParamName] = i;
 TagBuilder tag = new TagBuilder("a");
 if (PagingUrl != null)
 {
 values["href"] = PagingUrl + "?" + PagingParamName + "= " + i + "   ";
 }
 if (CurrentPage == i && innerText != " First" && innerText != " Last")
 {
 values["id"] = "on";
 }
 else
 {
 tag.Attributes["id"] = "";
 }
 tag.MergeAttributes(values);
 tag.SetInnerText(innerText);
 return tag.ToString();
 }
 #endregion
 }
}

后臺(tái)Controller代碼

//
// GET: /Home/

public ActionResult Index(int? page)
{
 page = page ?? 1;
 PagingHelper.CurrentPage = Convert.ToInt32(page);
 PagingHelper.PageSize = 20;

 //{獲取數(shù)據(jù)集的中條數(shù),以及分頁(yè)的數(shù)據(jù)集}

 PagingHelper.TotalCount = 2000;
 return View();
}

前臺(tái)頁(yè)面代碼

@{
 ViewBag.Title = "Index";
}
@using HtmlHelperMvc.Models;
<h2>Index</h2>
<hr />
<style type="text/css">
 #on
 {
 color: #FFF;
 background-color: #337AB7;
 border-color: #337AB7;
 }

 .pagination a
 {
 margin-right: 3px;
 padding: 5px 10px;
 font-size: 12px;
 text-decoration: none;
 background-color: #fff;
 border: 1px solid #ddd;
 cursor: pointer;
 display: inline-block;
 border-radius: 3px;
 }

 a
 {
 color: #337ab7;
 text-decoration: none;
 }

 a
 {
 background-color: transparent;
 }

 *
 {
 -webkit-box-sizing: border-box;
 -moz-box-sizing: border-box;
 box-sizing: border-box;
 }
</style>
<script src="~/Scripts/jquery-1.8.2.js"></script>
<script type="text/javascript">
 $(function () {
 $(".pagination .active").live("click", function () {
 $("#page").val($(this).attr("page"));
 $("#form_Submit").submit();
 });
 });
</script>
<form id="form_Submit" action="/Home/Index" method="post">
 <div class="fix">
 <div class="page">
 <div class="pagination pagination-sm pull-right" id="pageDiv" style="margin: 0px 0;">
 <input type="hidden" id="page" name="page" value="@PagingHelper.CurrentPage" />
 @Html.Paging(new { @class = "active" })
 </div>
 </div>
 </div>
</form>

最終效果圖:

聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

MVC HtmlHelper擴(kuò)展類(lèi)(PagingHelper)實(shí)現(xiàn)分頁(yè)功能

MVC HtmlHelper擴(kuò)展類(lèi)(PagingHelper)實(shí)現(xiàn)分頁(yè)功能:MVC HtmlHelper擴(kuò)展類(lèi)PagingHelper實(shí)現(xiàn)分頁(yè)功能,供大家參考,具體內(nèi)容如下 using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; using System.T
推薦度:
標(biāo)簽: page mvc 分頁(yè)類(lèi)
  • 熱門(mén)焦點(diǎn)

最新推薦

猜你喜歡

熱門(mén)推薦

專(zhuān)題
Top