【问题标题】:Cannot show jquery datables with php json_encode?无法使用 php json_encode 显示 jquery 数据表?
【发布时间】: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


【解决方案1】:

您可能想看看数据表中的子功能:https://datatables.net/examples/api/row_details.html

您可以使用表格行代替您在示例中看到的额外信息。

【讨论】:

  • 我已经更新了我的代码,并尝试从 php 获取数据,但它无法工作,你能帮我解决问题吗?
  • 我今天会看看,希望能让你走上正轨。
  • 凯尔牧师,谢谢你的帮助,我会等你的,非常感谢
猜你喜欢
  • 2013-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多