C# .Net MVC Razor 视图静态分页导航栏生成器

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xxj_jing/article/details/7899125

这里贴出3个重要组成部分

1.分页导航栏主算法代码(包括 @Html 扩展方法)

2.HTML 页面调用代码(包括导航栏样式)

3.Controller 控制器代码

4.测试路径:http://www.****.com/EBusiness/Paination/2?pc=10(这里只是个本机测试url实例,不同机子测试 域名部分(www.*****.com)需要替换一下)

   参数说明:pc(pageCount),总页数

   其中的 2 是:pageIndex ,当前页索引;使用的是默认路由也就是 {controller}/{action}/{id} 形式的路由定义

5.效果展示

注:实现效果简单,如同学们有时间丰富一下配置参数,让其可设置性更强些。

      丰富完了帖回来我也用哈偷笑

------------------------------------------------------------------------------------------------------------------------------

1.分页导航栏主算法代码

using System;
using System.Text;
using System.Web.Mvc;

namespace EB.Ctrl.Utility
{
  #region Mvc 分页栏扩展方法
  /// <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, PaginationBarBilder bar)
    {
      return new MvcHtmlString(bar.GenPaginationHtml());
    }
  }

  #endregion

  #region 分页条参数
  /// <summary>
  /// 分页条参数
  /// </summary>
  public class PageBarPars
  {
    /// <summary>
    /// 总页数
    /// </summary>
    public int PageCount { set; get; }
    /// <summary>
    /// 当前页索引
    /// </summary>
    public int PageIndex { set; get; }
  }
  #endregion

  #region 分页导航栏生成器
  public class PaginationBarBilder
  {
    /// <summary>
    /// 分页导航栏生成器
    /// </summary>
    /// <param name="url">页面地址模板,其中改变的页面索引部分使用{0}替换</param>
    /// <param name="pageCount">总页数</param>
    /// <param name="pageIndex">当前页索引</param>
    public PaginationBarBilder(string url,int pageCount,int pageIndex)
    {
      m_url = url;
      m_pageCount = pageCount;
      m_pageIndex = pageIndex;
    }

    private int m_pageIndex = 0;
    private int m_pageCount = 0;
    private string m_url = string.Empty;
    private int m_offset = 3;
    private StringBuilder m_html = new StringBuilder();

    /// <summary>
    /// 生成分页m_html代码
    /// </summary>
    /// <returns></returns>
    public string GenPaginationHtml()
    {
      StringBuilder m_html = new StringBuilder();
      m_html.Append("<div id='pagination'>");
      m_html.Append(GenPrevious());
      m_html.Append(GenLeftAnchor());
      m_html.Append(GenMidArea());
      m_html.Append(GenRightAnchor());
      m_html.Append(GenNext());
      m_html.Append("</div>");
      return m_html.ToString();
    }
    /// <summary>
    /// 生成上一页
    /// </summary>
    private string GenPrevious()
    {
      StringBuilder m_html = new StringBuilder();
      string href = string.Empty;
      if (m_pageIndex <= 0 || m_pageCount<=1)
      {
        m_html.Append("<span class='current prev'>前一页</span>");
        return m_html.ToString();
      }
      href = string.Format(m_url, m_pageIndex-1);
      m_html.Append(
        string.Format("<a class='prev' href='{0}'>前一页</a>", href));
      return m_html.ToString();
    }
    /// <summary>
    /// 生成后一页
    /// </summary>
    private string GenNext()
    {
      StringBuilder m_html = new StringBuilder();
      string href = string.Empty;
      if (m_pageIndex >= m_pageCount-1)
      {
        m_html.Append("<span class='current next'>后一页</span>");
        return m_html.ToString();
      }
      href = string.Format(m_url, ++m_pageIndex);
      m_html.Append(
        string.Format("<a class='prev' href='{0}'>后一页</a>", href));
      return m_html.ToString();
    }
    /// <summary>
    /// 左停靠连接
    /// </summary>
    private string GenLeftAnchor()
    {
      StringBuilder m_html = new StringBuilder();
      string href = string.Empty;
      if (m_pageIndex - m_offset > 0)
      {
        href = string.Format(m_url, 0);
        m_html.Append(string.Format("<a href='{0}'>1</a>", href));
      }

      if (m_pageIndex - m_offset > 1)
        m_html.Append(string.Format("<span>...</span>"));
     
      return m_html.ToString();
    }
    /// <summary>
    /// 右停靠连接
    /// </summary>
    private string GenRightAnchor()
    {
      if (m_pageIndex + m_offset >= m_pageCount - 1)
        return string.Empty;

      StringBuilder m_html = new StringBuilder();
      string href = string.Empty;

      if (m_pageIndex + m_offset < m_pageCount-2)
        m_html.Append(string.Format("<span>...</span>"));

      if (m_pageIndex+m_offset<m_pageCount)
      {
        href = string.Format(m_url, m_pageCount - 1);
        m_html.Append(string.Format("<a href='{0}'>{1}</a>", href,m_pageCount));
      }

      return m_html.ToString();
    }
    /// <summary>
    /// 生成中间分页按钮部分
    /// </summary>
    private string GenMidArea()
    {
      StringBuilder m_html = new StringBuilder();
      string href = string.Empty;
      if (m_pageCount == 1)
      {
        m_html.Append("<span class='current'>1</span>");
        return m_html.ToString();
      }
      Action<int> addHtml = (index =>
      {
        if (index == m_pageIndex)
        {
          href = string.Format("<span class='current'>{0}</span>", index+1);
          m_html.Append(href);
          return;
        }
        href = string.Format(m_url,index);
        href = string.Format("<a href='{0}'>{1}</a>", href, index + 1);
        m_html.Append(href);
      });

      int start = 0;
      int end = 0;
      if (m_pageIndex < m_offset)
      {
        start = 0;
        if (m_offset + m_offset < m_pageCount)
          end = m_offset + m_offset;
        else
          end = m_pageCount - 1;
      }
      else
      {
        start = m_pageIndex - m_offset;
        if (m_pageIndex + m_offset >= m_pageCount)
          end = m_pageCount-1;
        else
          end = m_pageIndex + m_offset;
      }

      for (int i = 0; i < m_pageCount; i++)
      {
        if (i < start || i > end)
          continue;
        addHtml(i);
      }

      return m_html.ToString();
    }
  }
  #endregion
}

 

 

