1. 程式人生 > >C# .Net MVC Razor 檢視靜態分頁導航欄生成器

C# .Net MVC Razor 檢視靜態分頁導航欄生成器

這裡貼出3個重要組成部分

1.分頁導航欄主演算法程式碼(包括 @Html 擴充套件方法)

2.HTML 頁面呼叫程式碼(包括導航欄樣式)

3.Controller 控制器程式碼

   引數說明: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();
    }
  }
}