如何用json字符串填充数据表


How to populate data tables with the json string?

如果我使用sAjaxSource = "filepath/file" ,我成功地填充了数据表

然而,如果我传递PHP json数据并尝试收集它,它不会填充数据表中的数据,

如何在不调用objects.txt文件中的数据的情况下直接使用json数据?

json_data  = { "aaData": [
    {
        "Rendering_engine": "Trident",
        "Browser": "Internet Explorer 4.0",
        "Platforms": "Win 95+",
        "Engine_Version": "4",
        "CSS_Grade": " b"
    },
    {
        "Rendering_engine": "rident",
        "Browser": "Internet Explorer 5.0",
        "Platforms": "Win 95+",
        "Engine_Version": "5",
        "CSS_Grade": " a"
    }
] }

php代码

$r = //sql query    
$ma['aaData'] =$r;
                        $data = json_encode($ma);
                        var_dump($data);

脚本

    <script>
    $(document).ready(function() {
        var json_data_chk =  <?php echo($data); ?>;
        var json_data = JSON.stringify(json_data_chk);
        alert(json_data);
// code     
var sImageUrl = "img/";

        var oTable = $('#hidden-table-info').dataTable( {
            "bProcessing": true,
           "sAjaxSource": "objects.txt",
            "bServerSide": false,
             "aoColumns": [
                {
                   "mDataProp": null,
                   "sClass": "control center",
                   "sDefaultContent": '<img src="'+sImageUrl+'details_open.png'+'">'
                },
                { "mDataProp": "Rendering_engine" },
                { "mDataProp": "Browser" },
                { "mDataProp": "Platforms" },
            ],
// code
    </script>

编辑:json_data是一个变量,它具有与objects.txt相同的json数据,但我不想调用文件,而是想使用这个变量的数据

您可以使用此指令

$('#example').dataTable( {
  "ajax": "json_data"
} );

或者这个:

 "ajax": {
    "url": "data.json",
    "type": "POST"
  }

参考:https://datatables.net/reference/option/ajax