-
-
Save SitanHuang/d1566828a47fddb9b9291846ecd09962 to your computer and use it in GitHub Desktop.
Ruby Pagination Algorithm HTML Generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module PaginationHelper | |
def paginate(current, max, template, delta: 2) | |
left = current - delta | |
right = current + delta + 1 | |
range = [] | |
range_with_dots = [] | |
l = nil | |
max.times do |_i| | |
i = _i + 1 | |
if i == 1 || i == max || i >= left && i < right | |
range << <<-HTML | |
<a class="num button" href="#{template.sub('%num%', ((i-1) * ITEMS_PER_LOAD).to_s)}">#{i}</a> | |
HTML | |
end | |
end | |
range.each_with_index do |btn, i| | |
if l&.>0 | |
if i - l == 2 | |
range_with_dots << range[l] | |
elsif i - l != 1 | |
range_with_dots << '<span>...</span>' | |
end | |
end | |
range_with_dots << btn | |
l = i | |
end | |
range_with_dots.join("\n") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment