Multiple pagination with kaminari via Ajax

前端 未结 2 1902
南笙
南笙 2021-02-04 06:57

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         


        
相关标签:
2条回答
  • 2021-02-04 07:56

    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.

    0 讨论(0)
  • 2021-02-04 07:57

    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.

    0 讨论(0)
提交回复
热议问题