Multiple pagination with kaminari via Ajax

前端 未结 2 1907
南笙
南笙 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.

提交回复
热议问题