未捕获的TypeError:无法读取未定义的ONn数据表的属性“样式”

时间:2016-09-21 03:39:45

标签: json ajax datatables

我有这样的代码

var monthNames = ["January", "February", "March", "April", "May", "June",
                                      "July", "August", "September", "October", "November", "December"
                                    ];
                while (dateStart <= dateEnd) {
                    dateLoop.push({name : monthNames[dateStart.getMonth()] + ' ' +  dateStart.getFullYear() });
                    var newDate = dateStart.setDate(dateStart.getMonth() + 1);
                    dateStart.setMonth(dateStart.getMonth() + 1);
                }
var jsonArray = {"Columns":dateLoop};
                var columnArray = jsonArray['Columns']; 
                var titleArray = [];
                var title = {};
                title['title'] = "Buyer";
                titleArray.push(title);

                var i = 0;
                for ( 
                var j = 0; j < columnArray.length; j++) {
                    var temp2 = {};
                    temp2['title'] = columnArray[i]["name"];
                    titleArray.push(temp2);
                    i = i + 1;
                };

我用这个代码在Datatables上添加列

$('#del-date').DataTable({ 
                    "filter": false,
                    "destroy": true,
                    "processing": true,
                    "serverSide": true,
                    "columns": titleArray,
                    "ajax":{
                        url : host+'qty/'+oderby+'?buyer='+buyer+'&dari='+begin+'&sampai='+end,
                        error: function(){  // error handling
                            console.log(error);
                        }
                    }
                }).draw();

首先我提交我的过滤器是按钮,工作正常并显示我想要的数据,但当我更改过滤器显示错误未捕获TypeError:无法读取属性'样式'未定义

如何解决?对不起我的坏langaunge。我非常依赖这段代码

更新:此屏幕拍摄我收到错误

Uncaught TypeError: Cannot read property 'style' of undefined

更新已解决!!!!我换成这样的代码:

$.ajax({
                    "url": host+'qty/'+oderby+'?buyer='+buyer+'&dari='+begin+'&sampai='+end,
                    "success": function(json) {
                        var tableHeaders;
                        $.each(json.columns, function(i, val){
                            tableHeaders += "<th>" + val + "</th>";
                        });

                        $("#tableDiv").empty();
                        $("#tableDiv").append('<table id="displayTable" class="dataTable table table-bordered table-hover table-full-width" cellspacing="0" width="100%"><thead><tr>' + tableHeaders + '</tr></thead></table>');
                        //$("#tableDiv").find("table thead tr").append(tableHeaders);  

                        $('#displayTable').dataTable(json);
                    },
                    "dataType": "json"
                });

我使用controller和mysql来创建json。问题解决。

1 个答案:

答案 0 :(得分:0)

当传递给列(&#34; columns&#34;:titleArray)的titleArray数小于表(tableHeaders)所具有的列数时,就会发生这种情况。