------------------------------------------------------------------------------------------------------------------------------

2.HTML 页面调用代码(包括导航栏样式)

@{
  Layout = null;
}
@using EB.Ctrl.Utility;
<!DOCTYPE html>

<html>
<head>
    <title>Paination</title>
    <style>  
    #pagination{float:right}

    #pagination a {
        text-decoration: none;
     border: 1px solid #AAE;
     color: #666;
    }
    #pagination a:hover {
     BORDER-BOTTOM: #c00 1px solid; BORDER-LEFT: #c00 1px solid; BACKGROUND: #c00; COLOR: #fff; BORDER-TOP: #c00 1px solid; BORDER-RIGHT: #c00 1px solid; TEXT-DECORATION: none
    }
    #pagination a, #pagination span {
        display: inline-block;
        padding: 0.1em 0.4em;
        margin-right: 5px;
     margin-bottom: 5px;
    }

    #pagination .current {
        background: #bc0057;
        color: #fff;
     border: 1px solid #c00;
    }

    #pagination .current.prev, #pagination .current.next{
     color:#999;
     border-color:#999;
     background:#fff;
    }
    </style>
</head>
  <body>
    <!--使用这个扩展页面必须引用 using EB.Ctrl.Utility 命名空间-->
    @Html.PaginationBar(new PaginationBarBilder("http://www.****.com/EBusiness/Paination/{0}?pc=" + this.ViewBag.PagePars.PageCount.ToString(), this.ViewBag.PagePars.PageCount, this.ViewBag.PagePars.PageIndex))
  </body>
</html>

 

 

------------------------------------------------------------------------------------------------------------------------------

3.Controller 控制器代码

using System.Web.Mvc;
using EB.Ctrl.Utility;
using EB.Sys.Extension;

namespace EB.Ctrl.Controllers
{
  public class EBusinessController : Controller
  {

    public ActionResult Paination(string id,string pc)
    {
      PageBarPars pars = new PageBarPars();
      pars.PageCount = pc.GetInt();
      pars.PageIndex = id.GetInt();
      this.ViewBag.PagePars = pars;
      return View();
    }
  }
}

 

转载保留:http://blog.csdn.net/xxj_jing/article/details/7899125

 

 

 

 

 

 

 

展开阅读全文

没有更多推荐了,返回首页