zoukankan      html  css  js  c++  java
  • mvc 简单的分页扩展

    //YPageList
      /// <summary>
            /// 总数据个数
            /// </summary>
            private int _datatotalcount;
            /// <summary>
            /// 当前页数据个数
            /// </summary>
            private int _pagesize;
            /// <summary>
            /// 当前页索引
            /// </summary>
            private int _pageindex;
            /// <summary>
            /// 页面总个数
            /// </summary>
            private int _pagetotalcount;
    
            /// <summary>
            /// 总页数
            /// </summary>
            public int TotalPageCount
            {
                get { return _pagetotalcount; }
            }
    
            /// <summary>
            ///  数据总个数
            /// </summary>
            public int TotalDataCount
            {
                get { return _datatotalcount; }
                set { _datatotalcount = value; }
            }
    
            /// <summary>
            /// 当前页数据个数
            /// </summary>
            public int PageSize
            {
                get { return _pagesize; }
                set { _pagesize = value; }
            }
    
            /// <summary>
            /// 当前页索引值
            /// </summary>
            public int PageIndex
            {
                get { return _pageindex; }
                set { _pageindex = value; }
            }
            /// <summary>
            /// 构造
            /// </summary>
            /// <param name="items">当前页-数据集</param>
            /// <param name="pageIndex">当前页索引值</param>
            /// <param name="pageSize">当前页数据个数</param>
            /// <param name="totalCount">数据总数</param>
            public YPageList(IEnumerable<T> items, int pageIndex, int pageSize, int totalCount)
            {
                base.AddRange(items);
                this._datatotalcount = totalCount;
                this._pagetotalcount = (int)Math.Ceiling((double)(((double)totalCount) / ((double)pageSize)));
                this._pageindex = pageIndex;
                this._pagesize = pageSize;
            }
    <!--view-->
    
    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<YPageList<YanNis.WebSite.MvcPagerDemo.Controllers.User>>" %>
    
    <%@ Import Namespace="YanNis.WebSite.YPager.Helper" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>ViewPage1</title>
        <style>
            .pagenavi
            {
                text-align: center;
                margin-bottom: 15px;
            }
            .pagenavi a
            {
                margin: 0 5px;
                padding: 7px 14px;
                color: #62bee3;
                background: #f9fbfc;
                border: 1px solid #78bbe6;
                font-size: 14px;
            }
            .pagenavi a:hover, .pagenavi .current
            {
                background: #62bee3;
                color: #FFF;
            }
        </style>
    </head>
    <body>
        <p>
            <%=ViewData["Time"]  %></p>
        <table>
            <tr>
                <th>
                </th>
                <th>
                    Name
                </th>
                <th>
                    age
                </th>
            </tr>
            <% foreach (var item in Model)
               { %>
            <tr>
                <td>
                    <%= Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) %>
                    |
                    <%= Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ })%>
                    |
                    <%= Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })%>
                </td>
                <td>
                    <%= Html.Encode(item.Name) %>
                </td>
                <td>
                    <%= Html.Encode(item.age) %>
                </td>
            </tr>
            <% } %>
        </table>
        <p>
            <div class="pagenavi">
                <%=Ajax.YAjaxPage(Model)%>
            </div>
        </p>
    </body>
    </html>
    
    
    
    //Helper
    
     public static class MvcHtmlHelper
        {
            public static MvcHtmlString YAjaxPage<T>(this AjaxHelper ajax, YPageList<T> pagelist)
            {
                string controllname = ajax.ViewContext.RouteData.Values["controller"].ToString();
                string actionname = ajax.ViewContext.RouteData.Values["action"].ToString();
                return ajax.YAjaxPage<T>(pagelist, actionname, controllname);
            }
    
            public static MvcHtmlString YAjaxPage<T>(this AjaxHelper ajax, YPageList<T> pagelist, string actionname, string controllername)
            {
                StringBuilder strBuilder = new StringBuilder();
                if (pagelist.PageIndex > 1)//判断当前索引真是否大于1 ,为真则加上上一页
                {
                    strBuilder.Append("<a href='" + new UrlHelper(ajax.ViewContext.RequestContext).Action(actionname, controllername, new { _pageindex = pagelist.PageIndex - 1 }) + "'>&laquo;</a>");
                }
    
                int startindex = 1;
                int endindex = pagelist.TotalPageCount;
    
                if (pagelist.PageIndex - 1 > 3)//省略前分页数 并加上第一个分页索引
                {
                    startindex = pagelist.PageIndex - 2;
                    strBuilder.Append("<a href='" + new UrlHelper(ajax.ViewContext.RequestContext).Action(actionname, controllername, new { _pageindex = 1 }) + "'>1</a>");
                    strBuilder.Append("...");
                }
    
                if (pagelist.TotalPageCount - pagelist.PageIndex > 3)
                {
                    endindex = pagelist.PageIndex + 2;
                }
    
                for (int i = startindex; i <= endindex; i++)
                {
                    if (i == pagelist.PageIndex)
                    {
                        strBuilder.Append("<a class='current' href='" + new UrlHelper(ajax.ViewContext.RequestContext).Action(actionname, controllername, new { _pageindex = i }) + "'>" + i + "</a>");
                    }
                    else
                    {
                        strBuilder.Append("<a  href='" + new UrlHelper(ajax.ViewContext.RequestContext).Action(actionname, controllername, new { _pageindex = i }) + "'>" + i + "</a>");
                    }
                }
    
                if (pagelist.TotalPageCount > endindex)//省略后分页数 并加上总分页数
                {
                    strBuilder.Append("...");
                    strBuilder.Append("<a href='" + new UrlHelper(ajax.ViewContext.RequestContext).Action(actionname, controllername, new { _pageindex = pagelist.TotalPageCount }) + "'>" + pagelist.TotalPageCount + "</a>");
                }
    
    
                if (pagelist.PageIndex < pagelist.TotalPageCount)//判断是否小于总页数,为真则加上下一页
                {
                    strBuilder.Append("<a href='" + new UrlHelper(ajax.ViewContext.RequestContext).Action(actionname, controllername, new { _pageindex = pagelist.PageIndex + 1 }) + "'>&raquo;</a>");
                }
    
                return MvcHtmlString.Create(strBuilder.ToString());
            }
        }


    自己写的 有什么不足之处欢迎指正

    //action代码
       public ActionResult List(int? _pageindex, int? _pagesize)
            {
                DateTime starttime = DateTime.Now;
                int index = _pageindex ?? 1;
                int size = _pagesize ?? 15;
                IList<User> userlist = new List<User>();
                for (int i = 1; i <= 1000000; i++)//data
                {
                    userlist.Add(new User()
                    {
                        age = i,
                        Name = "YanNis" + i
                    });
                }
                YPageList<User> pagelist = new YPageList<User>(userlist.Skip((index - 1) * size).Take(size), index, size, userlist.Count);
                DateTime endtime = DateTime.Now;
                ViewData["Time"] = endtime - starttime;
                return View(pagelist);
            }
        }
  • 相关阅读:
    PAT 天梯赛 L1-048. 矩阵A乘以B 【数学】
    PAT 天梯赛 L1-047. 装睡 【水】
    PAT 天梯赛 L1-047. 装睡 【水】
    PAT 天梯赛 L1-045. 宇宙无敌大招呼 【水】
    PAT 天梯赛 L1-045. 宇宙无敌大招呼 【水】
    PAT 天梯赛 L1-044. 稳赢 【循环】
    PAT 天梯赛 L1-044. 稳赢 【循环】
    PAT 天梯赛 L1-042. 日期格式化 【水】
    PAT 天梯赛 L1-042. 日期格式化 【水】
    PAT 天梯赛 L1-041. 寻找250 【水】
  • 原文地址:https://www.cnblogs.com/yannis/p/2525988.html
Copyright © 2011-2022 走看看