当我的 AJAX 部分加载到 Rails 3.1 中时,如何隐藏搜索按钮并显示微调器?
我有一个部分返回 Amazon-ecs API 的结果(搜索书籍)。返回值大约需要 2.5 秒,因此我想添加一个微调器(最好隐藏搜索按钮),但我很难让它工作。
我已经用 JavaScript 刷新了部分结果。我只是希望该按钮给我一个微调器(并隐藏搜索按钮),直到部分完成重新加载。
这是我的主视图 (index.html.erb),它呈现部分搜索结果,每个结果都作为 AmazonItems 中的对象临时添加:
<%= form_tag amazon_items_path, :method => :get, :remote => true do %>
<p>
<%= text_field_tag :query, params[:query] %>
<span id="spinner" style="display:none">
<%= image_tag 'ajax-loader.gif' %>
</span>
<span id="search_button">
<%= submit_tag "Search" %>
</span>
</p>
<% end %>
<h1>Searching Amazon</h1>
<div id="amazon_returned">
<%= render 'amazon_returned' %>
</div>
我唯一的 JS 代码位于 update.js.erb 文件中,如下所示:
$("#amazon_returned").html("<%=j render 'amazon_returned' %>");
我确信这很容易,但我无法让它发挥作用。谢谢!
编辑:我的部分“_amazon_returned.html.erb”看起来像
<table>
<% for amazon_item in @amazon_items %>
<td> amazon_item.title </td>
...ETC...
<% end %>
</table>
I have a partial which returns results from the Amazon-ecs API (searches for books). It takes about 2.5 seconds to return values, so I want to add a spinner (and ideally hide the search button) but I've had a tough time getting it to work.
I already have javascript refreshing the partial with the results. I'd just like the button to give me a spinner (and hide the search button) until the partial finishes reloading.
Here is my main view (index.html.erb) which renders a partial of search results, each of which is added temporarily as an object in AmazonItems:
<%= form_tag amazon_items_path, :method => :get, :remote => true do %>
<p>
<%= text_field_tag :query, params[:query] %>
<span id="spinner" style="display:none">
<%= image_tag 'ajax-loader.gif' %>
</span>
<span id="search_button">
<%= submit_tag "Search" %>
</span>
</p>
<% end %>
<h1>Searching Amazon</h1>
<div id="amazon_returned">
<%= render 'amazon_returned' %>
</div>
My only JS code is in the update.js.erb file, which looks like:
$("#amazon_returned").html("<%=j render 'amazon_returned' %>");
I'm sure this is easy, but I can't get it work. Thanks!
EDIT: my partial "_amazon_returned.html.erb" looks like
<table>
<% for amazon_item in @amazon_items %>
<td> amazon_item.title </td>
...ETC...
<% end %>
</table>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以挂钩
before
事件来执行此操作:然后,在您的
update.js.erb
中,再次添加以下内容以将它们切换回来:You can hook into the
before
event to do this:Then, in your
update.js.erb
, add this again to toggle them back:按照 Dylan 的答案,将切换添加到 update.js.erb 并将以下内容插入到视图中:
非常酷!再次感谢您的帮助。
Went with Dylan's answer, added the toggle to the update.js.erb and inserted the following into the view:
Very cool! Thanks again for the help.