如何分页结合锚ID [英] how to paginate combined with anchor id

查看:31
本文介绍了如何分页结合锚ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Ruby on Rails 非常陌生,在尝试修复我公司网站上的一些错误时正在学习.我正在尝试对与特定锚点组合的记录集合进行分页,即当用户单击下一页/上一页时,分页发生并且用户登陆页面的特定部分.这是我的代码目前的样子:

I'm very new to Ruby on Rails and learning while trying to fix some bugs on my company website. I'm trying to paginate a collection of records combined with a particular anchor, i.e. when user clicks on the next/previous page, the pagination happens and user lands on a particular section of the page. This is how my code looks at the moment:

<%= page_navigation_links @student_logs, :page %></p>

控制器:

@student_logs.paginate(:page => params[:page], :per_page => 10)

application_helper

def page_navigation_links(pages, param_name=:page)

  will_paginate(pages, :class => 'pagination', :inner_window => 2, :outer_window => 0, :renderer => BootstrapHelper::LinkRenderer, :previous_label => '&larr;'.html_safe, :next_label => '&rarr;'.html_safe, :param_name => param_name)

end

这适用于我的分页,但我需要在此代码中添加对锚 ID"的引用,例如学生日志,以便当用户导航到不同的页面时,浏览器将用户导航到带有 id 的标题:student_logs".

This works fine for my pagination, but I need to fit a reference to an 'anchor id' into this code, like Student Logs, so that when user navigates to a different page the browser navigates the user to the heading with id: "student_logs".

我不知道如何做到这一点.有人可以帮忙吗?

I can't figure out how to do this. Can anyone help?

推荐答案

我没有直接的经验.但是,此链接 显示了一种方法.我无法测试,但您当然可以.

I do not have direct experience with it. However, this link shows a way. I could not test but you can certainly do.

<%= will_paginate @posts, :params => {:anchor => i} %>

如果可行,那么您可以在您的助手中使用这种方式:

If this works, then you can utilize this way in your helper:

def page_navigation_links(pages, param_name=:page)
  will_paginate(pages, :params => {:anchor => "#ANCHOR"}, :class => 'pagination', :inner_window => 2, :outer_window => 0, :renderer => BootstrapHelper::LinkRenderer, :previous_label => '&larr;'.html_safe, :next_label => '&rarr;'.html_safe, :param_name => param_name)
end

您可以根据需要替换 #ANCHOR.

You can replace #ANCHOR as you need.

这篇关于如何分页结合锚ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