欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-09 21:26:00
文章转载自:http://www.pythonheidong.com/blog/article/2186/ #coding:utf-8 total_pages=11 current_page=10 a=(1..total_pages).to_a #

文章转载自:

#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("")

 

 

 

 

 

 

文章转载自: