• 分页插件通用处理,以asp.net mvc为例


    Model:

       public class PaggerModel
        {
            public PaggerModel()
            {
                BarSize = 10;
            }
    
            public PaggerModel(int total, int page, int size) : this()
            {
                TotalCount = total;
                CurrentPageIndex = page;
                PageSize = size;
            }
    
            public PaggerModel(int total, int page, int size, string action, string controller) : this(total, page, size)
            {
                ActionName = action;
                ControllerName = controller;
            }
    
            public string ActionName { get; set; }
            public int BarSize { get; set; }
            public string ControllerName { get; set; }
    
            /// <summary>
            /// 当前页码
            /// </summary>
            public int CurrentPageIndex
            {
                get
                {
                    return currentPageIndex;
                }
                set
                {
                    currentPageIndex = (value < 1 ? 1 : value);
                }
            }
    
            /// <summary>
            /// 最后一页页码
            /// </summary>
            public int LastPageIndex
            {
                get
                {
                    if (PageSize == 0)
                    {
                        return (int)Math.Ceiling((double)TotalCount / 10);
                    }
                    return (int)Math.Ceiling((double)TotalCount / PageSize) == 0 ? 1 : (int)Math.Ceiling((double)TotalCount / PageSize);
                }
            }
    
    
            /// <summary>
            /// 单个页面显示条目
            /// </summary>
            public int PageSize
            {
                get
                {
                    return pageSize;
                }
                set
                {
                    pageSize = (value < 1 ? 1 : value);
                }
            }
    
            /// <summary>
            /// 总条目
            /// </summary>
            public int TotalCount { get; set; }
            private int currentPageIndex { get; set; }
            private int pageSize { get; set; }
        }

    View:

    以分部视图的方式渲染:

     @Html.Partial("PaggerModel", ViewBag.PaggerModel as PaggerModel)

    PaggerModel.cshtml:

    @model PaggerModel
    @{
        var config = ViewData["Config"] as PaggerModelConfig;
        var configcss = "";
        if (config != null)
        {
            configcss = "pager-init";
        }
        else
        {
            config = new PaggerModelConfig { };
        }
        int current = Model.CurrentPageIndex;
        var pageSize = Model.PageSize == 0 ? 10 : Model.PageSize;
        var half = (Model.BarSize - 1) / 2;
        var fi = current - half;
        var li = current + Model.BarSize - 1 - half;
        li = li > Model.LastPageIndex ? Model.LastPageIndex : li;
        if (fi < 1)
        {
            fi = 1;
            li = Model.LastPageIndex < Model.BarSize ? Model.LastPageIndex : Model.BarSize;
        }
        while (fi > 1 && li - fi + 1 < Model.BarSize)
        {
            --fi;
        }
        var maxrow = Model.TotalCount > 200 ? 200 : Model.TotalCount;
        if (maxrow == 0) { maxrow = 1; }
    }
    
    <div class="turn_page_wrap">
        <ul class="turn_page_bar df">
            <li class="first_page"><a href="@Url.QAction(Model.ActionName, Model.ControllerName, new { p=1, size=pageSize })">首页</a></li>
            @if (current > 1)
                {
                <li class="page_num"><a href="@Url.QAction(Model.ActionName, Model.ControllerName, new { p=current-1, size=pageSize })">&laquo;</a></li>
            }
            else
            {
                <li class="page_num on"><a href="javascript:void(0)">&laquo;</a></li>
            }
            @for (int i = fi; i <= li; i++)
                {
                    if (i == current)
                    {
                    <li class="page_num on"><a href="@Url.QAction(Model.ActionName, Model.ControllerName, new { p = i, size = pageSize })">@i</a></li>
                }
                else
                {
                    <li class="page_num"><a href="@Url.QAction(Model.ActionName, Model.ControllerName, new { p = i, size = pageSize })">@i</a></li>
                }
            }
            @if (current < Model.LastPageIndex)
                {
                <li class="page_num"><a href="@Url.QAction(Model.ActionName, Model.ControllerName, new { p = current + 1, size = pageSize })">&raquo;</a></li>
            }
            else
            {
                <li class="page_num on"><a href="javascript:void(0)">&raquo;</a></li>
            }
            <li class="last_page"><a href="@Url.QAction(Model.ActionName, Model.ControllerName, new { p=Model.LastPageIndex, size=pageSize })">尾页</a></li>
        </ul>
    </div>
  • 相关阅读:
    MySQL学习总结(五)表数据查询
    MySQL学习总结(四)数据的基本操作以及MySQL运算符和常用函数
    MySQL学习总结(三)索引
    MySQL学习总结(二)数据库以及表的基本操作
    MySQL学习总结(一)下载与安装
    记一次SmtpClient发送邮件引发的系列问题
    C语言学习笔记(五) 数组
    C语言学习笔记(四) 流程控制
    C语言学习笔记(三) 输入输出函数的基本用法以及运算符
    C语言学习笔记(二) 基础知识
  • 原文地址:https://www.cnblogs.com/a14907/p/6428316.html
Copyright © 2020-2023  润新知