【发布时间】:2016-10-23 07:01:48
【问题描述】:
我尝试将 curl 数据获取到 jquery 数据表(例如 https://datatables.net/examples/api/row_details.html),
这里是 response.php
echo json_encode($results);
json_encode 输出:
{
"hittotal":69511,
"data":[
{
"....."
},.....
]
}
我的网页中的 HTML 表格和 javascript(datatables.php):
<script>
function format ( d ) {
// `d` is the original data object for the row
return '<table cellpadding="3" cellspacing="0" border="0" style="padding-left:50px;">'+
'<tr>'+
'<td>message:</td>'+
'<td>'+(d.message)+'</td>'+
'</tr>'+
'<tr>'+
'<td>id:</td>'+
'<td>'+d.id+'</td>'+
'</tr>'+
'</table>';
}
$(document).ready(function() {
var table = $('#example').DataTable( {
"ajax": {
'type': 'POST',
'url': 'response.php',
'data': {
from: '<? echo $from; ?>',
to: '<? echo $to; ?>'
},
"columns": [
{
"className": 'details-control',
"orderable": false,
"data": null,
"defaultContent": ''
},
{ "data": "datetime" },
{ "data": "message" },
],
"order": [[1, 'asc']]
} );
// Add event listener for opening and closing details
$('#example tbody').on('click', 'td.details-control', function () {
var tr = $(this).closest('tr');
var row = table.row( tr );
if ( row.child.isShown() ) {
// This row is already open - close it
row.child.hide();
tr.removeClass('shown');
}
else {
// Open this row
row.child( format(row.data()) ).show();
tr.addClass('shown');
}
} );
} );
</script>
html:
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th></th>
<th>datetime</th>
<th>message</th>
</tr>
</thead>
<tfoot>
<tr>
<th></th>
<th>datetime</th>
<th>message</th>
</tr>
</tfoot>
</table>
我确定 echo json_encode($results) 不为空,但为什么不能在数据表中显示?
【问题讨论】:
-
我看到您正在使用数据表。你真的在使用 jquery datatables 包吗?
-
是的,我正在使用 jquery 数据表包
-
一些笔记。我注意到您正在使用 $('#example').DataTable();,但是我看不到您将表命名为 id='example' 的位置,这意味着 datatable 包不知道将自身应用于何处。你能简单介绍一下它目前是如何工作的吗?如果您不包含 php 部分,您是否能够看到一个简单的数据表?谢谢!
-
其实我想用你说的datatables.net/examples/api/row_details.html,但是我的数据来自服务器端(php),所以我不知道如何修改它
-
你能发一个返回数据的例子吗?
标签: jquery html-table row