zoukankan      html  css  js  c++  java
  • mvc 分页

    之前写的,很凌乱,而且,几乎不粘代码,这不便于理解和也无法直接寻找可用代码。以后的文章会尽量引入代码实例。

          这次需要一个翻页控件。之前webform直接拖个aspnetpager,写写前台样式和后台代码就好了,需要数据绑定时可以直接用pager控件的属 性,或者可以直接在sqldatasource或者entitydatasource里绑定parameter为controlparameter,指向 相应控件的属性。(代码就不找了) 

          现在MVC框架下,服务端控件被取消了,似乎分页变得复杂了不少。寻找微软提供的解决方案,没有找到。(可能是我找的不够细致)在网上看到说自己写个HtmlHelper的扩展,小研究了一下,发现不是很符合我的胃口。哈哈,那就自己整吧。

          先看下扩展:

             必须由要扩展的类型同一命名空间的下的类承载,一般定义为static class,命名为扩展功能+Extensions。对于类中的方法,应当使用static方法。方法的第一个参数为要扩展的类型,并使用this修饰 符。 还有就是,扩展方法作为静态方法,无法访问被扩展对象的非静态方法和属性。

          好了,开工。扩展参考了网上某位同仁的代码,但是代码基本上重写了,保留了思想,哈哈。忘记地址了,这里就不贴了。

          

    1. using System.Reflection;  
    2. using System.Text;  
    3. using System.Web.Routing;  
    4.   
    5. namespace System.Web.Mvc  
    6. {  
    7.     #region 分页配置类  
    8.     /// <summary>  
    9.     /// 分页元素位置  
    10.     /// </summary>  
    11.     public enum PagerElementPosition  
    12.     {  
    13.         Left,  
    14.         Right  
    15.     }  
    16.   
    17.     /// <summary>  
    18.     /// 配合Pager扩展,分页控件设置类  
    19.     /// </summary>  
    20.     public class PagerConfig  
    21.     {  
    22.         /// <summary>  
    23.         /// 记录总条数  
    24.         /// </summary>  
    25.         public int TotalRecord { getset; }  
    26.         /// <summary>  
    27.         /// 记录的单位,默认为“条”  
    28.         /// </summary>  
    29.         public string RecordUnit { getset; }  
    30.         /// <summary>  
    31.         /// 记录的名称,默认为“记录”  
    32.         /// </summary>  
    33.         public string RecordName { getset; }  
    34.         /// <summary>  
    35.         /// 当前页码的参数名  
    36.         /// </summary>  
    37.         public string CurrentPageKey { getset; }  
    38.         /// <summary>  
    39.         /// 当前页码 只读  
    40.         /// </summary>  
    41.         public int CurrentPage  
    42.         {  
    43.             get  
    44.             {  
    45.                 if (HttpContext.Current.Request.Params[CurrentPageKey] == null)  
    46.                 {  
    47.                     return 1;  
    48.                 }  
    49.                 else  
    50.                 {  
    51.                     try  
    52.                     {  
    53.                         int currentPage = Convert.ToInt32(HttpContext.Current.Request.Params[CurrentPageKey]);  
    54.                         if (currentPage < 1)  
    55.                         {  
    56.                             return 1;  
    57.                         }  
    58.                         else if (currentPage > TotalPage)  
    59.                         {  
    60.                             return TotalPage;  
    61.                         }  
    62.                         else  
    63.                         {  
    64.                             return currentPage;  
    65.                         }  
    66.                     }  
    67.                     catch  
    68.                     {  
    69.                         return 1;  
    70.                     }  
    71.                 }  
    72.             }  
    73.         }  
    74.         private int _PageSize;  
    75.         /// <summary>  
    76.         /// 每页显示记录数  
    77.         /// </summary>  
    78.         public int PageSize  
    79.         {  
    80.             get  
    81.             {  
    82.                 return _PageSize;  
    83.             }  
    84.             set  
    85.             {  
    86.                 if (value < 1)  
    87.                 {  
    88.                     _PageSize = 1;  
    89.                 }  
    90.                 else  
    91.                 {  
    92.                     _PageSize = value;  
    93.                 }  
    94.             }  
    95.         }  
    96.         /// <summary>  
    97.         /// 总页数 只读  
    98.         /// </summary>  
    99.         public int TotalPage  
    100.         {  
    101.             get  
    102.             {  
    103.                 return (int)Math.Ceiling(TotalRecord / (double)PageSize);  
    104.             }  
    105.         }  
    106.         /// <summary>  
    107.         /// 是否显示首页、尾页链接  
    108.         /// </summary>  
    109.         public bool ShowFirstLastPageLink { getset; }  
    110.         /// <summary>  
    111.         /// 是否显示上一页、下一页链接  
    112.         /// </summary>  
    113.         public bool ShowPrevNextPageLink { getset; }  
    114.         /// <summary>  
    115.         /// 是否显示数字按钮  
    116.         /// </summary>  
    117.         public bool ShowDigitalLink { getset; }  
    118.         /// <summary>  
    119.         /// 数字按钮数量  
    120.         /// </summary>  
    121.         public int DigitalLinkCount { getset; }  
    122.         /// <summary>  
    123.         /// 是否显示总记录数  
    124.         /// </summary>  
    125.         public bool ShowTotalRecord { getset; }  
    126.         /// <summary>  
    127.         /// 总记录数出现位置  
    128.         /// </summary>  
    129.         public PagerElementPosition TotalRecordPosition { getset; }  
    130.         /// <summary>  
    131.         /// 是否显示当前页数和总页数信息  
    132.         /// </summary>  
    133.         public bool ShowPageInfo { getset; }  
    134.         /// <summary>  
    135.         /// 当前页和总页数信息显示位置  
    136.         /// </summary>  
    137.         public PagerElementPosition PageInfoPosition { getset; }  
    138.         /// <summary>  
    139.         /// 是否显示GoTo输入区域  
    140.         /// </summary>  
    141.         public bool ShowGoTo { getset; }  
    142.         /// <summary>  
    143.         /// 指定生成的元素对应的class的前缀字符  
    144.         /// </summary>  
    145.         public string CssClassPreWord { getset; }  
    146.         /// <summary>  
    147.         /// 是否创建为ajax分页控件  
    148.         /// </summary>  
    149.         public bool UseAjax { getset; }  
    150.         /// <summary>  
    151.         /// Ajax提交后更新的html元素id  
    152.         /// </summary>  
    153.         public string AjaxUpdateTargetID { getset; }  
    154.         /// <summary>  
    155.         /// Ajax提交后调用的js function名称  
    156.         /// </summary>  
    157.         public string AjaxSuccessFunctionName { getset; }  
    158.         /// <summary>  
    159.         /// 是否自动生成Ajax提交后调用的js function  
    160.         /// </summary>  
    161.         public bool AutoGenarateAjaxSuccessFunction { getset; }  
    162.         /// <summary>  
    163.         /// 使用默认值初始化设置  
    164.         /// </summary>  
    165.         public PagerConfig()  
    166.         {  
    167.             PageSize = 20;  
    168.             RecordUnit = "条";  
    169.             RecordName = "记录";  
    170.             CurrentPageKey = "page";  
    171.             CssClassPreWord = "pager";  
    172.             ShowFirstLastPageLink = true;  
    173.             ShowPrevNextPageLink = true;  
    174.             ShowDigitalLink = true;  
    175.             DigitalLinkCount = 10;  
    176.             ShowTotalRecord = false;  
    177.             TotalRecordPosition = PagerElementPosition.Left;  
    178.             ShowPageInfo = false;  
    179.             PageInfoPosition = PagerElementPosition.Left;  
    180.             ShowGoTo = false;  
    181.             UseAjax = false;  
    182.             AjaxUpdateTargetID = "";  
    183.             AjaxSuccessFunctionName = "OnPageChanged";  
    184.             AutoGenarateAjaxSuccessFunction = true;  
    185.         }  
    186.     }  
    187.     #endregion  
    188.     /// <summary>  
    189.     /// 配合Pager扩展实现分页的帮助类  
    190.     /// </summary>  
    191.     public class PagerHelper  
    192.     {  
    193.         /// <summary>  
    194.         /// 获取记录开始和结束编号,并返回实际的页码  
    195.         /// </summary>  
    196.         /// <param name="allCount">记录总条数</param>  
    197.         /// <param name="pageSize">页大小</param>  
    198.         /// <param name="pageIndex">(输出)当前页码</param>  
    199.         /// <param name="startIndex">(输出)开始编号</param>  
    200.         /// <param name="endIndex">(输出)结束编号</param>  
    201.         /// <param name="currentPageKey">分页参数名称</param>  
    202.         /// <param name="pageIndexIs0Based">页码编号是否从0开始,默认为否</param>  
    203.         /// <param name="recordIndexIs0Based">记录编号是否从0开始,默认为否</param>  
    204.         public static void GetStartAndEndIndex(int allCount, int pageSize, out int pageIndex, out int startIndex, out int endIndex, string currentPageKey = "page"bool pageIndexIs0Based = falsebool recordIndexIs0Based = false)  
    205.         {  
    206.             //计算pageIndex的实际值  
    207.             pageIndex = GetRealPageIndex(allCount, pageSize, currentPageKey, pageIndexIs0Based);  
    208.   
    209.             //计算过程是0based的  
    210.             if (!pageIndexIs0Based)  
    211.             {  
    212.                 pageIndex--;  //转成0based  
    213.             }  
    214.             //计算startIndex和endIndex  
    215.             startIndex = pageIndex * pageSize;  
    216.             endIndex = startIndex + pageSize - 1;  
    217.             if (endIndex > allCount - 1)  
    218.             {  
    219.                 endIndex = allCount - 1;  
    220.             }  
    221.   
    222.             //0based计算完成,下面根据设置不同,输出不同  
    223.             if (!pageIndexIs0Based)  
    224.             {  
    225.                 pageIndex++;  
    226.             }  
    227.             if (!recordIndexIs0Based)  
    228.             {  
    229.                 startIndex++;  
    230.                 endIndex++;  
    231.             }  
    232.         }  
    233.   
    234.         /// <summary>  
    235.         /// 返回实际页码  
    236.         /// </summary>  
    237.         /// <param name="allCount">总记录数</param>  
    238.         /// <param name="pageSize">页面大小</param>  
    239.         /// <param name="currentPageKey">分页参数名称</param>  
    240.         /// <param name="pageIndexIs0Based">页码编号是否从0开始,默认为否</param>  
    241.         /// <returns>实际页码</returns>  
    242.         public static int GetRealPageIndex(int allCount, int pageSize, string currentPageKey = "page"bool pageIndexIs0Based = false)  
    243.         {  
    244.             int pageIndex;  
    245.             //整个计算过程都是0based的  
    246.             if (pageSize < 1)  
    247.             {  
    248.                 pageSize = 1; //容错  
    249.             }  
    250.             if (HttpContext.Current.Request.Params[currentPageKey] == null)  
    251.             {  
    252.                 pageIndex = 0;  
    253.             }  
    254.             else  
    255.             {  
    256.                 try  
    257.                 {  
    258.                     int _pageIndex = Convert.ToInt32(HttpContext.Current.Request.Params[currentPageKey]);   //待判断的页码  
    259.                     if (!pageIndexIs0Based)  
    260.                     {  
    261.                         _pageIndex--;   //转成0based  
    262.                     }  
    263.                     if (_pageIndex < 0)  
    264.                     {  
    265.                         pageIndex = 0;  
    266.                     }  
    267.                     else  
    268.                     {  
    269.                         int totalPage = (int)Math.Ceiling(allCount / (double)pageSize);  
    270.                         if (_pageIndex >= totalPage)  
    271.                         {  
    272.                             pageIndex = totalPage - 1;  
    273.                         }  
    274.                         else  
    275.                         {  
    276.                             pageIndex = _pageIndex;  
    277.                         }  
    278.                     }  
    279.                 }  
    280.                 catch  
    281.                 {  
    282.                     pageIndex = 0;  
    283.                 }  
    284.             }  
    285.             //0based计算完成,下面根据设置不同,输出不同  
    286.             return (pageIndexIs0Based) ? pageIndex : pageIndex + 1;  
    287.         }  
    288.   
    289.     }  
    290. }  
    291.   
    292. namespace System.Web.Mvc.Html  
    293. {  
    294.     public static class PagerExtensions  
    295.     {  
    296.         //提取 返回a标签 方法  
    297.         private static string getLinkHtml(UrlHelper urlHelper, bool useAjax, string ajaxSuccessFunction, string linkContent, string actionName, string controllerName, RouteValueDictionary routeValues)  
    298.         {  
    299.             string link = "";  
    300.             if (useAjax)  
    301.             {  
    302.                 link += "<a href="javascript:$.post('" + urlHelper.Action(actionName, controllerName) + "',{";  
    303.                 //将route放到post表单中  
    304.                 foreach (var route in routeValues.Keys)  
    305.                 {  
    306.                     link += route + ":'" + routeValues[route].ToString() + "',";  
    307.                 }  
    308.                 if (routeValues.Count > 0)  
    309.                 {  
    310.                     link = link.Remove(link.Length - 1);  
    311.                 }  
    312.                 link += "}," + ajaxSuccessFunction + ")" >";  
    313.             }  
    314.             else  
    315.             {  
    316.                 link += "<a href="" + urlHelper.Action(actionName, controllerName, routeValues) + "">";  
    317.             }  
    318.             link += linkContent;  
    319.             link += "</a>";  
    320.             return link;  
    321.         }  
    322.  
    323.         #region 分页扩展  
    324.   
    325.         /// <summary>  
    326.         /// 返回用于分页的div元素  
    327.         /// </summary>  
    328.         /// <param name="htmlHelper">HtmlHelper</param>  
    329.         /// <param name="pagerConfig">分页设置对象</param>  
    330.         /// <returns></returns>  
    331.         public static MvcHtmlString Pager(this HtmlHelper htmlHelper, PagerConfig pagerConfig)  
    332.         {  
    333.             return Pager(htmlHelper, """"new { }, new { }, pagerConfig);  
    334.         }  
    335.   
    336.         /// <summary>  
    337.         /// 返回用于分页的div元素  
    338.         /// </summary>  
    339.         /// <param name="htmlHelper">HtmlHelper</param>  
    340.         /// <param name="htmlAttributes">html属性对象</param>  
    341.         /// <param name="pagerConfig">分页设置对象</param>  
    342.         /// <returns></returns>  
    343.         public static MvcHtmlString Pager(this HtmlHelper htmlHelper, object htmlAttributes, PagerConfig pagerConfig)  
    344.         {  
    345.             return Pager(htmlHelper, """"new { }, htmlAttributes, pagerConfig);  
    346.         }  
    347.         /// <summary>  
    348.         /// 返回用于分页的div元素  
    349.         /// </summary>  
    350.         /// <param name="htmlHelper">HtmlHelper</param>  
    351.         /// <param name="actionName">方法</param>  
    352.         /// <param name="htmlAttributes">html属性对象</param>  
    353.         /// <param name="pagerConfig">分页设置对象</param>  
    354.         /// <returns></returns>  
    355.         public static MvcHtmlString Pager(this HtmlHelper htmlHelper, string actionName, object htmlAttributes, PagerConfig pagerConfig)  
    356.         {  
    357.             return Pager(htmlHelper, actionName, ""new { }, htmlAttributes, pagerConfig);  
    358.         }  
    359.         /// <summary>  
    360.         /// 返回用于分页的div元素  
    361.         /// </summary>  
    362.         /// <param name="htmlHelper">HtmlHelper</param>  
    363.         /// <param name="actionName">方法</param>  
    364.         /// <param name="controllerName">控制器</param>  
    365.         /// <param name="htmlAttributes">html属性对象</param>  
    366.         /// <param name="pagerConfig">分页设置对象</param>  
    367.         /// <returns></returns>  
    368.         public static MvcHtmlString Pager(this HtmlHelper htmlHelper, string actionName, string controllerName, object htmlAttributes, PagerConfig pagerConfig)  
    369.         {  
    370.             return Pager(htmlHelper, actionName, controllerName, new { }, htmlAttributes, pagerConfig);  
    371.         }  
    372.         /// <summary>  
    373.         /// 返回用于分页的div元素  
    374.         /// </summary>  
    375.         /// <param name="htmlHelper">HtmlHelper</param>  
    376.         /// <param name="actionName">方法</param>  
    377.         /// <param name="controllerName">控制器</param>  
    378.         /// <param name="routeValues">路由参数</param>  
    379.         /// <param name="htmlAttributes">html属性对象</param>  
    380.         /// <param name="pagerConfig">分页设置对象</param>  
    381.         /// <returns></returns>  
    382.         public static MvcHtmlString Pager(this HtmlHelper htmlHelper, string actionName, string controllerName, object routeValues, object htmlAttributes, PagerConfig pagerConfig)  
    383.         {  
    384.   
    385.             RouteValueDictionary RouteValues;  
    386.             if (routeValues == null)  
    387.             {  
    388.                 RouteValues = new RouteValueDictionary();  
    389.             }  
    390.             else  
    391.             {  
    392.                 RouteValues = new RouteValueDictionary(routeValues);  
    393.             }  
    394.   
    395.             UrlHelper Url = new UrlHelper(htmlHelper.ViewContext.RequestContext);  
    396.             AjaxHelper Ajax = new AjaxHelper(htmlHelper.ViewContext, htmlHelper.ViewDataContainer);  
    397.   
    398.             StringBuilder sbPager = new StringBuilder();  
    399.             sbPager.Append("<div");  
    400.             //利用反射获取htmlAttributes的全部元素和值  
    401.             if (htmlAttributes != null)  
    402.             {  
    403.                 PropertyInfo[] htmlProperties = htmlAttributes.GetType().GetProperties();  
    404.                 foreach (var property in htmlProperties)  
    405.                 {  
    406.                     sbPager.Append(" " + property.Name + "="" + property.GetValue(htmlAttributes).ToString() + """);  
    407.                 }  
    408.             }  
    409.             sbPager.Append(">");  
    410.             //左侧记录总数信息  
    411.             if (pagerConfig.ShowTotalRecord && pagerConfig.PageInfoPosition == PagerElementPosition.Left)  
    412.             {  
    413.                 sbPager.Append("<span class="" + pagerConfig.CssClassPreWord + "-count">共" + pagerConfig.TotalRecord + pagerConfig.RecordUnit + pagerConfig.RecordName + "</span>");  
    414.             }  
    415.             //左侧页码信息  
    416.             if (pagerConfig.ShowPageInfo && pagerConfig.PageInfoPosition == PagerElementPosition.Left)  
    417.             {  
    418.                 sbPager.Append("<span class="" + pagerConfig.CssClassPreWord + "-info">第" + pagerConfig.CurrentPage + "页/共" + pagerConfig.TotalPage + "页</span>");  
    419.             }  
    420.             //首页  
    421.             if (pagerConfig.ShowFirstLastPageLink)  
    422.             {  
    423.                 if (pagerConfig.CurrentPage > 1)  
    424.                 {  
    425.                     RouteValues[pagerConfig.CurrentPageKey] = 1;  
    426.                     sbPager.Append("<span class="" + pagerConfig.CssClassPreWord + "-btn">" + getLinkHtml(Url, pagerConfig.UseAjax, pagerConfig.AjaxSuccessFunctionName, "首页", actionName, controllerName, RouteValues) + "</span>");  
    427.                 }  
    428.                 else  
    429.                 {  
    430.                     sbPager.Append("<span class="" + pagerConfig.CssClassPreWord + "-btn">首页</span>");  
    431.                 }  
    432.             }  
    433.             //上一页  
    434.             if (pagerConfig.ShowPrevNextPageLink)  
    435.             {  
    436.                 if (pagerConfig.CurrentPage > 1)  
    437.                 {  
    438.                     RouteValues[pagerConfig.CurrentPageKey] = pagerConfig.CurrentPage - 1;  
    439.                     sbPager.Append("<span class="" + pagerConfig.CssClassPreWord + "-btn">" + getLinkHtml(Url, pagerConfig.UseAjax, pagerConfig.AjaxSuccessFunctionName, "上一页", actionName, controllerName, RouteValues) + "</span>");  
    440.                 }  
    441.                 else  
    442.                 {  
    443.                     sbPager.Append("<span class="" + pagerConfig.CssClassPreWord + "-btn">上一页</span>");  
    444.                 }  
    445.             }  
    446.             //数字导航开始  
    447.             if (pagerConfig.ShowDigitalLink)  
    448.             {  
    449.   
    450.                 int shownStartPageIndex, shownEndPageIndex;  
    451.                 //总页数少于要显示的页数,页码全部显示  
    452.                 if (pagerConfig.DigitalLinkCount >= pagerConfig.TotalPage)  
    453.                 {  
    454.                     shownStartPageIndex = 1;  
    455.                     shownEndPageIndex = pagerConfig.TotalPage;  
    456.                 }  
    457.                 else//显示指定数量的页码  
    458.                 {  
    459.                     int forward = (int)Math.Ceiling(pagerConfig.DigitalLinkCount / 2.0);  
    460.                     if (pagerConfig.CurrentPage > forward)//起始页码大于1  
    461.                     {  
    462.                         shownEndPageIndex = pagerConfig.CurrentPage + pagerConfig.DigitalLinkCount - forward;  
    463.                         if (shownEndPageIndex > pagerConfig.TotalPage)//结束页码大于总页码结束页码为最后一页  
    464.                         {  
    465.                             shownStartPageIndex = pagerConfig.TotalPage - pagerConfig.DigitalLinkCount + 1;  
    466.                             shownEndPageIndex = pagerConfig.TotalPage;  
    467.   
    468.                         }  
    469.                         else  
    470.                         {  
    471.                             shownStartPageIndex = pagerConfig.CurrentPage - forward + 1;  
    472.                         }  
    473.                     }  
    474.                     else//起始页码从1开始  
    475.                     {  
    476.                         shownStartPageIndex = 1;  
    477.                         shownEndPageIndex = pagerConfig.DigitalLinkCount;  
    478.                     }  
    479.                 }  
    480.                 //向上…  
    481.                 if (shownStartPageIndex > 1)  
    482.                 {  
    483.                     RouteValues[pagerConfig.CurrentPageKey] = shownStartPageIndex - 1;  
    484.                     sbPager.Append("<span class="" + pagerConfig.CssClassPreWord + "-number">" + getLinkHtml(Url, pagerConfig.UseAjax, pagerConfig.AjaxSuccessFunctionName, "...", actionName, controllerName, RouteValues) + "</span>");  
    485.                 }  
    486.                 //数字  
    487.                 for (int i = shownStartPageIndex; i <= shownEndPageIndex; i++)  
    488.                 {  
    489.                     if (i != pagerConfig.CurrentPage)  
    490.                     {  
    491.                         RouteValues[pagerConfig.CurrentPageKey] = i;  
    492.                         sbPager.Append("<span class="" + pagerConfig.CssClassPreWord + "-number">" + getLinkHtml(Url, pagerConfig.UseAjax, pagerConfig.AjaxSuccessFunctionName, i.ToString(), actionName, controllerName, RouteValues) + "</span>");  
    493.                     }  
    494.                     else  
    495.                     {  
    496.                         sbPager.Append("<span class="" + pagerConfig.CssClassPreWord + "-number " + pagerConfig.CssClassPreWord + "-currentnum">" + i.ToString() + "</span>");  
    497.                     }  
    498.                 }  
    499.                 //向下…  
    500.                 if (shownEndPageIndex < pagerConfig.TotalPage)  
    501.                 {  
    502.                     RouteValues[pagerConfig.CurrentPageKey] = shownEndPageIndex + 1;  
    503.                     sbPager.Append("<span class="" + pagerConfig.CssClassPreWord + "-number">" + getLinkHtml(Url, pagerConfig.UseAjax, pagerConfig.AjaxSuccessFunctionName, "...", actionName, controllerName, RouteValues) + "</span>");  
    504.                 }  
    505.             }  
    506.             ////数字导航结束  
    507.   
    508.             //下一页  
    509.             if (pagerConfig.ShowPrevNextPageLink)  
    510.             {  
    511.                 if (pagerConfig.CurrentPage < pagerConfig.TotalPage)  
    512.                 {  
    513.                     RouteValues[pagerConfig.CurrentPageKey] = pagerConfig.CurrentPage + 1;  
    514.                     sbPager.Append("<span class="" + pagerConfig.CssClassPreWord + "-btn">" + getLinkHtml(Url, pagerConfig.UseAjax, pagerConfig.AjaxSuccessFunctionName, "下一页", actionName, controllerName, RouteValues) + "</span>");  
    515.                 }  
    516.                 else  
    517.                 {  
    518.                     sbPager.Append("<span class="" + pagerConfig.CssClassPreWord + "-btn">下一页</span>");  
    519.                 }  
    520.             }  
    521.             //尾页  
    522.             if (pagerConfig.ShowFirstLastPageLink)  
    523.             {  
    524.                 if (pagerConfig.CurrentPage < pagerConfig.TotalPage)  
    525.                 {  
    526.   
    527.                     RouteValues[pagerConfig.CurrentPageKey] = pagerConfig.TotalPage;  
    528.                     sbPager.Append("<span class="" + pagerConfig.CssClassPreWord + "-btn">" + getLinkHtml(Url, pagerConfig.UseAjax, pagerConfig.AjaxSuccessFunctionName, "尾页", actionName, controllerName, RouteValues) + "</span>");  
    529.                 }  
    530.                 else  
    531.                 {  
    532.                     sbPager.Append("<span class="" + pagerConfig.CssClassPreWord + "-btn">尾页</span>");  
    533.   
    534.                 }  
    535.             }  
    536.   
    537.             //右侧记录总数信息  
    538.             if (pagerConfig.ShowTotalRecord && pagerConfig.PageInfoPosition == PagerElementPosition.Right)  
    539.             {  
    540.                 sbPager.Append("<span class="" + pagerConfig.CssClassPreWord + "-count">共" + pagerConfig.TotalRecord + pagerConfig.RecordUnit + pagerConfig.RecordName + "</span>");  
    541.             }  
    542.             //右侧页码信息  
    543.             if (pagerConfig.ShowPageInfo && pagerConfig.PageInfoPosition == PagerElementPosition.Right)  
    544.             {  
    545.                 sbPager.Append("<span class="" + pagerConfig.CssClassPreWord + "-info">第" + pagerConfig.CurrentPage + "页/共" + pagerConfig.TotalPage + "页</span>");  
    546.             }  
    547.   
    548.             //页码输入框  
    549.             if (pagerConfig.ShowGoTo)  
    550.             {  
    551.   
    552.                 RouteValues[pagerConfig.CurrentPageKey] = "--pageRouteValue--";  
    553.                 sbPager.Append("<span class="" + pagerConfig.CssClassPreWord + "-goto">转到第<input class="" + pagerConfig.CssClassPreWord + "-goto-input" type="text" url="" + Url.Action(actionName, controllerName, RouteValues) + "" />页");  
    554.                   
    555.                 if (pagerConfig.UseAjax)  
    556.                 {  
    557.                     sbPager.Append("<input class="" + pagerConfig.CssClassPreWord + "-goto-submit" type="button" value="GO" onclick="$.post( $(this).prev().attr('url').replace('--pageRouteValue--',$(this).prev().val())," + pagerConfig.AjaxSuccessFunctionName + ")" /></span>");  
    558.                 }  
    559.                 else  
    560.                 {  
    561.                     sbPager.Append("<input class="" + pagerConfig.CssClassPreWord + "-goto-submit" type="button" value="GO" onclick="window.location = $(this).prev().attr('url').replace('--pageRouteValue--',$(this).prev().val());" /></span>");  
    562.                 }  
    563.             }  
    564.             //ajax分页回调函数  
    565.             if (pagerConfig.UseAjax)  
    566.             {  
    567.                 if (pagerConfig.AutoGenarateAjaxSuccessFunction)  
    568.                 {  
    569.                     sbPager.Append("<script type="text/javascript">function " + pagerConfig.AjaxSuccessFunctionName + "(data){$('#" + pagerConfig.AjaxUpdateTargetID + "').html(data);}</script>");  
    570.                 }  
    571.             }  
    572.             sbPager.Append("</div>");  
    573.             return MvcHtmlString.Create(sbPager.ToString());  
    574.         }  
    575.  
    576.         #endregion  
    577.   
    578.     }  
    579. }  

    之前只实现了url分页,后来根据需要,又增加了ajax分页,ajax分页要配合一 个返回partialview的action来使用,具体就是调整调整useajax为true,调整AjaxUpdateTargetID,如果需要在 一个页面中多次使用分页控件,可能还需要用到AjaxSuccessFunctionName和 AutoGenarateAjaxSuccessFunction。几个分页控件如果使用相同的处理方法,只需要一个设置 AutoGenarateAjaxSuccessFunction为true即可,其余为false,如果分别用不同的处理方法,则设置每个 PagerConfig的AjaxSuccessFunctionName属性。

    调用方法:


    在controller利用PagerHelper实现分页:

      1.             int pageSize = 20;  
      2.             int allCount = db.WebVideoCom_User.Count();  
      3.             ViewBag.Num = allCount;  
      4.             ViewBag.PageSize = pageSize;  
      5.             int pageIndex, startIndex, endIndex;  
      6.             //获取开始和结束的记录序号  
      7.             PagerHelper.GetStartAndEndIndex(allCount, pageSize, out pageIndex, out startIndex, out endIndex);  
      8.             //调用存储过程返回指定序号范围的数据  
      9.             return View(db.SelectUserList(startIndex, endIndex)); 
  • 相关阅读:
    Developer 转型记:一个开发平台的“魔力”
    实践录丨如何在鲲鹏服务器OpenEuler操作系统中快速部署OpenGauss数据库
    一图看懂华为云DevCloud如何应对敏捷开发的测试挑战
    华为云GaussDB(DWS)内存知识点,你知道吗?
    在人工智能时代追逐的“后浪”
    【华为云技术分享】DLI跨源|当DLI遇见MongoDB
    授人以渔:stm32资料查询技巧
    云小课 | IPv4枯了,IPv6来了
    揭秘淘宝平台广告策略,拆解最佳投放实践
    520了,用32做个简单的小程序
  • 原文地址:https://www.cnblogs.com/wahaccp/p/3360674.html
Copyright © 2011-2022 走看看