jQuery数据表不显示数据

时间:2019-05-17 03:13:01

标签: jquery arrays json datatables

我的数据表有问题。我把像这样的jQuery数据表

var oTable = table.DataTable({ 
            scrollY:        300,
            scrollX:        true,
            scrollCollapse: true,
            paging:         false,
            fixedColumns:   {
                leftColumns: 1
            },
            "columns": [
                { "data": "Status" },
                { "data": "Jan" },
                { "data": "Feb" },
                { "data": "Mar" },
                { "data": "Apr" },
                { "data": "May" },
                { "data": "Jun" },
                { "data": "Jul" },
                { "data": "Aug" },
                { "data": "Sep" },
                { "data": "Oct" },
                { "data": "Nov" },
                { "data": "Des" }
            ],
            "ajax": host+'datatables/'+target+'?buyer='+buyer+'&tahun='+tahun,
            "columnDefs": [{
                "targets": [ 0 ],
                "className": "details-control",
            }]
        });
$('#btnfiltersales').click(function(){
    var buyer = $("#buyer").val();
            var tahun = $("#tahun").val();
oTable.ajax.url( host+'datatables/'+target+'?buyer='+buyer+'&tahun='+tahun).load();
});

在ajax php上,我使用此类代码

$buyer = $_GET['buyer'];
            $tahun = $_GET['tahun'];
            if($buyer<>"" && $tahun<>"")
            {
                $record = $this->tbl_project->infoprojection($buyer,$tahun)['count'];
                $json["recordsTotal"] = intval($this->tbl_project->infoprojection($buyer,$tahun)['count']);
                $json["recordsFiltered"] = intval($this->tbl_project->infoprojection($buyer,$tahun)['count']);
                if($record<>0)
                {   
                    $x = $this->tbl_project->infoprojection($buyer,$tahun)['items'];
                    while ($val = $x->fetch_assoc()) {
                        /*$json['data']=[$val['Status'],$val['Jan'],$val['Feb'],$val['Mar'],$val['Apr'],$val['May'],$val['Jun'],$val['Jul'],
                                       $val['Aug'],$val['Sep'],$val['Oct'],$val['Nov'],$val['Des']];*/

                        $json['data']=["Status"=>$val['Status'],"Jan"=>$val['Jan'],"Feb"=>$val['Feb'],"Mar"=>$val['Mar'],"Apr"=>$val['Apr'],
                                       "May"=>$val['May'],"Jun"=>$val['Jun'],"Jul"=>$val['Jul'],"Aug"=>$val['Aug'],"Sep"=>$val['Sep'],
                                       "Oct"=>$val['Oct'],"Nov"=>$val['Nov'],"Des"=>$val['Des']];
                    }
                }
                else
                {
                    $json["recordsTotal"] = "0";
                    $json["recordsFiltered"] = "0";
                    $json['data']=[];
                }
            }
            else
            {
                $json["recordsTotal"] = "0";
                $json["recordsFiltered"] = "0";
                $json['data']=[];
            }

            echo json_encode($json);

在我运行之后,它没有显示我的数据,我使用inspect元素进行了检查,它显示了这样的响应

{"recordsTotal":2,"recordsFiltered":2,"data":{"Status":"Projection Amt","Jan":"782657.2","Feb":"649597.17","Mar":"673178.6","Apr":"871113.81","May":"805787.78","Jun":"369851.56","Jul":"882085","Aug":"721188.81","Sep":"872933.17","Oct":"691758.35","Nov":"691758.35","Des":"691758.35"}}

我真的很困惑我的回答显示我的数据,但是没有显示在Datatable上。它仅显示No data available in table。我输入错误的代码还是什么?请帮助我,我被困在这里。

1 个答案:

答案 0 :(得分:0)

可能很明显,但是您是否将JS和CSS包含在dataTables中?

赞:

<script type="text/javascript" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>

<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/dataTables.bootstrap.min.css"/>