我正在Rails应用程序中使用数据表,并且由于表中大约有16,000条记录,因此加载时间开始变慢。我已经将该表配置为处理服务器端的数据并使用will_paginate gem,但是加载时间通常仍为5秒左右。我的以下代码中有什么特别令人讨厌的地方会导致这些较长的加载时间吗?
注意-我在下面的代码段中注释了几行,以查看加载时间是否有所不同-并没有。
谢谢! -迈克
class AccountsDatatable
delegate :params, :h, :link_to, :number_to_currency, to: :@view
attr_reader :user
include Rails.application.routes.url_helpers
include Pundit
def pundit_user
User.find(@current_user_id)
end
def initialize(view, account_status, current_user_id)
@view = view
@account_status = account_status
@current_user_id = current_user_id
@current_user = User.find(current_user_id)
end
def as_json(options = {})
{
sEcho: params[:sEcho].to_i,
iTotalRecords: Account.count,
iTotalDisplayRecords: accounts.total_entries,
aaData: data
}
end
private
def data
accounts.map do |account|
[
account.account_name,
account.account_status.to_s.humanize,
#account.get_departments_with_active_opportunities,
#account.get_departments_with_active_lobs,
link_to("Open", account_path(account), class: "btn btn-xs btn-success"),
((policy(account).update?) ? link_to("Edit", edit_modal_form_accounts_path(id: account.id), class: "btn btn-xs btn-default", remote: true) : " "),
((policy(account).destroy?) ? link_to("Delete", delete_modal_form_accounts_path(id: account.id), class: "btn btn-xs btn-danger", remote: true) : " ")
]
end
end
def accounts
@accounts ||= fetch_accounts
end
def fetch_accounts
if @account_status.nil? || @account_status == "all"
accounts = Account.order("#{sort_column} #{sort_direction}").all
else
accounts = Account.order("#{sort_column} #{sort_direction}").where(account_status: @account_status)
end
if params[:sSearch].present?
search = params[:sSearch]
begin
accounts = accounts.or( { account_name: (/.*#{search}.*/i) } )
rescue
accounts = accounts.or( { account_name: (/^#{::Regexp.escape(search)}$/i) } )
end
end
accounts = accounts.paginate(:page => page, :per_page => per_page)
accounts
end
def page
params[:iDisplayStart].to_i/per_page + 1
end
def per_page
params[:iDisplayLength].to_i > 0 ? params[:iDisplayLength].to_i : 10
end
def sort_column
columns = %w[account_name account_status]
columns[params[:iSortCol_0].to_i]
end
def sort_direction
params[:sSortDir_0] == "desc" ? "desc" : "asc"
end
end
页面脚本:
$(document).ready(function () {
const datatable_options = {
sPaginationType: 'full_numbers',
bProcessing: true,
bServerSide: true,
sAjaxSource: $('#accounts_table').data('source'),
columns: [
null,
null,
// { orderable: false },
// { orderable: false },
{ orderable: false },
{ orderable: false },
{ orderable: false }
]
};
initialize_datatable('#accounts_table', datatable_options);
});
查看
<table id = "accounts_table" class="table table-striped accounts_table" data-source="<%= accounts_path(format: "json", account_status: @account_status ) %>" >
<thead>
<tr>
<th>Account Name</th>
<th>Classification</th>
<!--<th>Active prospecting departments</th>-->
<!--<th>Active departments</th>-->
<th> </th>
<th> </th>
<th> </th>
</tr>
</thead>
<tbody>
</tbody>
</table>
答案 0 :(得分:0)
看看:DataTables is running slow. How can I speed it up?
问。 DataTables运行缓慢。如何加快速度?
答:有几种方法可以加快DataTables的速度。通常是第一个 要做的是启用分页 (paging
),如果已经 禁用,因为这只会减少要显示的数据量。 除此之外,您用于加载数据的方法将 通常会对性能产生最大的影响。
- 客户端处理-DOM来源的数据:约5000行。速度选项:
orderClasses
。- 客户端处理-Ajax源数据(
ajax
):〜50'000行。 速度选项:deferRender
- 服务器端处理(
serverSide
): 数百万行。
以上数字仅供参考。实际数量及其对性能的影响取决于各种因素,例如浏览器版本,插件等。