通过 Ajax 使用 kaminari 进行多分页
我想通过 Ajax 使用 Kaminari 应用多个分页,现在这是我的控制器代码,
def user_note
@user = current_user
@notes = Bookmark.where('user_id = ? && note is not NULL',current_user.id).order('created_at DESC').page(params[:page_1]).per(4)
@bookmarks = Bookmark.where('user_id = ? && note is NULL',current_user.id).order('created_at DESC').page(params[:page_2]).per(4)
respond_to do |format|
format.html
format.xml{ render :xml => @user}
end end
现在用于视图我有两个部分来渲染这个数组
<div id="bookmarks">
<%= render :partial =>"users/bookmark",:locals => { :bookmark => @bookmarks} %>
</div>
<%= paginate @bookmarks,:remote => true, :param_name => 'page' %>
内部部分是
<% bookmark.each do |bookmar| %>
<%= render :partial => 'show_bookmark.html.erb' , :locals => { :bookma => bookmar} %>
<%end%>
分页更新的脚本正在一个单独的文件中处理
$('#bookmarks').html('<%= escape_javascript render(:partial =>"users/bookmark",:locals => { :bookmark => @bookmarks}) %>');
$('#paginator').html('<%= escape_javascript(paginate(@bookmarks, :remote => true).to_s) %>');
但是通过做每件事它不是更新到页面状态既不包含在页面中。
I want to apply multiple pagination with Kaminari via Ajax now here is my code for controller
def user_note
@user = current_user
@notes = Bookmark.where('user_id = ? && note is not NULL',current_user.id).order('created_at DESC').page(params[:page_1]).per(4)
@bookmarks = Bookmark.where('user_id = ? && note is NULL',current_user.id).order('created_at DESC').page(params[:page_2]).per(4)
respond_to do |format|
format.html
format.xml{ render :xml => @user}
end end
now for views i have two partials to render this arrays
<div id="bookmarks">
<%= render :partial =>"users/bookmark",:locals => { :bookmark => @bookmarks} %>
</div>
<%= paginate @bookmarks,:remote => true, :param_name => 'page' %>
inner partial is
<% bookmark.each do |bookmar| %>
<%= render :partial => 'show_bookmark.html.erb' , :locals => { :bookma => bookmar} %>
<%end%>
script for pagination update is being handled in a separate file
$('#bookmarks').html('<%= escape_javascript render(:partial =>"users/bookmark",:locals => { :bookmark => @bookmarks}) %>');
$('#paginator').html('<%= escape_javascript(paginate(@bookmarks, :remote => true).to_s) %>');
But by doing every thing it is not updating to state of page neither the contain in the page.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您缺少在这一行传递参数,
我认为它应该是这样的
,并且您也在这一行传递了错误的参数,
它应该是这样的
,还请检查您是否将响应正确发送到 JS 文件。
you are missing to pass params at this line
i think it should be like this
and you are also passing wrong param at this line
it should be like this
and please also check that whether you are sending the response correctly to the JS file or not.
我发现这个问题是在同一页面上搜索多个模型的分页。我不清楚 @notes 集合的分页是如何工作的,但正如所介绍的,解决方案将仅通过 AJAX 对 @bookmarks 集合进行分页。
如果您想通过 html 维护两个集合的分页,或者更改 js 文件以呈现两个集合,则会出现问题。
我为我的类似问题实现了一个解决方案,如下所示:
@notes
(包括其分页助手),另一个呈现@bookmarks 及其分页助手
remote: true
的分页链接一个重新渲染两个部分的 js 视图,类似于
This is the key: Pagination links that take the current page of other model 作为参数。这样您就不会“丢失”您在其他模型中所在的页面。
再说一遍,我不太清楚提问者的系统应该如何运作,但这个解决方案将允许用户通过 AJAX 或 html 翻阅两个模型,而不会丢失他们在另一个模型中的“位置”。
I found this question searching for paginating multiple models on the same page. It's not clear to me how the pagination for the @notes collection is intended to work, but as presented, the solutions will only paginate the @bookmarks collection via AJAX.
There will be issues if you want to maintain pagination for both collections via html OR if you change the js file to render both collections.
I implemented a solution to my similar problem like this:
@notes
(including its pagination helper) and another that renders@bookmarks
with its pagination helperremote: true
One js view that re-renders the two partials, something like
This is the key: Pagination links that take the current page of the other model as a parameter. This way you do not "lose" which page you are on in the other model.
Again, I'm not really clear on how the asker's system is supposed to function, but this solution will allow user to page through both models without losing their "place" in the other model via AJAX or html.