2016-12-31 56 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'?我在你的問題中看到的每一行都有一張表格和一張渲染圖。根據你的問題,這應該起作用... –