Jquery Datatables不显示数据

Posted

技术标签:

【中文标题】Jquery Datatables不显示数据【英文标题】:Jquery Datatables Not showing data 【发布时间】:2019-10-04 08:28:21 【问题描述】:

我的数据表有问题。我把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);

我运行后,它没有显示我的数据,我使用检查元素检查,它显示像这样的响应

"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】:

可能很明显,但是您是否将 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"/>

【讨论】:

我做到了。我放了js datatbale和jquery。

以上是关于Jquery Datatables不显示数据的主要内容,如果未能解决你的问题,请参考以下文章

jquery dataTables 不显示数据显示 ajax 错误

请教jquery一个页面渲染两个datatables总是出现其中一个table加载不成功导致

JQuery UI Sortable + DataTables显示所有行而不刷新当前页面

DataTables - 排序,搜索,分页不起作用

jquery datatables如何去掉搜索框和每页显示多少条数据

JQuery DataTables - 删除 fnFilter 并显示所有结果