首页 > 代码库 > 自定义分页1

自定义分页1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc.Html;
using System.Web.Routing;
using Water.Helper;
using System.Web.Mvc.Ajax;
namespace System.Web.Mvc
{
    
    public static class PagerExtension
    {
        public static MvcHtmlString Pager<T>(this AjaxHelper ajax, PagedList<T> data, string tagId)
        {
            //数字导航的开始数字,7
            // 7-5>=1?7-5:1 start = 2
            int start = (data.PageIndex - 5) >= 1 ? (data.PageIndex - 5) : 1;
            //数字导航的结束数字
            //15-2 
            int end = (data.TotalPages - start) > 10 ? start + 10 : data.TotalPages;
            //路由数据
            RouteValueDictionary vs = ajax.ViewContext.RouteData.Values;
            //URL数据
            var queryString = ajax.ViewContext.HttpContext.Request.QueryString;
            //合并URL数据
            foreach (string key in queryString.Keys)
                if (queryString[key] != null && !string.IsNullOrEmpty(key))
                    vs[key] = queryString[key];
            //合并表单数据
            var FormString = ajax.ViewContext.HttpContext.Request.Form;
            foreach (string key in FormString.Keys)
                vs[key] = FormString[key];
            //输出分页Html
            var builder = new StringBuilder();
            builder.AppendFormat("<div class=\"mvc_pager\">");
            //显示首页和上页
            if (data.HasPreviousPage)
            {
                vs["pageIndex"] = 1;
                builder.Append(AjaxExtensions.ActionLink(ajax, "首页", vs["action"].ToString(),vs, new AjaxOptions { UpdateTargetId = tagId }));
                vs["pageIndex"] = data.PageIndex - 1;
                builder.Append(AjaxExtensions.ActionLink(ajax, "上页", vs["action"].ToString(),vs, new AjaxOptions { UpdateTargetId = tagId }));
            }
            //显示数字页码
            for (int i = start; i <= end; i++)
            {
                vs["pageIndex"] = i;
                if (i == data.PageIndex)
                {
                    builder.Append("<font class=‘thispagethis‘>" + i.ToString() + "</font>");
                }
                else
                {
                    builder.Append(AjaxExtensions.ActionLink(ajax, i.ToString(), vs["action"].ToString(),vs, new AjaxOptions { UpdateTargetId = tagId }));
                }
            }
            //显示下页和末页
            if (data.HasNextPage)
            {
                vs["pageIndex"] = data.PageIndex + 1;
                builder.Append(AjaxExtensions.ActionLink(ajax, "下页", vs["action"].ToString(),vs, new AjaxOptions { UpdateTargetId = tagId }));
                vs["pageIndex"] = data.TotalPages;
                builder.Append(AjaxExtensions.ActionLink(ajax, "末页", vs["action"].ToString(),vs, new AjaxOptions { UpdateTargetId = tagId }));
            }
            builder.Append("" + data.TotalCount + "条 第" + data.PageIndex + "页/共" + data.TotalPages + "页 </div>");
            return new MvcHtmlString(builder.ToString());

            //new MvcHtmlString(builder.ToString());
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="html"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string Pager<T>(this HtmlHelper html, PagedList<T> data)
        {
            //数字导航的开始数字,7
            // 7-5>=1?7-5:1 start = 2
            int start = (data.PageIndex - 5) >= 1 ? (data.PageIndex - 5) : 1;
            //数字导航的结束数字
            //15-2 
            int end = (data.TotalPages - start) > 10 ? start + 10 : data.TotalPages;
            //路由数据
            RouteValueDictionary vs = html.ViewContext.RouteData.Values;
            //URL数据
            var queryString = html.ViewContext.HttpContext.Request.QueryString;
            //合并URL数据
            foreach (string key in queryString.Keys)
                if (queryString[key] != null && !string.IsNullOrEmpty(key))
                vs[key] = queryString[key];
            //合并表单数据
            var FormString = html.ViewContext.HttpContext.Request.Form;
            foreach (string key in FormString.Keys)
                vs[key] = FormString[key];
            //输出分页Html
            var builder = new StringBuilder();
            builder.AppendFormat("<div class=\"mvc_pager\">");
            //显示首页和上页
            if (data.HasPreviousPage)
            {
                vs["pageIndex"] = 1;
                builder.Append(LinkExtensions.ActionLink(html, "首页", vs["action"].ToString(), vs));
                vs["pageIndex"] = data.PageIndex - 1;
                builder.Append(LinkExtensions.ActionLink(html, "上页", vs["action"].ToString(), vs));
            }
            //显示下页和末页
            if (data.HasNextPage)
            {
                vs["pageIndex"] = data.PageIndex + 1;
                builder.Append(LinkExtensions.ActionLink(html, "下页", vs["action"].ToString(), vs));
                vs["pageIndex"] = data.TotalPages;
                builder.Append(LinkExtensions.ActionLink(html, "末页", vs["action"].ToString(), vs));
            }
            builder.Append("" + data.TotalCount + "条 第" + data.PageIndex + "页/共" + data.TotalPages + "页 </div>");
            return builder.ToString();
        }
    }
}

 

自定义分页1