小编par*_*ban的帖子

具有静态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)

请举例说明这样做的方法.提前致谢.

datatable jquery json

0
推荐指数
1
解决办法
7820
查看次数

标签 统计

datatable ×1

jquery ×1

json ×1