通过 Ajax 使用 kaminari 进行多分页

发布于 2024-12-29 05:41:14 字数 1306 浏览 1 评论 0原文

我想通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北风几吹夏 2025-01-05 05:41:14

您缺少在这一行传递参数,

$('#paginator').html('<%= escape_javascript(paginate(@bookmarks, :remote => true).to_s) %>');

我认为它应该是这样的

$('#paginator').html('<%= escape_javascript(paginate(@bookmarks, :remote => true, :param_name => 'page_2').to_s) %>');

,并且您也在这一行传递了错误的参数,

<%= paginate @bookmarks,:remote => true, :param_name => 'page' %>

它应该是这样的

<%= paginate @bookmarks,:remote => true, :param_name => 'page_2' %>

,还请检查您是否将响应正确发送到 JS 文件。

you are missing to pass params at this line

$('#paginator').html('<%= escape_javascript(paginate(@bookmarks, :remote => true).to_s) %>');

i think it should be like this

$('#paginator').html('<%= escape_javascript(paginate(@bookmarks, :remote => true, :param_name => 'page_2').to_s) %>');

and you are also passing wrong param at this line

<%= paginate @bookmarks,:remote => true, :param_name => 'page' %>

it should be like this

<%= paginate @bookmarks,:remote => true, :param_name => 'page_2' %>

and please also check that whether you are sending the response correctly to the JS file or not.

网白 2025-01-05 05:41:14

我发现这个问题是在同一页面上搜索多个模型的分页。我不清楚 @notes 集合的分页是如何工作的,但正如所介绍的,解决方案将仅通过 AJAX 对 @bookmarks 集合进行分页。

如果您想通过 html 维护两个集合的分页,或者更改 js 文件以呈现两个集合,则会出现问题。

我为我的类似问题实现了一个解决方案,如下所示:

  1. 一个 html 视图,它呈现具有两个部分的模板:一个用于 @notes (包括其分页助手),另一个呈现 @bookmarks 及其分页助手
  2. 标有 remote: true 的分页链接
  3. 一个重新渲染两个部分的 js 视图,类似于

    $('#notes_container').html('<%= j(渲染部分:'pagination_notes_list') %>');
    $('#bookmarks_container').html('<%= j(渲染部分:'pagination_bookmarks_list'); %>');
    
  4. This is the key: Pagination links that take the current page of other model 作为参数。这样您就不会“丢失”您在其他模型中所在的页面。

    <%= 分页@notes, param_name: 'page_1', params: {page_2: params[:page_2]} %>;
    <%= 分页@bookmarks, param_name: 'page_2', params: {page_1: params[:page_1]} %>
    

再说一遍,我不太清楚提问者的系统应该如何运作,但这个解决方案将允许用户通过 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:

  1. An html view that renders a template with two partials: one for @notes (including its pagination helper) and another that renders @bookmarks with its pagination helper
  2. Pagination links marked with remote: true
  3. One js view that re-renders the two partials, something like

    $('#notes_container').html('<%= j(render partial: 'paginated_notes_list') %>');
    $('#bookmarks_container').html('<%= j(render partial: 'paginated_bookmarks_list'); %>');
    
  4. 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.

    <%= paginate @notes, param_name: 'page_1', params: {page_2: params[:page_2]} %>
    <%= paginate @bookmarks, param_name: 'page_2', params: {page_1: params[:page_1]} %>
    

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文