Skip to content Skip to sidebar Skip to footer

Passing Text_field Value To Will_paginate As Parameter In Rails

I have a controller for Books, in index action I am using will_paginate. I also have filters for title and authors. Every time title or authors changes, I am making ajax call to g

Solution 1:

I saw your post over here Rails3: How to pass param into custom will_paginate renderer? while trying to find a way to dynamically change the page links generated by will_paginate to include information from the user.

In my case the user can choose a chart to display the data shown by will_paginate, but the default page links don't pass any extra parameters so the chart they chose is not saved between pages.

My solution was to override the link to include a custom class in each of the links, which I could then have the javascript on the page target and adjust the attributes of.

Here is my override code

modulePageoverrideclassPaginationListLinkRenderer < WillPaginate::ActionView::LinkRenderer
    protected
      deflink(text, target, attributes = {})if target.is_a? Fixnum
          attributes[:rel] = rel_value(target)
          target = url(target)
        end
        attributes[:href] = target
        attributes[:class] = 'chartpagetest'
        tag(:a, text, attributes)
      endendend

The only change I made was adding a :class key to the attributes array, with the value of my custom class identifer

I include the custom renderer in my will_paginate call

<%= will_paginate(@dbtests, :renderer => Pageoverride::PaginationListLinkRenderer) %>

And my Javascript to target the elements that match my class, I am taking the current data from the variable "chart_id" ad appending it to the end of "href" attribute for each element.

for (index = 0; index<document.getElementsByClassName("chartpagetest").length;++index) {
                document.getElementsByClassName("chartpagetest")[index].setAttribute('href',document.getElementsByClassName("chartpagetest")[index].getAttribute('href') + "&chart="+chart_id);
            }

In your case you may be able to add a "hidden" attribute as well as a class to the links (attributes[:hidden] = 'false'), and set that to "true" if the page should no longer exist.

Post a Comment for "Passing Text_field Value To Will_paginate As Parameter In Rails"