使用ajax datatable显示数据

时间:2014-08-21 11:21:00

标签: javascript php jquery ajax datatable

我一直坚持这个问题一整天,基本上我有一个表,我需要通过数据表和ajax发送数据multidimensional array $list,以便我可以对它进行json编码并将其发回显示:

$('#table_test').dataTable( {
    "bProcessing": false,
    "bServerSide": true,
    "bJQueryUI": true,
    "sAjaxSource": "ajax.php",
    "sAjaxDataProp": "data",
    "fnServerData" : function(sSource, aoData, fnCallback) {
        jQuery.ajax({
            "dataType" : "json",
            "type" : "POST",
            "url" : sSource,
            "data" : aoData,
            "success" : function(data) {
               fnCallback(data);
            }
        });
    },
    "aoColumns": [
        { "mData": "thumbnail" },
        { "mData": "title" },
        { "mData": "category" },
        { "mData": "price" },
        { "mData": "date_post" },
        { "mData": "actions" }]
});

ajax.php(与测试一样)

<?php

    $array = array(
        "aaData" => array(
            array(
                'thumbnail' => "1",
                'title' => "1",
                'category' => "1",
                'price' => "1",
                'date_post' => "1",
                'actions' => "ONE"
            ),
            array(
                'thumbnail' => "1",
                'title' => "2",
                'category' => "2",
                'price' => "2",
                'date_post' => "2",
                'actions' => "TWO"
            ),
            array(
                'thumbnail' => "3",
                'title' => "3",
                'category' => "3",
                'price' => "3",
                'date_post' => "3",
                'actions' => "THREE"
            ),
        )
    );

    echo json_encode($array);
?>

这是我想用以下内容填充数据的表格:

<table cellpadding='0' cellspacing='0' border='0' class='datatable table table-striped table-bordered' id='table_test' >

</table> 

对此有何帮助?非常感谢。

1 个答案:

答案 0 :(得分:0)

只是一个想法,您是否尝试将mData字符串更改为aoColumns中的aaData?

{
    fieldName: "something"
    /*** OR ****/
    fieldName: Object of case class Sample(..........)
}

});