我正在尝试使用 jQuery Datatable 插件在 HTML 页面中进行分页。
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script type="text/javascript" src=" https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src=" https://cdn.datatables.net/buttons/1.2.4/js/dataTables.buttons.min.js"></script>
<meta charset="UTF-8">
<title>Clients</title>
</head>
<body>
<table style="width:100%" id="clients_data">
<caption>Clients</caption>
<tr>
<th>Clients</th>
<th>Number of Sites</th>
<th>Reset the Processing</th>
</tr>
</table>
<table style="width:100%" id="machines_data">
<caption>Machines</caption>
<tr>
<th>Number</th>
<th>Machine Name</th>
</tr>
</table>
$(document).ready(function() {
loadCustomers();
loadMachines();
$('#clients_data').DataTable( {
"pagingType": "full_numbers"
} );
$('#machines_data').DataTable( {
"pagingType": "full_numbers"
} );
} );
function loadCustomers() {
$.ajax({
type: 'GET',
url: 'http://localhost:8080/cache/getCustomers',
dataType: 'json',
success: function(data) {
var rows = [];
$.each(data,function(id,value) {
rows.push('<tr><td><a href="clientSiteInfo.html?client='+id+'">'+id+'</td><td>'+value+'</td><td><button type="button" onclick="reset(\''+id+'\')">Reset</td></tr>');
});
$('#clients_data').append(rows.join(''));
}
});
};
function loadMachines() {
$.ajax({
type: 'GET',
url: 'http://localhost:8080/cache/getMachines',
dataType: 'json',
success: function(data) {
var rows = [];
$.each(data,function(id,value) {
rows.push('<tr><td>'+id+'</td><td><a href="machineInfo.html?machine='+value+'">'+value+'</td></tr>');
});
$('#machines_data').append(rows.join(''));
}
});
};
</script>
</body>
</html>
对于上述内容,当我尝试加载页面时,我得到:
Uncaught TypeError: Cannot read property 'mData' of undefined at $('#clients_data').DataTable({...})
我的脚本有什么问题? 我正在关注this guide .
我的 fiddle : https://jsfiddle.net/yeu7f3f2/
最佳答案
将两个 $.datatable() 函数调用移动到每个调用的成功函数内。
请记住,ajax 是异步的,这意味着即使它尚未返回,下一行也会在调用它后立即执行。我们希望确保仅当 ajax 完成插入表数据时才调用 datatable() 函数。
关于javascript - dataTable 给出未捕获的类型错误 : Cannot read property 'mData' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46763366/