【发布时间】:2019-04-02 18:52:48
【问题描述】:
我有一个使用 bootstrap 3 创建的选项卡部分。对于其中一个选项卡,选项卡内容是一个数据表。数据是在服务器端生成的,并且在初始加载时工作正常。但是,我试图在 onchange 函数中使用 ajax.reload() ,并且数据没有刷新。我也尝试使用 clear() 方法,但数据没有被清除。我已经验证该函数正在触发,并且返回的 AJAX 响应是有效的 JSON 并且不是空的。我的控制台中没有错误,只是没有任何反应。
我正在使用 Datatables 1.10,源直接来自 cdn (https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js)。
我已经在 StackOverflow 和 Datatbles 论坛上搜索过这个问题。其他人已经有了它,并且接受的解决方案似乎是$('#TableID').DataTable().ajax.reload(),我正在这样做。我还尝试将数据表存储到 var table 并尝试 table.ajax.reload(),并传入回调函数并将页面刷新显式设置为 true。我从回调函数返回日志,仍然没有错误,但没有任何变化。
HTML:
<div id="tab_datatable" class="tab-pane fade">
<table id="example" class="display" style="width:100%">
<thead>
<tr>
<th>Year</th>
<th>Course</th>
<th>Title</th>
<th>Enrollment</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
我的 js 代码创建了表格,效果很好:
$( document ).ready(function() {
var $table = $('#example').DataTable({
scrollCollapse: false,
paging: true,
pagingType: 'simple_numbers',
lengthMenu: [[10, 25, 50, 100, -1], [10, 25, 50, 100, 'All']],
processing: true,
serverSide: true,
ajax: {
url: myURL,
data: {
campus: $campus.val(),
college: $college.val(),
year: $term.val(),
draw: parseInt(1),
length: (10),
start: parseInt(1)
}
},
searching: false,
info: false,
});
});
表格在“示例” div 中正确呈现。
我有一个 updatePage() 函数,当页面上的三个菜单选项之一发生变化时会触发该函数。
$(document).on('change', '.dynamic', function(){
updatePage();
});
//Updates dynamic content on page
function updatePage()
{
//sessionStorage
sesssionStateStorage('campus', $campus.val());
sesssionStateStorage('college', $college.val());
sesssionStateStorage('term', $term.val());
console.log($term.val()); //confirming that value is updating
//Refresh datatable
console.log($table); //Shows table object so I know it exists
//$table.clear(); //This does't work; no errors but no change
//$table.ajax.reload() //doesn't work; no errors but no change
//$('#example').DataTable().ajax.reload(); //doesn't work; no errors but no change
$('#example').DataTable().ajax.reload(test(), true); //Confirming that I see test logging from test() function, but data still doesn't update
}
function test()
{
console.log('in test');
console.log('table object: ' + $table)
}
非常感谢任何帮助。
编辑:忘记包括我也在监控控制台中的网络调用,当其中一个菜单值更改时,我确实看到触发了新的 AJAX 调用并返回了正确的数据。所以问题似乎只是在 ajax.reload() 之后表格没有正确呈现。
【问题讨论】:
标签: jquery datatables