par*_*ban 0 datatable jquery json
我正在尝试使用json格式的静态数据创建一个数据表,而不通过ajax请求命中服务器来获取json源数据.试图找到一种方法,但没有运气.我可以用静态json数据变量来做,如下所示,
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery DataTable</title>
<link rel="stylesheet" href="jquery-ui.css">
<link rel="stylesheet" type="text/css" href="demo_table_jui.css" />
<link rel="stylesheet" type="text/css" href="jquery-ui-1.7.2.custom.css" />
<script src="jquery-1.10.2.js"></script>
<script src="jquery-ui.js"></script>
<script type="text/javascript" src="complete.min.js"></script>
<script type="text/javascript" src="jquery.dataTables.min.js"></script>
<script>
$(function() {
var jsonData = [{"userID":"1","userName":"name1"},{"userID":"2","userName":"name2"},{"userID":"3","userName":"name3"}];
$('#example').dataTable({
data: jsonData,
columns: [
{ data: 'userID' },
{ data: 'userName' }
]
});
});
</script>
</head>
<body>
<table cellpadding="0" cellspacing="0" border="0" class="display" id="example" align="center">
<thead>
<tr>
<th>UserID</th>
<th>userName</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</body>
</html>Run Code Online (Sandbox Code Playgroud)
请举例说明这样做的方法.提前致谢.
通过包含您的JS代码$(function(){}),它将在您<table>创建之前运行.所以你必须:1.把那些代码放在你的最后<body>; 投入$(document).ready();
$(document).ready(function(){
var jsonData = [{"userID":"1","userName":"name1"},{"userID":"2","userName":"name2"},{"userID":"3","userName":"name3"}];
$('#example').dataTable({
data: jsonData,
columns: [
{ data: 'userID' },
{ data: 'userName' }
]
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7820 次 |
| 最近记录: |