如何解决数据表未显示数据
我已经包含了CDN,就像网站提供的那样,即使复制粘贴了每行代码,仍然无法显示或显示任何结果。
这是CDN
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.22/css/jquery.dataTables.css">
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.22/js/jquery.dataTables.js"></script>
这是我的代码
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.22/css/jquery.dataTables.css">
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.22/js/jquery.dataTables.js"></script>
<body>
<table id="example" class="display">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Salary</th>
</tr>
</thead>
</table>
</body>
这是脚本
[
{
"name": "Tiger Nixon","position": "System Architect","salary": "$3,120"
},{
"name": "Garrett Winters","position": "Director","salary": "$5,300"
}
]
$('#example').DataTable( {
data: data,columns: [
{ data: 'name' },{ data: 'position' },{ data: 'salary' }
]
} );
解决方法
您缺少两件事:
- jQuery库
-
data
变量
请检查以下解决方案。
var data = [{
"name": "Tiger Nixon","position": "System Architect","salary": "$3,120"
},{
"name": "Garrett Winters","position": "Director","salary": "$5,300"
}
]
$('#example').DataTable({
data: data,columns: [{
data: 'name'
},{
data: 'position'
},{
data: 'salary'
}
]
});
<html>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.22/css/jquery.dataTables.css">
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.22/js/jquery.dataTables.js"></script>
<body>
<table id="example" class="display">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Salary</th>
</tr>
</thead>
</table>
</body>
</html>
,
jQuery datatables.js需要使用jquery。 您应该在jquery.datatables.js之前添加它。
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.22/css/jquery.dataTables.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.22/js/jquery.dataTables.js"></script>
And you even don't need to define columns in your datatable,datatables does the work itself :
<body>
<table id="example" class="display" width="100%"></table>
</body>
<script>
var dataSet = [
[ "Tiger Nixon","System Architect","Edinburgh","5421","2011/04/25","$320,800" ],[ "Garrett Winters","Accountant","Tokyo","8422","2011/07/25","$170,750" ],];
$(document).ready(function() {
$('#example').DataTable( {
data: dataSet,columns: [
{ title: "Name" },{ title: "Position" },{ title: "Office" },{ title: "Extn." },{ title: "Start date" },{ title: "Salary" }
]
} );
} );
</script>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。