• 一个分页效果ruby版可作为面试题吧


    #coding:utf-8
    total_pages=11
    current_page=10
    a=(1..total_pages).to_a
      #<div class="page_box"><span><font>&laquo;首页</font></span><span><font>&#173;上一页</font></span>
    #<span id="page_on"><font>1</font></span>
    #<span><a href="" >2</a></span>
    #<span><a href="" >3</a></span>
    #<span><a href="" >4</a></span>
    #<span><a href="" >5</a></span>
    #<span><a href="" >...</a></span>
    #<span><a href="" >11</a></span>
    #<span><a href="">&#173;下一页</a></span>
    #<span><a href="" >&#173;尾页&raquo;</a></span>
    #<span><font>1/共11页</font></span>
    #</div>
    #r=[]
    #if current_page-5>0
    #  r<<"1"
    #  r<<"..."+(current_page-3).to_s
    #  r<<current_page-2
    #  r<<current_page-1
    #  r<<current_page
    #  if current_page+1<=total_pages
    #    r<<current_page+1
    #  end
    #  if current_page+2<==total_pages
    #    r<<current_page+2
    #  end
    #else
    #  r.concat((1...current_page).to_a)
    #end
    #r<<current_page
    #if current_page+5<=total_pages
    #  r<<current_page+1
    #  r<<current_page+2
    #  r<<"..."+(current_page+3).to_s
    #else
    #  r.concat(((current_page+1)..total_pages).to_a)
    #end
    #
    #
    #
    #puts r
    #r=[]
    #r<<current_page
    #if current_page-2>0
    #  r.unshift current_page-1
    #  
    #end
    #if current_page-5>0 and current_page+2<=total_pages
    #  a[1..
    #end
    #puts a
    r=[]
    if total_pages<=6
      r.concat((1..total_pages).to_a)
    else
      if current_page-5>0
        if current_page+2<=total_pages
          a.delete_if{|x| x< current_page-2}
        elsif current_page+1<=total_pages
          a.delete_if{|x| x<current_page-3}
        elsif current_page<=total_pages
          a.delete_if{|x| x<current_page-4}
        end
      end
      if current_page+5>0
        if current_page-2>0
          a.delete_if{|x| x >current_page+2}
        elsif current_page-1>0
          a.delete_if{|x| x>current_page+3}
        elsif current_page>0
          a.delete_if{|x| x>current_page+4}
        end
      end
    end
    if a.first!=1
      a.unshift "..."
      a.unshift 1
    end
    if a.last!=total_pages
      a<<"..."
      a<<total_pages
    end
    puts a
    我也是费了好久,快要penkui了,可以看到注释的部分我也是很糊,另外也有对ruby的函数不熟悉,(这个是借口,主要是脑子不好使)
    效果参见ecshop的分页效果,
    实现如下效果

    «首页­上一页1...1415161718­下一页­尾页»18/共18页

    «首页­上一页12345...18­下一页­尾页»1/共18页



    «首页­上一页1­下一页­尾页»1/共1页
    之前blog有一篇有python写的分页,比这个简单,可以算是这个的子集

    最终的代码

    #coding:utf-8

    total_pages=11

    current_page=10

    a=(1..total_pages).to_a

      #<div class="page_box"><span><font>&laquo;首页</font></span><span><font>&#173;上一页</font></span>

    #<span id="page_on"><font>1</font></span>

    #<span><a href="" >2</a></span>

    #<span><a href="" >3</a></span>

    #<span><a href="" >4</a></span>

    #<span><a href="" >5</a></span>

    #<span><a href="" >...</a></span>

    #<span><a href="" >11</a></span>

    #<span><a href="">&#173;下一页</a></span>

    #<span><a href="" >&#173;尾页&raquo;</a></span>

    #<span><font>1/共11页</font></span>

    #</div>

    #r=[]

    #if current_page-5>0

    #  r<<"1"

    #  r<<"..."+(current_page-3).to_s

    #  r<<current_page-2

    #  r<<current_page-1

    #  r<<current_page

    #  if current_page+1<=total_pages

    #    r<<current_page+1

    #  end

    #  if current_page+2<==total_pages

    #    r<<current_page+2

    #  end

    #else

    #  r.concat((1...current_page).to_a)

    #end

    #r<<current_page

    #if current_page+5<=total_pages

    #  r<<current_page+1

    #  r<<current_page+2

    #  r<<"..."+(current_page+3).to_s

    #else

    #  r.concat(((current_page+1)..total_pages).to_a)

    #end

    #

    #

    #

    #puts r

    #r=[]

    #r<<current_page

    #if current_page-2>0

    #  r.unshift current_page-1

    #  

    #end

    #if current_page-5>0 and current_page+2<=total_pages

    #  a[1..

    #end

    #puts a

    if total_pages>6

      if current_page-5>0

        if current_page+2<=total_pages

          a.delete_if{|x| x< current_page-2}

        elsif current_page+1<=total_pages

          a.delete_if{|x| x<current_page-3}

        elsif current_page<=total_pages

          a.delete_if{|x| x<current_page-4}

        end

      end

      if current_page+5>0

        if current_page-2>0

          a.delete_if{|x| x >current_page+2}

        elsif current_page-1>0

          a.delete_if{|x| x>current_page+3}

        elsif current_page>0

          a.delete_if{|x| x>current_page+4}

        end

      end

    end

    if a.first!=1

      a.unshift "..."

      a.unshift 1

    end

    if a.last!=total_pages

      a<<"..."

      a<<total_pages

    end

    puts a

    puts "----------"

    pos=a.index(current_page)

    def gen_link(page)

      page=page.to_s

      #拼接样式输出

      %Q{<span><a href="">#{page}</a></span>}

    end

    r=[]

    a.each_with_index do |elem,index|

      page=elem

      if elem=="..." and index<pos

        page=a[a.index("...")+1]-1

      end

      if elem=="..." and index>pos

        page=a[a.rindex("...")-1]+1

      end

      r<<gen_link(page)

    end

    #修改当前页的样式

    r[pos]=%Q{<span id="page_on"><font>#{a[pos]}</font></span>}

    if current_page==1

      r.unshift %q{<span><font>&#173;上一页</font></span>}

      r.unshift %q{<span><font>&laquo;首页</font></span>}

      r.unshift %q{<div class="page_box">}

    else

      page=current_page-1

      r.unshift %Q{<span><a href="">上一页</a></span>}

      r.unshift %Q{<span><a href="">首页</a></span>}

      r.unshift %q{<div class="page_box">}

    end

    if current_page==total_pages

      r<<%q{<span><a href="">&#173;下一页</a></span>}

      r<<%q{<span><a href="" >&#173;尾页&raquo;</a></span>}

      r<<%q{<span><font>1/共11页</font></span>}

      r<<%q{</div>}

    else

      page=current_page+1

      r<< %Q{<span><a href="">&#173;下一页</a></span>}

      r<< %Q{<span><a href="">&#173;尾页&raquo;</a></span>}

      r<<%q{</div>}

    end

    puts r

    puts r.join("")

  • 相关阅读:
    SE Springer小组之《Spring音乐播放器》需求分析说明书一
    SE Springer小组之《Spring音乐播放器》可行性研究报告一、二(转载)
    SE Springer小组之《Spring音乐播放器》可行性研究报告五、六
    软件分析之QQ
    浅谈《软件工程》
    前端格式
    Django基础
    正则表达式
    任务-线程
    一名奔三的程序猿的困惑
  • 原文地址:https://www.cnblogs.com/lexus/p/1882140.html
Copyright © 2020-2023  润新知