【代码】C#生成翻页器HTML代码,可自定义文字和样式

2022-01-12 10:14:00  阅读 2881 次 评论 0 条

刚才做项目,要做一个列表,带翻页的,本来想偷个懒,用下其他项目的代码,想想,还是写个通用的吧。

效果预览:

image.png

image.png

image.png

image.png



话不多说,直接上代码

/// <summary>
        /// 生成翻页器HTML 使用示例看https://www.yangguangdream.com/?id=2191
        /// </summary>
        /// <param name="currentPage">当前页</param>
        /// <param name="totalPage">页总数</param>
        /// <param name="pageCount">下面显示页码的数量(推荐用奇数),默认5</param>
        /// <param name="url">目标页面的url,页码部分用占位符代替,如{{page}}</param>
        /// <param name="urlFormat">url参数中提到的占位符,默认为{{page}}</param>
        /// <param name="pageBreakClass">页码省略符的class,用于设定样式</param>
        /// <param name="pageTextClass">翻页上一页下一页的class,用于设定样式</param>
        /// <param name="pageNumClass">页码数字的class,用于设定样式</param>
        /// <param name="pageCurClass">当前页的class,用于设定样式</param>
        /// <param name="pageTarget">翻页打开目标(当前页,新窗口),默认_self</param>
        /// <param name="prevPageText">上一页的链接文本</param>
        /// <param name="nextPageText">下一页的链接文本</param>
        /// <param name="pageBreakText">页码省略符的文本,默认为"..."</param>
        /// <returns></returns>
        public static string getPageBarHtml(int currentPage, int totalPage, int pageCount = 5, string url = "", string urlFormat = "{{page}}", string pageBreakClass = "pageBreak", string pageTextClass = "pageText", string pageNumClass = "pageNum", string pageCurClass = "pagecur", string pageTarget = "_self", string prevPageText = "上一页", string nextPageText = "下一页", string pageBreakText = "...")
        {
            StringBuilder sb = new StringBuilder();

            //总页数大于1
            if (totalPage > 1)
            {

                if (currentPage == 1)
                {
                    //当前页为1的情况
                    sb.AppendFormat("<span class=\"{0} {1}\">{2}</span>", pageTextClass, pageCurClass, prevPageText);
                    sb.AppendFormat("<span class=\"{0} {1}\">1</span>", pageNumClass, pageCurClass);
                    for (int i = 2; i <= (currentPage + pageCount - 1 < totalPage ? currentPage + pageCount - 1 : totalPage); i++)//以小的来,如果pagecount
                    {
                        string cpage = i.ToString();
                        sb.AppendFormat("<a href=\"{0}\" class=\"{1}\" target=\"{2}\">{3}</a>", url.Replace(urlFormat, cpage.ToString()), pageNumClass, pageTarget, cpage.ToString());
                    }
                    if (totalPage > pageCount)
                    {
                        if (!string.IsNullOrEmpty(pageBreakText))
                        {
                            sb.AppendFormat("<span class=\"{0}\">{1}</span>", pageBreakClass, pageBreakText);//页码省略号...
                        }
                    }
                    sb.AppendFormat("<a class=\"{0}\" href=\"{1}\" target=\"{2}\">{3}</a>", pageTextClass, url.Replace(urlFormat, (currentPage + 1).ToString()), pageTarget, nextPageText);//下一页
                }
                else if (currentPage == totalPage)
                {
                    //当前页是尾页的情况
                    sb.AppendFormat("<a class=\"{0}\" href=\"{1}\" target=\"{2}\">{3}</a>", pageTextClass, url.Replace(urlFormat, (currentPage - 1).ToString()), pageTarget, prevPageText);//上一页
                    int startI = currentPage - pageCount;//往后移一位,因为多减去了他本身
                    startI = (startI <= 0) ? 1 : startI;//如果减多了,小于0了,就给他默认1
                    if (startI > 1)
                    {
                        startI += 1;
                        if (!string.IsNullOrEmpty(pageBreakText))
                        {
                            sb.AppendFormat("<span class=\"{0}\">{1}</span>", pageBreakClass, pageBreakText);//页码省略号...
                        }
                    }
                    for (int i = startI; i < totalPage; i++)
                    {
                        string cpage = i.ToString();
                        sb.AppendFormat("<a href=\"{0}\" class=\"{1}\" target=\"{2}\">{3}</a>", url.Replace(urlFormat, cpage.ToString()), pageNumClass, pageTarget, cpage.ToString());
                    }
                    sb.AppendFormat("<span class=\"{0} {1}\">{2}</span>", pageNumClass, pageCurClass, totalPage);
                    sb.AppendFormat("<span class=\"{0} {1}\">{2}</span>", pageTextClass, pageCurClass, nextPageText);
                }
                else
                {
                    int leftCount = 0;
                    int rightCount = 0;
                    //是中间页的情况
                    pageCount = pageCount - 1;//要排除那个页码的自身
                    if (pageCount % 2 == 0)//看下是否可以对半分
                    {
                        //显示页码总数是偶数的情况,对半分
                        leftCount = pageCount / 2;
                        rightCount = pageCount / 2;
                    }
                    else
                    {
                        //显示页码总数是奇数的情况
                        double d_l = Convert.ToDouble(pageCount / 2.0);
                        leftCount = Convert.ToInt32(Math.Floor(d_l));//左边少,那就用Floor,反之就用Ceiling
                        rightCount = Convert.ToInt32(Math.Ceiling(d_l));//右边多,那就用Ceiling,反之就用Floor
                    }

                    if (rightCount > (totalPage - currentPage))//判断剩余的页码和右面要显示的页码哪个多,取小的那个
                    {
                        int cha = totalPage - currentPage;//计算当前页距离尾页还有几页
                        leftCount += rightCount - cha;//如果右面不足,那就用左面的补充,rightcount-cha就是计算rightcount比这个相差页数多出几页
                    }

                    int startI = currentPage - leftCount;
                    startI = (startI <= 0) ? 1 : startI;//如果减多了,小于0了,就给他默认1
                    sb.AppendFormat("<a class=\"{0}\" href=\"{1}\" target=\"{2}\">{3}</a>", pageTextClass, url.Replace(urlFormat, (currentPage - 1).ToString()), pageTarget, prevPageText);//上一页
                    if (startI > 1)
                    {
                        if (!string.IsNullOrEmpty(pageBreakText))
                        {
                            sb.AppendFormat("<span class=\"{0}\">{1}</span>", pageBreakClass, pageBreakText);//页码省略号...
                        }
                    }
                    for (int i = startI; i < currentPage; i++)
                    {
                        string cpage = i.ToString();
                        sb.AppendFormat("<a href=\"{0}\" class=\"{1}\" target=\"{2}\">{3}</a>", url.Replace(urlFormat, cpage.ToString()), pageNumClass, pageTarget, cpage.ToString());
                    }
                    sb.AppendFormat("<span class=\"{0} {1}\">{2}</span>", pageNumClass, pageCurClass, currentPage);//当前页
                    for (int i = currentPage + 1; i <= (currentPage +rightCount < totalPage ? currentPage+rightCount : totalPage); i++)
                    {
                        string cpage = i.ToString();
                        sb.AppendFormat("<a href=\"{0}\" class=\"{1}\" target=\"{2}\">{3}</a>", url.Replace(urlFormat, cpage.ToString()), pageNumClass, pageTarget, cpage.ToString());
                    }
                    if (currentPage + rightCount < totalPage)
                    {
                        if (!string.IsNullOrEmpty(pageBreakText))
                        {
                            sb.AppendFormat("<span class=\"{0}\">{1}</span>", pageBreakClass, pageBreakText);//就那个页数的...
                        }
                    }
                    sb.AppendFormat("<a class=\"{0}\" href=\"{1}\" target=\"{2}\">{3}</a>", pageTextClass, url.Replace(urlFormat, (currentPage + 1).ToString()), pageTarget, nextPageText);//下一页
                }
            }
            else
            {
                //总页数等于1
                sb.AppendFormat("<span class=\"{0} {1}\">{2}</span>", pageTextClass, pageCurClass, prevPageText);
                sb.AppendFormat("<span class=\"{0} {1}\">1</span>", pageNumClass, pageCurClass);
                sb.AppendFormat("<span class=\"{0} {1}\">{2}</span>", pageTextClass, pageCurClass, nextPageText);
            }


            string res = sb.ToString();
            return res;

        }


