0

2 つのフォームがあるページがあります。各フォームは、ページ上の結果をフィルタリングするさまざまな方法です。最初の形式はcollection_select. 2 番目の形式はtext_field_tag. それぞれが同じコントローラに送信され、 を使用してデータが返されますindex.js.erb。Firebug は、Ajax 呼び出しがそれらの両方で機能し、適切な結果を返すことを示していますが、フォームから返された結果のみがtext_field_tag実際にページを更新します。同じコードを使用してindex.js.erb両方の呼び出しから結果を返すため、一方が機能し、もう一方が機能しない理由がわかりません。

index.html.erb:

<h1>Listing info</h1>
<%= render "partials/filter" %> OR
<%= render "search" %>

<div id="filter_table">
  <%= render 'list' %>
</div>

_list.html.erb:

<table class="tablesorter">
<thead><tr><td>Name</td><td>ID</td><td>Description</td></tr></thead>
<tbody>
<% @info.each do |inf| %>
<tr><td><%= inf.name %></td><td><%= inf.id %></td><td><%= inf.desc %></td></tr>
<% end %>
</tbody>
</table>

index.js.erb:

$("#filter_table").replaceWith("<div id=\"filter_table\"><%= escape_javascript(render 'list') %></div>");
$(".tablesorter").tablesorter({widgets: ['zebra']});

_filter.html.erb:

<%= form_tag("#", :method => "get", :remote => true) do %>
  <% cur_id = params[:id].to_i %>
  <%= submit_tag("Filter") %>
<% end %>

_search.html.erb:

<%= form_tag("#", :method => "get", :remote => true) do %>
  Search for an ID: <%= text_field_tag("id") %>
  <%= submit_tag("Search") %>
<% end %>

コントローラー.rb:

def index
  @info = Blahblah
  respond_to do |format|
    format.html
    format.js
  end
end

AJAX 呼び出しからの戻り値の例を次に示します。

動作:

http://hostname/blah?utf8=%E2%9C%93&id=Stuff&commit=Search

$("#filter_table").replaceWith("<div id=\"filter_table\"><table class=\"tablesorter\">\n  <thead>\n  <tr>\n    <th class=\"{sorter: \'digit\'}\">One<\/th>\n    <th>ID<\/th>\n    <th>Person<\/th>\n    <th>Person<\/th>\n    <th>Two<\/th>\n    <th>VID<\/th>\n    <th>type<\/th>\n    <th>Status<\/th>\n    <th>Three<\/th>\n    <th>Four<\/th>\n    <th>Five<\/th>\n  <\/tr>\n  <\/thead>\n  <tbody>\n  <tr>\n    <td><a href=\"/blah/801\" id=\"801\">801<\/a><\/td>\n    <td>Meep<\/td>\n    <td>1814<\/td>\n    <td>Meep2<\/td>\n    <td>Test<\/td>\n    <td>Stuff<\/td>\n    <td>unknown<\/td>\n    <td>submitted<\/td>\n    <td>47<\/td>\n    <td>16.6<\/td>\n    <td>7.9<\/td>\n  <\/tr>\n<\/tbody>\n<\/table>\n\n\n\n</div>");
$(".tablesorter").tablesorter({widgets: ['zebra']});

動作しません:

http://hostname/blah?utf8=%E2%9C%93&filter=123&commit=Filter

$("#filter_table").replaceWith("<div id=\"filter_table\"><table class=\"tablesorter\">\n  <thead>\n  <tr>\n    <th class=\"{sorter: \'digit\'}\">One<\/th>\n    <th>ID<\/th>\n    <th>Person<\/th>\n    <th>Person<\/th>\n    <th>Two<\/th>\n    <th>VID<\/th>\n    <th>type<\/th>\n    <th>Status<\/th>\n    <th>Three<\/th>\n    <th>Four<\/th>\n    <th>Five<\/th>\n  <\/tr>\n  <\/thead>\n  <tbody>\n  <tr>\n    <td><a href=\"/blah/801\" id=\"801\">801<\/a><\/td>\n    <td>Meep<\/td>\n    <td>1814<\/td>\n    <td>Meep2<\/td>\n    <td>Test<\/td>\n    <td>Stuff<\/td>\n    <td>unknown<\/td>\n    <td>submitted<\/td>\n    <td>47<\/td>\n    <td>16.6<\/td>\n    <td>7.9<\/td>\n  <\/tr>\n<\/tbody>\n<\/table>\n\n\n\n</div>");
$(".tablesorter").tablesorter({widgets: ['zebra']});

Firebug は、両方の呼び出しからの Ajax リターンが正常に機能することを示していreplaceWithますが、フィルター フォームから返されたものに対しては機能しませんが、検索フォームから返されたものに対しては機能します。お気付きかもしれませんが、サンプルの Ajax の結果はまったく同じですが、なぜか最初の結果は機能し、2 番目の結果は機能しません。両方に同じindex.js.erbコードが使用されます。

アップデート:

また、上記の index.js.erb を次のように置き換えてみました。

$("#filter_table").empty();
$("#filter_table").append("<div id=\"filter_table\"><%= escape_javascript(render 'list') %></div>");
$(".tablesorter").tablesorter({widgets: ['zebra']});

これは、上記と同じ方法で機能します。を使用したtext_field_tagクエリと を使用したクエリは、どちらも同じコントローラーと同じビュー コードをcollection_select使用し、両方とも同じ AJAX 結果を返しますが、機能しません。index.js.erb

更新 2:

このバージョンを使用するcollection_selectと、AJAX 呼び出しが jquery 行 2215 に戻った後、firebug で警告が表示されます。

Use of attributes' specified attribute is deprecated.  It always returns true.
return !val || val.specified ? elem.val : elem.text; 

text_field_tagAJAX 呼び出し ではこの警告は表示されません。

4

1 に答える 1

0

私はあなたのコードを実行しようとしました(ただし、 this なしで、正常に動作します。呼び出し$(".tablesorter").tablesorter({widgets: ['zebra']});を貼り付けました:alert

$("#filter_table").empty();
alert("1");
$("#filter_table").append("<%= j render 'list' %>");

そして、「1」を見たとき、テーブルはありませんでした。あなたのコードは問題ありません。多分それはすべてtablesorter電話についてですか?

于 2012-09-28T12:29:26.633 に答える