Rails:用搜索框中的结果数据填充表

Rails: Fill Table with resulting data from a search box

本文关键字:结果 数据 填充 搜索 Rails      更新时间:2023-09-26

正如问题所说,我正试图用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>

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

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

有很多方法可以做到这一点。最简单的方法是为您的表创建js视图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 %>