moduleWillPaginate moduleViewHelpers # This class does the heavy lifting of actually building the pagination # links. It is used by +will_paginate+ helper internally. classLinkRenderer < LinkRendererBase # * +collection+ is a WillPaginate::Collection instance or any other object # that conforms to that API # * +options+ are forwarded from +will_paginate+ view helper # * +template+ is the reference to the template being rendered defprepare(collection, options, template) super(collection, options) @template = template @container_attributes = @base_url_params = nil end
# Process it! This method returns the complete HTML string which contains # pagination links. Feel free to subclass LinkRenderer and change this # method as you see fit. defto_html html = pagination.map do|item| item.is_a?(Fixnum) ? page_number(item) : send(item) end.join(@options[:link_separator]) @options[:container] ? html_container(html) : html end
# Returns the subset of +options+ this instance was initialized with that # represent HTML attributes for the container element of pagination links. defcontainer_attributes @container_attributes||= @options.except(*(ViewHelpers.pagination_options.keys + [:renderer] - [:class])) end protected # page_number方法显示分页元素 defpage_number(page) unless page == current_page link(page, page, :rel => rel_value(page)) else tag(:em, page, :class => 'current') end end # gap方法在页数超过设定值时用...代替 defgap text = @template.will_paginate_translate(:page_gap) { '…' } %(<span class="gap">#{text}</span>) end # previous_page方法显示上一页 defprevious_page num = @collection.current_page > 1 && @collection.current_page - 1 previous_or_next_page(num, @options[:previous_label], 'previous_page') end # next_page方法显示下一页 defnext_page num = @collection.current_page < total_pages && @collection.current_page + 1 previous_or_next_page(num, @options[:next_label], 'next_page') end # 边界值按钮 defprevious_or_next_page(page, text, classname) if page link(text, page, :class => classname) else tag(:span, text, :class => classname + ' disabled') end end defhtml_container(html) tag(:div, html, container_attributes) end # Returns URL params for +page_link_or_span+, taking the current GET params # and <tt>:params</tt> option into account. defurl(page) raise NotImplementedError end private
defparam_name @options[:param_name].to_s end
# 私有方法, 构建a标签 deflink(text, target, attributes = {}) if target.is_a? Fixnum attributes[:rel] = rel_value(target) target = url(target) end attributes[:href] = target tag(:a, text, attributes) end # 私有方法, 包裹标签 deftag(name, value, attributes = {}) string_attributes = attributes.inject('') do|attrs, pair| unless pair.last.nil? attrs << %( #{pair.first}="#{CGI::escapeHTML(pair.last.to_s)}") end attrs end "<#{name}#{string_attributes}>#{value}</#{name}>" end
defrel_value(page) case page when@collection.current_page - 1; 'prev' + (page == 1 ? ' start' : '') when@collection.current_page + 1; 'next' when1; 'start' end end
defsymbolized_update(target, other) other.each do|key, value| key = key.to_sym existing = target[key] if value.is_a?(Hash) and (existing.is_a?(Hash) or existing.nil?) symbolized_update(existing || (target[key] = {}), value) else target[key] = value end end end end end end
moduleWillPaginate moduleViewHelpers # This class does the heavy lifting of actually building the pagination # links. It is used by +will_paginate+ helper internally. classLinkRenderer < LinkRendererBase # * +collection+ is a WillPaginate::Collection instance or any other object # that conforms to that API # * +options+ are forwarded from +will_paginate+ view helper # * +template+ is the reference to the template being rendered defprepare(collection, options, template) super(collection, options) @template = template @container_attributes = @base_url_params = nil end
# Process it! This method returns the complete HTML string which contains # pagination links. Feel free to subclass LinkRenderer and change this # method as you see fit. defto_html html = pagination.map do|item| item.is_a?(Fixnum) ? page_number(item) : send(item) end.join(@options[:link_separator]) @options[:container] ? html_container(html) : html end
# Returns the subset of +options+ this instance was initialized with that # represent HTML attributes for the container element of pagination links. defcontainer_attributes @container_attributes||= @options.except(*(ViewHelpers.pagination_options.keys + [:renderer] - [:class])) end protected # 修改后,我使用私有方法tag,在link外面套了一层li,同时修改了class属性 defpage_number(page) unless page == current_page # link(page, page, :rel => rel_value(page)) tag :li, link(page, page, :rel => rel_value(page)), :class => 'waves-effect' else # tag(:em, page, :class => 'current') tag(:li, link(page, '#!', :rel => rel_value(page)), :class => 'active') end end defgap text = @template.will_paginate_translate(:page_gap) { '…' } %(<span class="gap">#{text}</span>) end # 这里没有修改全局变量@options,使用打开类最好不要修改全局变量。所以直接改了icon defprevious_page num = @collection.current_page > 1 && @collection.current_page - 1 # previous_or_next_page(num, @options[:previous_label], 'previous_page') previous_or_next_page(num, 'chevron_left', 'previous_page') end # 这里没有修改全局变量@options,使用打开类最好不要修改全局变量。所以直接改了icon defnext_page num = @collection.current_page < total_pages && @collection.current_page + 1 # previous_or_next_page(num, @options[:next_label], 'next_page') previous_or_next_page(num, 'chevron_right', 'next_page') end # 修改了边界值的按钮,增加了局部变量icon_item用于google icon defprevious_or_next_page(page, text, classname) icon_item = tag :i, text, :class => 'material-icons' if page # link(text, page, :class => classname) tag(:li, link(icon_item, page), :class => 'waves-effect') else # tag(:span, text, :class => classname + ' disabled') tag(:li, link(icon_item, '#!'), :class => 'disabled') end end defhtml_container(html) tag(:div, html, container_attributes) end # Returns URL params for +page_link_or_span+, taking the current GET params # and <tt>:params</tt> option into account. defurl(page) raise NotImplementedError end private
defparam_name @options[:param_name].to_s end
deflink(text, target, attributes = {}) if target.is_a? Fixnum attributes[:rel] = rel_value(target) target = url(target) end attributes[:href] = target tag(:a, text, attributes) end deftag(name, value, attributes = {}) string_attributes = attributes.inject('') do|attrs, pair| unless pair.last.nil? attrs << %( #{pair.first}="#{CGI::escapeHTML(pair.last.to_s)}") end attrs end "<#{name}#{string_attributes}>#{value}</#{name}>" end
defrel_value(page) case page when@collection.current_page - 1; 'prev' + (page == 1 ? ' start' : '') when@collection.current_page + 1; 'next' when1; 'start' end end
defsymbolized_update(target, other) other.each do|key, value| key = key.to_sym existing = target[key] if value.is_a?(Hash) and (existing.is_a?(Hash) or existing.nil?) symbolized_update(existing || (target[key] = {}), value) else target[key] = value end end end end end end