我在一个页面上有两个DataTables对象,我们称它们为 searchResultsTable 和 currentPortfolioTable。当我单击按钮时,我正在使用fnReloadAjax插件按需重新加载 DataTables。但是,只有一个表加载了新数据 (currentPortfolioTable),searchResultsTable 确实执行了对数据的 ajax 请求,但无法将新的(有效的)数据加载到表中。
我尝试破坏 DataTable 并创建一个新的,甚至重写 fnReloadAjax 插件的部分内容,看看是否可以产生不同的结果。
即使只为 currentPortfolioTable 调用 fnReloadAjax,它也拒绝显示加载的新数据。
使用 DataTable 的 aoColumns 属性的 bVisible 状态显示/隐藏列时,也会出现 searchResultsTable 无法加载新数据的问题。
示例代码:
var dataTableObjects = dataTableObjects || {
"searchResultsTable": {},
"currentPortfolioTable": {}
};
var _rankingsRootUrl = window.ROOT + 'rankings/';
var _defaultDataTableSettings = {
"aoColumns": [
{ "bSortable": false,
"sTitle": "Add to Portfolio",
"bVisible": true
},
{
"bSortable": true,
"sTitle": "Name of Investment",
"bVisible": true
},
{ "bSortable": true,
"sTitle": "Chart",
"bVisible": true
},
{ "bSortable": true,
"sTitle": "Rating",
"bVisible": true
},
{ "bSortable": true,
"sTitle": "Minimum",
"bVisible": true
},
{ "bSortable": true,
"sTitle": "ROR",
"bVisible": false
},
{ "bSortable": true,
"sTitle": "Max DD",
"bVisible": false
},
{ "bSortable": true,
"sTitle": "Sharpe",
"bVisible": false
},
{ "bSortable": true,
"sTitle": "Sterling",
"bVisible": false
}
],
"aaSorting": [
],
"sAjaxSource": _rankingsRootUrl + 'search_results/',
"bServerSide": true,
"bProcessing": true,
"bPaginate": false,
"bLengthChange": false,
"sScrollY": 200,
"sScrollX": "100%",
"sScrollXInner": "100%",
"bScrollCollapse": true,
"fnServerData": function ( sSource, aoData, fnCallback ) {
$.ajax( {
"dataType": 'json',
"type": "POST",
"url": sSource,
"data": aoData,
"success": fnCallback
} );
},
"fnServerParams": function ( aoData ) {
aoData.push(
{"name": "program_type", "value": $(':input#RankingProgramType').val()},
{"name": "program_name", "value": $(':input#RankingProgramName').val()},
{"name": "min_investment", "value": $(':input#RankingMinimumInvestment').val()},
{"name": "rate_of_return", "value": $(':input#RankingRateOfReturn').val()},
{"name": "max_dd", "value": $(':input#RankingMaxDd').val()},
{"name": "time_span", "value": $(':input#RankingTimeSpan').val()},
{"name": "flags", "value": $(':input#RankingFlags').val()},
{"name": "strategies", "value": $(':input#RankingStrategies').val()},
{"name": "recommended", "value": $(':input#RankingRecommended').val()},
{"name": "portfolio_id", "value": (window.PORTFOLIO && window.PORTFOLIO.id) || ""}
);
}
};
var _defaultCurrentPortfolioTableSettings = {
"aoColumns": [
{
"bSortable": true,
"sTitle": "Name of Investment",
"bVisible": true
},
{ "bSortable": true,
"sTitle": "Chart",
"bVisible": true
},
{ "bSortable": true,
"sTitle": "Rating",
"bVisible": true
},
{ "bSortable": true,
"sTitle": "Minimum",
"bVisible": true
},
{ "bSortable": true,
"sTitle": "ROR",
"bVisible": false
},
{ "bSortable": true,
"sTitle": "Max DD",
"bVisible": false
},
{ "bSortable": true,
"sTitle": "Sharpe",
"bVisible": false
},
{ "bSortable": true,
"sTitle": "Sterling",
"bVisible": false
}
],
"aaSorting": [
],
"sAjaxSource": _rankingsRootUrl + 'current_portfolio/',
"bServerSide": true,
"bProcessing": true,
"bPaginate": false,
"bLengthChange": false,
"sScrollY": 200,
"sScrollX": "100%",
"sScrollXInner": "100%",
"bScrollCollapse": true,
"fnServerData": function ( sSource, aoData, fnCallback ) {
$.ajax( {
"dataType": 'json',
"type": "POST",
"url": sSource,
"data": aoData,
"success": fnCallback
} );
},
"fnServerParams": function ( aoData ) {
aoData.push(
{"name": "portfolio_id", "value": (window.PORTFOLIO && window.PORTFOLIO.id) || ""}
);
}
};
dataTableObjects.searchResultsTable = $('#search-results table').dataTable(_defaultDataTableSettings);
dataTableObjects.currentPortfolioTable = $('#currently-in-portfolio table').dataTable(_defaultCurrentPortfolioTableSettings);
$("#rankings").on("click", "a.add", function (e){
dataTableObjects.searchResultsTable.fnReloadAjax('/datable1-url');
dataTableObjects.currentPortfolioTable.fnReloadAjax('/datable2-url');
e.preventDefault();
});
尝试修复:
- 使用 fnReloadAjax 的回调方法来触发 dataTable2 的数据重新加载
- 验证每个表的 id 是否唯一
- 验证 dataTableJSObjects 是页面的正确且唯一的表
- 控制台没有记录错误或问题
- JShinted js 确保没有js错误或错误
- 关闭 fnReloadAjax dataTableObjects.searchResultsTable 和 dataTableObjects.currentPortfolioTable 仍然无法正确重新加载
- 使用 fnReloadAjax 替换为 fnDraw