传递text_field价值will_paginate在Rails的参数 [英] Passing text_field value to will_paginate as parameter in Rails

查看:161
本文介绍了传递text_field价值will_paginate在Rails的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制器的图书,在指数的动作,我使用的 will_paginate 。我也有过滤器的标题作者

每次标题作者的变化,我想提出AJAX调用得到更新版本 @books 的。

在我index.js.erb的文件,我有我的code渲染视图:

  $('#book_list)HTML(<%= escape_javascript(渲染('show_books')).html_safe%>中);
 

我的索引文件是这样的:

 <%= will_paginate @books%>
<醇类=列表无样式的ID =book_list>
  <%=渲染'show_books%>
< / OL>
&其中;%= will_paginate @books%GT;
 

问题是, will_paginate 将不会被AJAX调用后更新。默认情况下我有45本书(30本书每页,这使得2页),AJAX调用后,我得到9书籍。它应该只显示1页,但它仍然显示2页。

如果我把 will_paginate 里面的 book_list ,它会消失。如果我把外面将不会被更新。

另一种选择是,我可以通过自定义的参数 will_paginate ,但我不知道我怎样才能得到一个视图中的文本字段的值,并把它传递作为参数传递给 will_paginate 没有提交。

这些都是输入表单过滤的书:

 < D​​IV CLASS =形组>
  <%=标签:标题,标题%GT;< BR>
  <%= text_field:书:标题,{类:表格控,占位符:书名}%>
< / DIV>
< D​​IV CLASS =形组>
  <%=标签:作家,作者%GT;< BR>
  <%= text_field:书,:作者,{类:表格控,占位符:作者}%>
< / DIV>
 

解决方案

我看到您的帖子在这里<一href="http://stackoverflow.com/questions/6834546/rails3-how-to-pass-param-into-custom-will-paginate-renderer">Rails3:如何通过参数到自定义will_paginate渲染?试图找到一种方法来动态改变由will_paginate产生的页面链接到包括用户信息。

在我的情况下,用户可以选择一个图表显示由will_paginate显示的数据,但默认页面链接不传递任何额外的参数,所以他们选择了图表不会在页面之间保存。<​​/ P>

我的解决办法是重写的链接,包括自定义类中的每个环节,我便可以对网页目标javascript和调整的属性。

下面是我重写code

 模块Pageoverride
  类PaginationListLinkRenderer&LT; WillPaginate :: ::的ActionView LinkRenderer
    保护
      高清链接(文字,目标属性= {})
        如果target.is_a? Fixnum对象
          属性[:REL = rel_value(目标)
          目标= URL(目标)
        结束
        属性[:HREF =目标
        属性[:类] ='chartpagetest
        标签(:一,文本,属性)
      结束
  结束
结束
 

我做了添加唯一的变化:类关键属性阵列,与我自定义的类标识符值

我包括自定义呈现在我的will_paginate通话

 &LT;%= will_paginate(@dbtests,:渲染=&GT; Pageoverride :: PaginationListLinkRenderer)%&GT;
 

和我的JavaScript的目标相匹配我的课,我从变量取当前数据chart_id的广告追加到href属性每个元素的结束。的元素

 的(指数= 0;指数&LT; document.getElementsByClassName(chartpagetest)的长度; ++指数){
                document.getElementsByClassName("chartpagetest")[index].setAttribute('href',document.getElementsByClassName("chartpagetest")[index].getAttribute('href') +与&amp;图表=+ chart_id);
            }
 

在你的情况下,您可以添加一个隐藏属性,以及一类的链接(属性[:隐藏] ='假'),并设置为true如果页面不应该再存在的。

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 get updated version of @books.

In my index.js.erb file I have my code to render the view:

$('#book_list').html("<%= escape_javascript( render('show_books') ).html_safe %>");

My index file looks like this:

<%= will_paginate @books %>
<ol class="list-unstyled" id="book_list">
  <%= render 'show_books' %>
</ol>
<%= will_paginate @books %>

The problem is that, will_paginate will not be updated after AJAX call. By default I have 45 books (30 books per page, that makes 2 pages), after AJAX call I get 9 books. It is supposed to show only 1 page but it still shows 2 pages.

If I put will_paginate inside book_list, it will disappear. If I keep outside it won't be updated.

Another option is that, I can pass custom arguments to will_paginate, but I don't know how can I get value of a text field in a view and pass it as parameter to will_paginate without submitting.

These are input forms to filter books:

<div class="form-group">
  <%= label :title, "Title" %><br>
  <%= text_field :book, :title, {class: 'form-control', placeholder: "Book Title" } %>
</div>
<div class="form-group">
  <%= label :authors, "Authors" %><br>
  <%= text_field :book, :authors, {class: 'form-control', placeholder: "Authors" } %>
</div>

解决方案

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

module Pageoverride
  class PaginationListLinkRenderer < WillPaginate::ActionView::LinkRenderer
    protected
      def link(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)
      end
  end
end

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.

这篇关于传递text_field价值will_paginate在Rails的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