【问题标题】:Rails: Fill Table with resulting data from a search boxRails:用搜索框的结果数据填充表格
【发布时间】:2014-05-28 17:34:28
【问题描述】:

正如问题所说,我正在尝试使用 rails 4 中搜索框的结果将数据填充到表格中。我用谷歌搜索,但找不到答案,甚至找不到有关如何执行此操作的示例。

这是我的看法:

<% title @sale.client_name %>
<div class="hero-unit">
    <CENTER><H1><%= @sale.client_name %> </H1>
    <br>
    <p>
        Numero de Factura:<br>
        <%= @sale.check_number %>
    </p>
    <p>
        Nit del Cliente:<br>
        <%= @sale.nit%>
    </p>
    <p>
        Fecha de venta:<br>
        <%= @sale.created_at %>
    </p>
    <p>    
        Total Venta:<br>
        <%= @sale.price %>
   </p>
   <p>
       <h2><Center><%= link_to_function "Ver Detalle de Venta", "$('#hide').toggle()" %>          </Center></h2>
<div id="hide" style="display:none">
    <table class="table table-striped">
        <thead>
            <tr>
                <th><Center>Nombre</Center></th>
                <th><Center>Codigo del SubProducto</Center></th>
                <th><Center>Marca</Center></th>
                <th><Center>Categoria</Center></th>
                <th><Center>Precio</Center></th>        
            </tr>
        </thead>
        <tbody>
            <% @sale.subproducts.each do |subproduct| %>
                <tr>
                    <td><CENTER><%= subproduct.product.name %></CENTER></td>
                    <td><CENTER><%= subproduct.code %></CENTER></td>
                    <td><CENTER><%= subproduct.product.brand %></CENTER></td>
                    <td><CENTER><%= subproduct.product.category %></CENTER></td>
                    <td><CENTER><%= subproduct.product.sale_price %></CENTER></td>
                    <td><%= link_to 'Eliminar de venta', :controller => :subproducts,  action => 'eliminar_subproducto_venta', :id => subproduct.id, :sale_id => @sale.id %></td>
                </tr>
            <% end %>
        </tbody>
    </table>
</div>
</p>

<% if @sale.confirmed == false %>
    <p>
        <h2><Center><%= link_to_function "Agregar Producto a venta",   "$('#subproduct').toggle()" %></Center></h2>
    <div id="subproduct" style="display:none">
        <table class="table table-striped">
            <thead>
                <tr>
                      <th><Center>Nombre</Center></th>
                      <th><Center>Codigo del SubProducto</Center></th>
                      <th><Center>Marca</Center></th>
                  <th><Center>Categoria</Center></th>
                  <th><Center>Precio De Venta</Center></th>        
            </tr>
        </thead>
        <tbody>
            <% @subproducts.each do |subproduct| %>
                <tr>
                    <%if subproduct.available == true || subproduct.available == "NULL"%>
                    <td><CENTER><%= subproduct.product.name %></CENTER></td>
                    <td><CENTER><%= subproduct.code %></CENTER></td>
                    <td><CENTER><%= subproduct.product.brand %></CENTER></td>
                    <td><CENTER><%= subproduct.product.category %></CENTER></td>
                    <td><CENTER><%= subproduct.product.sale_price %></CENTER></td>

  <td><%= link_to 'Agregar a Venta', :controller => :subproducts, :action =>    'agregar_subproducto_venta', :id => subproduct.id, :sale_id => @sale.id %></td>
                        <% end %>
                    </tr>
               <% end %>
        </tbody>
    </table>
</div>
</p>
<%if @sale.price !=0 %>
<%= link_to 'Confirmar Venta', :controller => :sales, :action => 'confirm_sale',  :identificator => @sale.id%><br>
<%end%>

<%= link_to 'Cancelar Venta', :controller => :sales, :action => 'cancel_sale', :deletor => @sale.id%><br>
<%end%><br> 
<%= button_to 'Volver', sales_path, :method => :get, :class => 'btn btn-default' %><br>

</CENTER>
</div>

我希望当用户点击带有特定参数的搜索按钮时可以刷新第二个表格。

谁能帮帮我??我已经尝试了将近 2 周,但我没有结果 =(

【问题讨论】:

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


    【解决方案1】:

    有很多方法可以做到这一点。最简单的方法是为您的表格创建 js-view searach_table.js.erb,这将返回您呈现的 html 表格

    你的 js 视图:

    $("#your_second_table_container").html(<%= escape_javascript(render(partial: 'search_results', locals: {subproducts: @subproducts}))%>)
    

    以及您的搜索链接:

    <%= link_to 'Search', <ypur_rout_to_js_view_action>, remote: true %>
    

    【讨论】:

    • 请问你有这个例子吗?如果我明天不能完成这个作业,我会遇到很多麻烦@Aray
    • 您对搜索有任何操作吗?正如我已经说过的,您应该使用 .js.erb 扩展名向此操作添加新视图。在那里,您应该使用 jQuery 从部分渲染您的表格。然后你应该通过 ajax.get 请求这个 js 视图,或者简单地将 Rails 链接与​​ remote: true 选项。您也可以将 form_for 与相同的遥控器一起使用:true 属性和 f.submit
    • 我会看看并评论我的结果。非常感谢你
    猜你喜欢
    • 2015-02-25
    • 1970-01-01
    • 1970-01-01
    • 2014-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-02
    • 2014-02-14
    相关资源
    最近更新 更多