1
我放在一對錶的頁面中,既擔任過服務器端的管理DataTable對象....jQuery的數據表 - 警告「無法初始化數據表」僅在運行時
一個是可見的,用戶訪問, 第二個隱藏在一個不可見的框架中(它僅用於生成用於.xls導出的結構,因爲需要顯示較少的列,但需要導出所有列)。
,以保持兩個表一致, 到第二DataTable的.draw()
方法的調用已被插入第一.draw事件處理程序...
這是HTML表定義:
<table id="index_quotes" class="display" data-source="<%= quotes_url(format: 'json')%>">
<thead>
<tr>
<th><%= I18n.t('quote_ref') %></th>
<th>Status</th>
<th><%= I18n.t('quote_date') %></th>
<th><%= I18n.t('quote_customer') %></th>
<th><%= I18n.t('quote_pcs') %></th>
<th><%= I18n.t('quote_tot_amnt') %></th>
<th><%= I18n.t('quote_net_amnt') %></th>
<th><%= I18n.t('quote_discount') %></th>
</tr>
</thead>
<tbody>
...
</tbody>
</table>
<!-- hidden div used to store datatable used for export to .xls functionalities -->
<div style="display:none;">
<table id="index_quotes_export" class="display" data-source="<%= quotes_url(format: 'json')%>">
<thead>
<tr>
<th><%= I18n.t('quote_ref') %></th>
<th>Status</th>
<th><%= I18n.t('quote_date') %></th>
<th><%= I18n.t('quote_customer') %></th>
<th><%= I18n.t('quote_pcs') %></th>
<th><%= I18n.t('quote_tot_amnt') %></th>
<th><%= I18n.t('quote_net_amnt') %></th>
<th><%= I18n.t('quote_discount') %></th>
<th><%= I18n.t('quote_mat_group') %></th>
<th><%= I18n.t('quote_seller') %></th>
<th><%= I18n.t('quote_area_mgr') %></th>
<th><%= I18n.t('quote_notes') %></th>
<th><%= I18n.t('quote_feedback') %></th>
<th><%= I18n.t('quote_private_notes') %></th>
</tr>
</thead>
<tbody>
....
</tbody>
</table>
</div>
這是頁面的JavaScript部分:(!省略縮短代碼段列規格)
$(document).ready(function(){
//
// datatables initialization
jQuery(function() {
// main display table definition
$("#index_quotes").dataTable({
bJQueryUI: true,
bAutoWidth: false,
bLengthChange: false,
bProcessing: true,
bServerSide: true,
ajax: {
url: $('#index_quotes').data('source'),
dataType: 'json',
cache: false,
type: 'GET',
data: function(d) {
var dt_params;
$.extend(d, $('#index_quotes').data);
dt_params = $('#index_quotes').data('dt_params');
if (dt_params) {
$.extend(d, dt_params);
}
}
},
iDisplayLength: 15,
aaSorting: [[0, "asc"]],
aoColumns: [
....
]
}).on('draw.dt', function() {
var search_filter = $('.dataTables_filter input').val();
$('#index_quotes_export').DataTable().search(search_filter);
$('#index_quotes_export').DataTable().draw();
});
// export table definition
$("#index_quotes_export").dataTable({
bJQueryUI: false,
bLengthChange: false,
bProcessing: true,
bServerSide: true,
iDisplayLength: -1,
ajax: {
url: $('#index_quotes').data('source'),
dataType: 'json',
cache: false,
type: 'GET',
data: function(d) {
var dt_params;
$.extend(d, $('#index_quotes').data);
dt_params = $('#index_quotes').data('dt_params');
if (dt_params) {
$.extend(d, dt_params);
}
}
},
aoColumns: [
....
]
});
});
請注意:
- 這個代碼插入一個.html.erb文件中
- 沒有錯誤可言出現在發展中運行的代碼(所有工作正常,當我(Ruby on Rails的ENV!)在主數據表上運行,隱藏的將根據請求精確對齊!)
- 當我發佈生產環境代碼(ubuntu linux 16.04 lts + apache + phusion乘客)時,兩個數據表似乎都能正常工作,除了警告消息當您加載或刷新頁面時在客戶端瀏覽器中顯示,如果您在頁面本身內進行搜索,分頁或其他操作,似乎都可以正常工作!
等待你的建議...
問候,
弗朗西斯
我今天試試....然後更新你的結果! (只是爲了解釋,.draw()事件處理程序中的代碼用於將第二個表的搜索過濾器與第一個表的搜索過濾器進行對齊,然後強制第二個表重新繪製以使表內容完全相同,只有第二個沒有分頁,並將返回從查詢中提取的所有記錄,使用此解決方法,讓操作員在屏幕上查看搜索過濾和分頁結果,但導出所有生成的項目而不處理分頁!) –
修改後的代碼如您所建議的,警告消失.. ..非常感謝您的建議,我已將您的答案標記爲解決方案!問候。弗朗切斯科 –