效果就和开源中国分页一摸一样,但算法很迁就啊,脑子笨请人指点下

周睿 发布于 2010/01/23 09:02
阅读 373
收藏 0
/// 
    /// 仿造开源中国的分页,分页界面类似URL分页
    /// 1 [2] [3] [4] [100]
    /// qq:Zr、还有点BUG没修正
    /// 右边界值(决定了初始化几个数字)
    /// 当前索引
    /// 总页数
    /// 步长(步长决定点击[4]后出现的数字个数)
    /// 跳转的页面
    /// 样式名
    /// 
    public string pagIng(int rightNum, int currentIndex, int count, int step, string url, string classname)
    {
        int LeftNum = 0; StringBuilder sb = new StringBuilder();
        if (currentIndex - step <= 1)
            LeftNum = 1;
        else
        {
            LeftNum = currentIndex - step;
            rightNum = currentIndex + (2*step+1);//< rightNum ? rightNum :currentIndex + step;
            if (currentIndex + step > count)
            {
                rightNum = count;
            }
        }
        if (rightNum > count || step <= 0 || currentIndex <= 0)
        {
            rightNum = count;
            return null;
        }
        sb= currentIndex == 1 ? sb.Append("" + 1 + "") : sb.Append("[" + 1 + "]");
        for (int i = LeftNum+1; i < rightNum; i++)
        {
            sb = i == currentIndex ? sb.Append("" + i + "") : sb.Append("[" + i + "]");
        }
        if(count!=1)
        sb = currentIndex == count ? sb.Append("" + count + "") : sb.Append("[" + count + "]");
        return sb.ToString();
    }
这么来调用
public void str()
    {
        int currentIndex = Convert.ToInt32(Request.QueryString["pid"]);
        int LeftNum = 0;//左边界
        int RightNum =11;//右边界
        int Step =3;//步长
        int count = c1.bb() % pagesize == 0 ? c1.bb() / pagesize : c1.bb() / pagesize + 1;
       string str= pagIng(RightNum, currentIndex, count, Step, "?pid", "");
       Response.Write(str); 
    }
代码有点问题,不过基本效果已经出来了

效果就和开源中国分页一摸一样,但算法很迁就啊,脑子笨请人指点下

加载中
0
周睿
周睿

   public string pagIng(int rightNum, int currentIndex, int count, int step, string url, string classname)
    {
        int LeftNum = 0; StringBuilder sb = new StringBuilder();
        if (currentIndex - step <= 1)
            LeftNum = 2;
        else
        {
            LeftNum = currentIndex - step;
            rightNum = currentIndex + (2 * step + 1) > count ? count:currentIndex + (2 * step + 1); 
            if (currentIndex + step > count)
            {
                rightNum = count;
            }
        }
        if (rightNum > count || step <= 0 || currentIndex <= 0)
        {
            rightNum = count;
            return null;
        }
        sb= currentIndex == 1 ? sb.Append("<span class=\"'" + classname + "'\">" + 1 + "</span>") : sb.Append("<span  class=\"'" + classname + "'\"><a href='" + url + "=1'>[" + 1 + "]</a></span>");
        for (int i = LeftNum; i < rightNum; i++)
        {
            sb = i == currentIndex ? sb.Append("<span class=\"'" + classname + "'\">" + i + "</span>") : sb.Append("<span  class=\"'" + classname + "'\"><a href='" + url + "=" + i + "'>[" + i + "]</a></span>");
        }
        if(count!=1)
        sb = currentIndex == count ? sb.Append("<span class=\"'" + classname + "'\">" + count + "</span>") : sb.Append("<span  class=\"'" + classname + "'\"><a href='" + url + "=" + count + "'>[" + count + "]</a></span>");
        return sb.ToString();
    }

  1. 修改下,这里int RightNum =11;//右边界   
  2.         int Step =3;//步长   定义成别的数字,效果就有点乱了唉
返回顶部
顶部