Class: RailsAdminContent::Paginate

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_admin_content/paginate.rb

Instance Method Summary collapse

Constructor Details

#initialize(scope, options = {}) ⇒ Paginate

Returns a new instance of Paginate.



4
5
6
# File 'lib/rails_admin_content/paginate.rb', line 4

def initialize(scope, options = {})
  @scope, @options = scope, options
end

Instance Method Details

#to_sObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rails_admin_content/paginate.rb', line 8

def to_s
  return "" unless @scope
  current_page, total_pages = @options[:spage].to_i, RailsAdminContent::Client.total_nums / @options[:stint] + 1
  # return "共#{RailsAdminContent::Client.total_nums}条数据" if total_pages <= 1
  param_name = @options["param_name"] || "page"
  url = @options["url"] || ""
  helpers. :div, :class => "pagination" do
    helpers. :ul do
      result = ''
      result += helpers.(:li, class: 'previous'){helpers.link_to("‹ 上一页","#{url}?#{param_name}=#{current_page-1}")} if current_page.to_i > 1
      if total_pages > 8
        1.upto(3) { |i| result << page_link(i, current_page) }
        if current_page > 1 and current_page < total_pages
          result << helpers.(:li, helpers.link_to("...",'#'), class: 'disabled') if current_page > 5
          min = current_page > 4 ? current_page-1 : 4
          max = current_page < total_pages-4 ? current_page+2 : total_pages-2
          (min..max-1).each { |i| result << page_link(i, current_page) } if max >= min+1
          result << helpers.(:li, helpers.link_to("...", "#"), class: 'disabled') if current_page < total_pages-4
        else
          result << helpers.(:li, helpers.link_to("...",'#'), class: 'disabled')
        end
        (total_pages-2..total_pages).each{ |i| result << page_link(i, current_page) }
      else
        (1..total_pages).each{ |i| result << page_link(i, current_page) }
      end
      result += helpers.(:li, class: "next"){helpers.link_to("下一页 ›", "#{url}?#{param_name}=#{current_page+1}")} if current_page < total_pages
      result += helpers.(:li, class: 'disabled total_pages'){helpers.link_to "#{RailsAdminContent::Client.total_nums}条数据", "#"}
      result.html_safe
    end
  end

end