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

时间:2014-09-12 13:35:06

标签: php jquery ajax json datatable

如果我使用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数据的变量,但不是调用文件我想使用此变量中的数据

1 个答案:

答案 0 :(得分:0)

您可以使用此说明

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

或者这个:

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

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