<script type="text/javascript" language="javascript" src="./plugins/jquery/jquery-1.12.4.min.js"></script> <script type="text/javascript" language="javascript" src="./plugins/jquery.dataTables/dataTables/jquery.dataTables.min.js"></script> <link rel="stylesheet" type="text/css" href="./plugins/jquery.dataTables/dataTables/jquery.dataTables.min.css" /> <style> /* 表格表头和数据都水平居中,默认居左 */ .table > tbody > tr > td { text-align: center; } .table > thead:first-child > tr:first-child > th { text-align: center; } </style> <div id="container" style="width: 50%"> <table id="myTable" style="width: 100%; white-space: nowrap"> <thead> <tr> <th>name</th> <th>age</th> <th>score</th> </tr> </thead> <tbody></tbody> </table> </div> <script> $(function () { let i = 1; const table = $("#myTable").DataTable({ serverSide: false, //不检索 searching: false, //不排序 ordering: false, //不允許用戶更改表的分页显示长度 lengthChange: false, ajax: { url: "../test.php", type: "POST", // 传给服务器的数据,可以添加查询参数 data: function (param) { param.num = i++; return param; }, //处理数据 dataSrc: function (json) { for (var i = 0, length = json.data.length; i < length; i++) { json.data[i][1] = json.data[i][1] + "岁"; } return json.data; }, }, //自定义列 columnDefs: [ { targets: [0], render: function (data, type, full) { return "<span style='color:red;'>" + data + "</span>"; }, }, ], }); setInterval(() => { // 请求不同的url // table.ajax.url(url).load(); // 请求同一个url table.ajax.reload(function (json) {}, true); }, 3000); }); </script>
<?php $ex = [ ['ee', 24, 34], ['tt', 23, 45] ]; $data = []; if ($_POST['num']) { $num = $_POST['num']; for ($i = 0; $i <= $num; $i++) { foreach ($ex as $item) { $data[] = $item; } } } echo json_encode(['data' => $data, 'num' => $num]);
标签:jquery,function,示例,url,数据源,ajax,json,table,data From: https://www.cnblogs.com/caroline2016/p/16976522.html