【发布时间】:2015-06-25 20:21:01
【问题描述】:
我在 Rails 应用程序中工作,并在 jQuery DataTables 上使用 deferLoading: true 选项,以便将第一个 DataTables 的加载传递给 Rails 控制器。
我让数据表按我想要的方式加载,在控制器中加载初始表可以消除初始 html 加载时的 Ajax 延迟,但是,数据表信息部分不会显示分页结果。
代码和图片如下所示。
除了表格底部的分页之外,一切都正常工作,我只是无法让它应用与 Ajax 调用数据表相同的细节。对此问题的任何想法或方向将不胜感激!
index.html.erb:
<div class="row">
<div class="col-xs-12 table-wrapper">
<div class="inner-wrapper">
<p class="quick-app">
<a class="custom-btn accent-inverse-btn add-user" href="<%= calculator_path%>">Quick Application</a>
</p>
<table class="table table-striped table-scroll cms-table-width dataTable" id="customer_deals_datatable" data-source="<%= dealer_customer_deals_url(:include_archived => params[:include_archived].present?) %>" >
<div>
<thead>
<tr>
<th>ID/Calculator</th>
<th>Applicant/Co-Applicant</th>
<th>Year</th>
<th>Model</th>
<th>App Status</th>
<th>Tier Number</th>
<th>Docs Status</th>
<th>Submitted On</th>
<th>Days Remaining</th>
<th>Chrome Decision</th>
<th>Updated At</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<% @datatable.data.each do |datum| %>
<tr>
<% datum[0] = datum[0].join('') %>
<%= (datum.map {|content| "<td>#{content}</td>"}.join('')).html_safe %>
</tr>
<% end %>
</tbody>
</div>
</table>
</div>
</div> <!-- </div>#content -->
</div>
controller
def index
respond_to do |format|
format.html do
params.merge!({"iDisplayLength"=>"10","iSortCol_0"=>"10","sSortDir_0"=>"desc"})
@datatable = CustomerDeals::CustomerDealsDataTable.new(view_context, @dealer)
end
format.json { render json: CustomerDeals::CustomerDealsDataTable.new(view_context, @dealer) }
end
end
这是项目中数据表类的部分代码:
module CustomerDeals
class CustomerDealsDataTable
def fetch_deal_searches
return @deal_searches if @deal_searches.present?
deal_searches = CustomerDeals::CustomerDealSearch.where(dealership_id: @dealer )
if is_submitted_on_sort?
deal_searches = deal_searches.where('deal_dated_calculator_value != ?', 'calculator')
end
if params[:sSearch].present?
deal_searches = deal_searches.containing(params[:sSearch])
end
deal_searches = deal_searches.order(order_query)
@deal_searches = deal_searches
end
def is_submitted_on_sort?
SORT_COLUMNS[params[:iSortCol_0].to_i] == 'deal_submitted_on'
end
def lookup_sort_column
SORT_COLUMNS[params[:iSortCol_0].to_i]
end
def order_query
"#{lookup_sort_column} #{params[:sSortDir_0]}"
end
def paged_deal_searches
fetch_deal_searches.page(current_page_number).per(params[:iDisplayLength])
end
def current_page_number
params[:iDisplayLength].to_i == 0 ? 1 : params[:iDisplayStart].to_i/params[:iDisplayLength].to_i + 1
end
end
end
【问题讨论】:
标签: ruby-on-rails ajax datatables