在我的DataTables中,我通过ajax获取列信息和行数据。他们都按预期解析。
然而,在DataTable之上,我得到了一个额外的“未定义”文本。
HTML来源:
<div id="dapel"></div>
DOM来源:
<div id="dapel">undefined<div class="dataTables_wrapper no-footer" id="displ...
SCRIPT:
$(document).ready(function($){
$.ajax({
"url": "'.$refresh.'",
"dataType": "json",
"success": function(json) {
var tableHeaders;
$.each(json.columns, function(i, val){
tableHeaders += "<th "+ val.width +">" + val.key + "</th>";
});
$("#dapel").empty();
$("#dapel").append(\'<table id="displayTable" class="display" cellspacing="0" width="100%" style="font-size: 0.7em;table-layout: fixed;word-wrap:break-word;"><thead><tr>\' + tableHeaders + \'</tr></thead></table>\');
$("#displayTable").dataTable({
"processing": true,
"serverSide": true,
"paging": false,
"ajax": "'.$refresh.'",
});
},
});
});
data.php
$json['columns'][] = array('key' => 'SERVER' , 'width' => 50);
$json['columns'][] = array('key' => 'Position' , 'width' => 50);
$json['columns'][] = array('key' => 'Office' , 'width' => 50);
$json['columns'][] = array('key' => 'Extn' , 'width' => 50);
$json['columns'][] = array('key' => 'Start date' , 'width' => 50);
$json['columns'][] = array('key' => 'Salary' , 'width' => 50);
$json['data'][] = array('Tiger Nixxon','System Architect','Edinburgh','5421','2011/04/25','$320,800',);
$json['data'][] = array('Garrett Winters','Accountant','Tokyo','8422','2011/07/25','$170,750',);
$json['data'][] = array('Ashton Cox','Junior Technical Author','San Francisco','1562','2009/01/12','$86,000',);
$json['data'][] = array('Cedric Kelly','Senior Javascript Developer','Edinburgh','6224','2012/03/29','$433,060',);
$json['data'][] = array('Airi Satou','Accountant','Tokyo','5407','2008/11/28','$162,700',);
$json['recordsTotal'] = 3000;
$json['recordsFiltered'] = 40;
echo json_encode($json);