Home How to paginate a modal when the list total can change due to user actions?
Reply: 0

How to paginate a modal when the list total can change due to user actions?

Brian Hellekin
1#
Brian Hellekin Published in 2018-02-08 19:28:36Z

I have a modal which shows a list of people and a "search by name" field where the user can filter the list to show only the people whose name contains the string in question. This functionality uses an Ajax call to an action in the controller which realizes the query using the name as a parameter.

Now, I want to paginate this modal. My project uses the will-paginate gem. Since for every searched name the total number of results (and so the total number of pages) can change, for what I know I can't simply call a will_paginate when generating the modal, unless I regenerate its entire content for every search the user do.

So I'm trying the call the will_paginate command inside the controller and returning it inside the json rendered by the action.

I'm using this answer which suggests to create a helper:

will_paginate_helper.rb

module WillPaginateHelper
  class WillPaginateJSLinkRenderer < WillPaginate::ActionView::LinkRenderer
    def prepare(collection, options, template)
      options[:params] ||= {}
      options[:params]['_'] = nil
      super(collection, options, template)
    end

    protected
    def link(text, target, attributes = {})
      if target.is_a? Fixnum
        attributes[:rel] = rel_value(target)
        target = url(target)
      end
      @template.link_to(target, attributes.merge(remote: true)) do
        text.to_s.html_safe
      end
    end
  end

  def js_will_paginate(collection, options = {})
    will_paginate(collection, options.merge(:renderer => WillPaginateHelper::WillPaginateJSLinkRenderer))
  end
end

And I'm trying to include the helper in the Application Controller to use it in any controller, using this answer. For now, it's only the People controller:

people_controller.rb

class PeopleController < ApplicationController

  def my_method
    people = Person.all
    pagination = ajax_will_paginate people, renderer: BootstrapPagination::Rails      
    render json: ["page" => page, "pagination" => pagination, "list" => people.paginate(:page => page, :per_page => 15).order(name: :asc)]  
  end
end

application_controller.rb

include WillPaginateHelper
class ApplicationController < ActionController::Base
  ...
end

However, it returns this error:

undefined method `ajax_will_paginate' for #<PeopleController:0000000000000000>

In the beginning, I tried to call directly the will_paginate in controller, and I got the same undefined_method error. I googled for an answer to this problem, but I did not found a proper answer for this situation.


So, my question is: How to add pagination to modal when the list inside the modal can be changed due to an user action (and so the total number of pages/content of each specific page)? If needed, I can try another gem, manually adding the page links and changing them by javascript or reformulate the modal logic (as reloading the entire content from a html.erb file with the will_paginate command), but I would prefer to be able to just add the helper I created inside the controller, if possible.c

P.s.: the project is in Rails 4.1.5

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.305611 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO