2016-12-31 111 views
2

我有一张名为clientes的表,这个表有大约15536​​条记录,这使得数据加载非常缓慢。我如何优化日志加载以改进过程?如何显示数千条记录

这是我的索引视图

<h1>Clientes</h1> 
<style> 
.container { 
} 

</style> 




    <table id="clientes" class="display"><!--el id clientes es de datatables referenciado en clientes.coffe y display class es una clase de datatables--> 
    <thead> 

    <tr><!--active es para sombrear la fila--> 
     <th>Clave</th> 
     <th>Nombre</th> 
     <th>Nombre Corto</th> 
     <th>Dirección</th> 
     <th>Colonia</th> 
     <th>Credito</th> 
     <th>DiasCredito</th> 
     <th>LimiteCredito</th> 
     <th>Saldo</th> 
     <th>Ruta</th> 
     <th>Promociones</th> 
     <th>Acción</th> 
     <th></th> 

    </tr> 
    </thead> 
    <tbody id="container_clientes"> 
     <%= render @clientes %><!--carga todos los clientes--> 
</tbody> 

我的部分cliente.html.erb

<tr id="cliente_<%= cliente.id %>"> 
    <td><%=cliente.IdCli%> 

</td> 
    <td><%=cliente.Nombre%></td> 
    <td><%=cliente.NombreCorto%></td> 
    <td><%=cliente.Direccion%></td> 
    <td><%=cliente.Colonia%></td> 
    <td> 
    <% if cliente.Credito == true%> 
     <input type="checkbox" disabled="true" checked="true"> 
    <%else%> 
     <input type="checkbox" disabled="true" > 
    <%end%> 
    </td> 
    <td><%=cliente.DiasCreedito%></td> 
    <td><%=cliente.LimiteCredito%></td> 
    <td> 
    <% if cliente.detallecob.last != nil%> 
     <%=cliente.detallecob.last.Saldo%> 
     <%else%> 
     <%=cliente.Saldo%> 
    <%end%> 
    </td> 
    <td> 
    <% if cliente.relclirutas != nil%> 
     <% cliente.relclirutas.each do |varias| %> 
     <%=varias.ruta.Ruta%> 
     <%end%> 
    <%end%> 
    </td> 
    <td> 
    <% if cliente.relclili != nil%> 
     <%=cliente.relclili.listapromomast.ListaMaster%> 
    <%end%> 
    </td> 


    <td> 
     <%= link_to '<i class="fa fa-gift" aria-hidden="true"></i> Activos'.html_safe, activos_cliente_path(cliente), class:"btn btn-primary btn-xs boton" %> 

     <button type="button" class="btn btn-warning btn-xs" data-toggle="modal" data-target="#myupdatecliente_<%= cliente.id %>"> 
      Editar 
     </button> 
     <!--Destroy--> 
     <% if cliente.Status == true%> 
      <%= link_to 'Eliminar', cliente, method: :delete, class: "btn btn-danger btn-xs", remote:true %> 
     <%else%> 
      <%= link_to 'Habilitar', cliente, method: :delete, class: "btn btn-success btn-xs", remote:true %> 
     <%end%> 
     </td> 


<td class="no" > 
    </td> 
</tr> 
+0

您是否为数据进行分页或者只是一次加载? – gaga5lala

+0

这是一个简单的决定。一方面,您可以将所有记录一次加载到浏览器中。加载需要多长时间取决于许多无法控制的因素,例如用户的计算机。这可能值得等待。另一方面,您可以一次快速加载一些记录,并让用户选择一些内容来查看更多内容。像所有的决定一样,每个选项至少具有优势。 –

+0

@ gaga5lala只是加载一次,在数据表中 – LuisC

回答

0

这是一个有点猜的,因为我不是一个Ruby程序员,但是,让我们来看看这样的:

<td> 
    <% if cliente.relclirutas != nil%> 
     <% cliente.relclirutas.each do |varias| %> 
     <%=varias.ruta.Ruta%> 
     <%end%> 
    <%end%> 
    </td> 

如果这意味着,

if cliente.relclirutas is not null or an empty string 
loop through it and display each value of varias.ruta.Ruta 

然后你不需要if语句。只需循环通过空值或空字符串。这可能为每个记录节省几纳秒,其中有15,000多个记录。

1

您可以使用kaminari gem进行数据分页,只需要部分数据并通过访问下一页需要更多数据。

Cliente.page(1).per(50) 

雷也做分页链接给你,只需要使用辅助

<%= paginate @cliente %> 

您可以找到使用中README

为了更好的用户体验,您应该通过API(AJAX)进行通信,并使用像无限滚动这样的技术。

1

你可以卸载你的抓取。这意味着,使用javascript和ajax稍微复杂一些的方法。在这个例子中,我不会使用分页宝石。

修改你的控制器:

def index 
    if !params[:page].blank? 
     respond_to do |f| 
      f.html {} 
      f.json { # only for pagination, work around this to make it more complex 
       offset = 50*params[:page].to_i 
       @clients = Client.offset(offset).limit(50).select("field1, field2") # appended to any other condition you may have 
       render :json => @clients.as_json 
      } 
     end 
    else 
     @clients = Client.limit(50) 
    end 
end 

我不会发布更多的,你自己看着办吧。在文档加载,添加一个Ajax获取事件,这将:

  • 呼叫/ clientes页= 2等(添加增加计数器)
  • 做,直到你得到下面的空响应:
    • 你所得到的JSON响应的每一行,添加一行到表,下面的部分代码

您可以用定时器或循环做到这一点。但是,这是加载第一组行(在本例中为50)后页面将变得有响应的一种方式,然后,在您工作时,将获取更多行。

+0

感谢您的回答。这个方法使用数据表? – LuisC

+0

什么是'datatable'?我在你的问题中看到的每一行都有一张表格和一张渲染图。根据你的问题,这应该起作用... –