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

作者: lexus
发布时间:2015-07-13 11:31:29

#coding:utf-8
total_pages=11
current_page=10
a=(1..total_pages).to_a
  #<div ><span><font>&laquo;首页</font></span><span><font>&#173;上一页</font></span>
#<span id="page_on"><font>1</font></span>
#<span><a >2</a></span>
#<span><a >3</a></span>
#<span><a >4</a></span>
#<span><a >5</a></span>
#<span><a >...</a></span>
#<span><a >11</a></span>
#<span><a >&#173;下一页</a></span>
#<span><a >&#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 ><span><font>&laquo;首页</font></span><span><font>&#173;上一页</font></span>

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

#<span><a >2</a></span>

#<span><a >3</a></span>

#<span><a >4</a></span>

#<span><a >5</a></span>

#<span><a >...</a></span>

#<span><a >11</a></span>

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

#<span><a >&#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 >#{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 >}

else

  page=current_page-1

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

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

  r.unshift %q{<div >}

end

if current_page==total_pages

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

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

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

  r<<%q{</div>}

else

  page=current_page+1

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

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

  r<<%q{</div>}

 

end

puts r

puts r.join("")

 

 

 

 

 

 

标签: Ruby
来源:http://www.cnblogs.com/lexus/archive/2010/11/19/1882140.html

推荐: