【问题标题】:Updating table data in rails using ajax使用ajax更新rails中的表数据
【发布时间】:2014-05-29 05:21:36
【问题描述】:

我只有一个视图 index.html.erb,它显示了数据库中的所有员工姓名和所有详细信息。我有一个搜索表单,其中的搜索结果应该在表 tbody 中更新,id 为“content”。我的 index.html.erb 第一次渲染得很好。
这是我的 index.html.erb

//my view index.html.erb
<%= form_for :query, :url => { :controller => "home", :action => "show" }, :html =>{:id => "search_form"} do |f|%>
    <div>
        <p>Enter the search keywords:</p>
        <input type="text" id="query_search_key" name="query[search_key]"/>

        <%= f.submit "Search" %>
        <p>Raised By : <select style="width:300px" id="query_raised_by" name="query[raised_by]">
        <option value="-1" selected>Select member</option>
        <% @m_n.each do |m_n| %>
        <option value="<%= m_n.full_name %>"><%= m_n.full_name %></option>
        <% end %>
        </select>
    </div>
<% end %>

<div>
<table id="table" class="display">
    <thead>
    <tr><th>Sr Number</th><th>Raised By</th><th>Title</th></tr>
    </thead>
    <tbody id="content">
        <% if @query%>      
            <% @query.each do |query| %>
                <tr><td><%= query.sr_number %></td><td><%= query.from %></td><td><%= query.title %></td></tr>
            <% end %>
        <% end %>
    </tbody>
</table>
</div>

//ajax call handler in index.html.erb
$("#search_form").submit(function(){
    var dataSet = $(this).serialize();
    $.ajax({
        type: "POST",
        url: $(this).attr("action"),
        data: dataSet,
        success: function(data){
        //update the contents of tbody with id content
        }
    });
    return false;
});

这是我的控制器

//my controller
class HomeController <  ActionController::Base
  def index

    @m_n = (db query getting member names)
    #displaying al records
    @query = (db query)
  end

  def show
    #displaying filtered results
    @query = (db query)
    flash[:notice] = "success: "+ no_of_results + " were found"
    render :partial => 'search'

  end
end

从显示操作中,我只想使用@query 对象更新 index.html.erb 中 id 为“content”的表 tbody。

【问题讨论】:

  • 您想如何从显示页面更新 id 为“内容”的 tbody,您想单击任何按钮还是触发显示页面操作?你能详细说明你的问题吗?
  • 当我在输入搜索键后按下搜索按钮时,控件会显示动作。从那里我想使用@query 对象更新带有过滤结果的表。
  • @SahilGrover 然后我将如何更新表格??
  • 你好 @FredoCorleone 记得冬天快到了.. 也告诉 Michael Corleone

标签: jquery ruby-on-rails ruby ajax ruby-on-rails-3


【解决方案1】:

尝试写一些partial并将部分数据添加到tbody

例如:

控制者:

def show
    #displaying filtered results
    @query = (db query)
    #pass @query to index.html.erb and update only the tbody with id=content which takes @query
    render :partial => 'show_partial'

  end

_show_partial.html.erb:

<% if @query%>      
      <% @query.each do |query| %>
          <tr><td><%= query.sr_number %></td><td><%= query.from %></td><td><%= query.title %></td></tr>
      <% end %>
<% end %>

然后

//ajax call handler in index.html.erb
$("#search_form").submit(function(){
    var dataSet = $(this).serialize();
    $.ajax({
        type: "POST",
        url: $(this).attr("action"),
        data: dataSet,
        success: function(data){
              $('#content').html(data)
        }
    });
    return false;
});

我认为它可以解决你的问题

【讨论】:

  • 感谢一百万 anusha.. 我挣扎了 2 天。我会投票。再次感谢。 :)
  • @Fredo Corleone:很高兴弗雷多。
  • 嘿,我想从控制器发送一个 flash[:notice] 来查看并将其添加到 id 为“content”的 div 中。我已经更新了我的控制器。你能帮忙吗?我在成功函数中直接访问它:$("#content").html("")。但它只呈现一次,并且通知不会在下一次 ajax 调用时更新。
  • @FredoCorleone:在控制器中指定 flash[:notice] 并将其添加到 _show_partial.html.erb
  • 谢谢阿努莎。另一个头痛和另一个完美的答案。谢谢一百万
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-06
  • 1970-01-01
  • 2015-01-14
  • 2020-04-18
  • 1970-01-01
  • 2012-08-20
  • 1970-01-01
相关资源
最近更新 更多