你的位置:首页 > ASP.net教程

[ASP.net教程]Mvc 分页栏扩展方法


using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Web.Mvc;

namespace System.Web.Mvc
{
    #region Mvc 分页栏扩展方法 HtmlPaginationBar

    /// <summary>
    ///  Mvc 分页栏扩展方法
    /// </summary>
    public static class HtmlPaginationBar
    {
        /// <summary>
        /// 生成分页栏(页面调用 @Html.PaginationBar)
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="bar">分页栏生成器</param>
        /// <returns></returns>
        public static MvcHtmlString PaginationBar(this HtmlHelper helper, PaginationBar bar)
        {
            return new MvcHtmlString(bar.GenPaginationHtml());
        }
    }

    #endregion

    #region 分页实体 PageBar
    /// <summary>
    /// 分页参数类
    /// </summary>
    public class PageBar
    {
        /// <summary>
        /// 总页数
        /// </summary>
        public int RecordCount { get; set; }
        /// <summary>
        /// 当前页索引
        /// </summary>
        public int PageIndex { get; set; }

        /// <summary>
        /// 当前页索引
        /// </summary>
        public int PageSize { get; set; }
    }
    #endregion

    #region 分页导航栏生成器 PaginationBar

    public class PaginationBar
    {
        /// <summary>
        /// 分页导航栏生成器
        /// </summary>
        /// <param name="url">页面地址模板,其中改变的页面索引部分使用{0}替换</param>
        /// <param name="pageBar">分页实体</param
        public PaginationBar(String url, PageBar pageBar, object keyValues = null)
        {
            m_url = url;
            m_pageCount = pageBar.RecordCount % pageBar.PageSize == 0 ? pageBar.RecordCount / pageBar.PageSize : (Int32)(pageBar.RecordCount / pageBar.PageSize) + 1;
            m_pageIndex = pageBar.PageIndex;
            m_recordCount = pageBar.RecordCount;
            m_pageSize = pageBar.PageSize;
            m_keyValues = keyValues;
        }

        private Int32 m_pageIndex = 0;
        private Int32 m_pageCount = 0;
        private Int32 m_recordCount = 0;
        private Int32 m_pageSize = 0;
        private String m_url = String.Empty;
        private object m_keyValues = null;
        private StringBuilder m_html = new StringBuilder();
        private StringBuilder m_js = new StringBuilder();
        private StringBuilder m_param = new StringBuilder();

        /// <summary>
        /// 生成分页m_html与js代码
        /// </summary>
        /// <returns></returns>
        public String GenPaginationHtml()
        {
            StringBuilder m_html = new StringBuilder();

            //js参数复制
            m_js.AppendLine("<script> function Pagination(){");
            m_js.Append(GenJs());
            m_js.AppendLine("} Pagination();</script>");

            //HTML
            m_html.Append("<div id='pagination'>");
            m_html.Append(GenFirst());
            m_html.Append(GenPre());
            m_html.Append(GenNext());
            m_html.Append(GenLast());
            m_html.Append(GenJump());
            m_html.Append(GenLabel());
            m_html.Append("</div>");

            return m_html.ToString() + m_js.ToString();
        }

        /// <summary>
        /// 获取js代码
        /// </summary>
        /// <returns></returns>
        public String GenJs()
        {
            if (m_keyValues == null)
                return String.Empty;

            StringBuilder js = new StringBuilder();

            //循环参数
            foreach (var p in m_keyValues.GetType().GetProperties())
            {
                object val = p.GetValue(m_keyValues, null);
                js.AppendLine(String.Format("document.getElementById('{0}').value={1}", p.Name, val));
                m_param.Append(String.Format("&{0}={1}", p.Name, val));
            }

            return js.ToString();
        }

        /// <summary>
        /// 首页
        /// </summary>
        private String GenFirst()
        {
            return String.Format("<a class='first' href='{0}?pageIndex={1}{2}'>首页</a>", m_url, 0, m_param);
        }

        /// <summary>
        /// 尾页
        /// </summary>
        private String GenLast()
        {
            return String.Format("<a class='last' href='{0}?pageIndex={1}{2}'>尾页</a>", m_url, m_pageCount, m_param);
        }

        /// <summary>
        /// 生成上一页
        /// </summary>
        private String GenPre()
        {
            return String.Format("<a class='prev' href='{0}?pageIndex={1}{2}'>前一页</a>", m_url, (m_pageIndex != 1 ? m_pageIndex - 1 : 1), m_param);
        }

        /// <summary>
        /// 生成下一页
        /// </summary>
        private string GenNext()
        {
            return String.Format("<a class='next' href='{0}?pageIndex={1}{2}'>后一页</a>", m_url, (m_pageIndex == m_pageCount ? m_pageIndex : m_pageCount + 1), m_param);
        }

        /// <summary>
        /// 生成
        /// </summary>
        private String GenJump()
        {
            StringBuilder m_jump = new StringBuilder();
            for (Int32 i = 1; i <= m_pageCount; i++)
            {
                m_jump.Append(String.Format("<option value={0}>{0}</option>", i));
            }
            return String.Format("<select  id='pageIndex' name='pageIndex' onchange='javacript:window.location.href={1}?1=1{2}' />{0}<select>", m_jump, m_url, m_param);
        }

        /// <summary>
        /// 生成
        /// </summary>
        private String GenLabel()
        {
            return String.Format("第{0}页,共{1}页,每页{2}条,共{3}条", m_pageIndex, m_pageCount, m_pageSize, m_recordCount);
        }
    }

    #endregion
}