调用:

     string url = "https://www.yangguangdream.com/?page={{page}}";
            int page = 5;
            int totalPage = 180;
            string pageBarHtml = getPageBarHtml(page, totalPage, 6, url, "{{page}}", "pagebreak pagecur", "pageText", "pageNum", "pagecur", "_self", "上一页", "下一页", "...");

输出(经过在线工具手动格式化,实际出来是没有换行格式的):

 <a class="pageText" href="https://www.yangguangdream.com/?page=4" target="_self">上一页</a>
  <span class="pagebreak pagecur">...</span>
  <a href="https://www.yangguangdream.com/?page=3" class="pageNum" target="_self">3</a>
  <a href="https://www.yangguangdream.com/?page=4" class="pageNum" target="_self">4</a>
  <span class="pageNum pagecur">5</span>
  <a href="https://www.yangguangdream.com/?page=6" class="pageNum" target="_self">6</a>
  <a href="https://www.yangguangdream.com/?page=7" class="pageNum" target="_self">7</a>
  <a href="https://www.yangguangdream.com/?page=8" class="pageNum" target="_self">8</a>
  <span class="pagebreak pagecur">...</span>
  <a class="pageText" href="https://www.yangguangdream.com/?page=6" target="_self">下一页</a>



微信扫码查看本文
本文地址:https://www.yangguangdream.com/?id=2191
版权声明:本文为原创文章,版权归 编辑君 所有,欢迎分享本文,转载请保留出处!

发表评论


表情

还没有留言,还不快点抢沙发?