completed pagination with first and last and also next and previous

This commit is contained in:
Harry Bomrah 2014-06-11 17:52:29 +08:00
parent fbbd75cdce
commit 4f0b05fcc7
4 changed files with 68 additions and 22 deletions

View File

@ -191,28 +191,9 @@ module ApplicationHelper
html = html.gsub("%7B%7B#{key}%7D%7D",value.to_s)
end
end
file = File.join(Rails.root, 'app', 'templates', "#{@key}", 'home', "pagination.html.erb")
total_pages = data['total_pages'].to_i rescue 1
if total_pages > 1
if File.exists?file
file = File.open(file)
doc = Nokogiri::HTML(file, nil, "UTF-8")
file.close
paginationobj = doc.css("*[data-pagination='true']").first
in_html = paginationobj.inner_html
f_html = ""
(1..total_pages).each do |i|
h = in_html
h = h.gsub("%7B%7Bpagination_link%7D%7D","?page_no=#{i.to_s}")
h = h.gsub("{{page_number}}",i.to_s)
h = h.gsub("{{pagination_active}}",(i == OrbitHelper.page_number ? "active" : ""))
f_html = f_html + h
end
paginationobj.inner_html = f_html
html = html.gsub("{{pagination_goes_here}}",paginationobj.to_s)
else
html = html.gsub("{{pagination_goes_here}}","");
end
html = html.gsub("{{pagination_goes_here}}",create_pagination(total_pages))
else
html = html.gsub("{{pagination_goes_here}}","");
end
@ -357,4 +338,62 @@ module ApplicationHelper
html.html_safe
end
def create_pagination(total_pages)
file = File.join(Rails.root, 'app', 'templates', "#{@key}", 'home', "pagination.html.erb")
html = ""
if File.exists?file
file = File.open(file)
doc = Nokogiri::HTML(file, nil, "UTF-8")
file.close
paginationobj = doc.css("*[data-pagination='true']").first
in_html = first = last = nextpage = prevpage = paginationobj.inner_html
f_html = ""
current_page_number = OrbitHelper.page_number
if current_page_number > 1
first = first.gsub("%7B%7Bpagination_link%7D%7D","?page_no=1")
first = first.gsub("{{page_number}}","First")
first = first.gsub("{{pagination_active}}","")
f_html = f_html + first
end
start_number = current_page_number - 2
end_number = current_page_number + 2
if end_number > total_pages
end_number = total_pages
start_number = total_pages - 4
end
if start_number < 1
start_number = 1
end_number = 5
end
(start_number..end_number).each do |i|
h = in_html
h = h.gsub("%7B%7Bpagination_link%7D%7D","?page_no=#{i.to_s}")
h = h.gsub("{{page_number}}",i.to_s)
h = h.gsub("{{pagination_active}}",(i == current_page_number ? "active" : ""))
f_html = f_html + h
end
if current_page_number > 1
prevpage = prevpage.gsub("%7B%7Bpagination_link%7D%7D","?page_no=#{current_page_number - 1}")
prevpage = prevpage.gsub("{{page_number}}","&laquo;")
prevpage = prevpage.gsub("{{pagination_active}}","")
f_html = f_html + prevpage
end
if current_page_number < total_pages
nextpage = nextpage.gsub("%7B%7Bpagination_link%7D%7D","?page_no=#{current_page_number + 1}")
nextpage = nextpage.gsub("{{page_number}}","&raquo;")
nextpage = nextpage.gsub("{{pagination_active}}","")
f_html = f_html + nextpage
last = last.gsub("%7B%7Bpagination_link%7D%7D","?page_no=#{total_pages}")
last = last.gsub("{{page_number}}","Last")
last = last.gsub("{{pagination_active}}","")
f_html = f_html + last
end
paginationobj.inner_html = f_html
html = paginationobj.to_s
end
html
end
end

View File

@ -17,6 +17,9 @@ module OrbitHelper
def self.set_page_number(page)
@page_number = page
if @page_number == 0
@page_number = 1
end
end
def self.page_number

View File

@ -1,3 +1,7 @@
<ul class="pagination" data-pagination="true">
<li class="{{pagination_active}}"><a href="{{pagination_link}}">{{page_number}}</a></li>
</ul>
<!-- <li><a href="#">First</a></li>
<li><a href="#">&laquo;</a></li>
<li><a href="#">&raquo;</a></li>
<li><a href="#">Last</a></li> -->

View File

@ -127,7 +127,7 @@
</div>
</div>
<div id="page-module" class="module" onclick="importModule('pages')">
<div id="pages-module" class="module" onclick="importModule('pages')">
<div class="lead muted">
<i class="icons-newspaper"></i><br/>
<%= t("page_content.page") %>