我正在使用jquery数据表,而我仍然坚持将新数据加载到datatable并重绘。我有以下代码片段。
首次使用DataTable初始化
var myDataTable=$('#patient_data').DataTable({
searching: false,
"ajax": {
"url": "process/get-patient-search.php",
"type": "POST",
data: {
"clinic": <?php echo $clinicID ?>
},
'dataSrc':function (data){
return data;
}
},
"columns": [
{ "data": "detail_id" },
{ "data": "fname" },
{ "data": "surname" },
{ "data": "dentist" ,
"className":"hidden-xs"
},
{ "data": "clinic_name" ,
"className":"hidden-xs"
},
{ "data": "speciality" ,
"className":"hidden-xs"
},
{ "data": "date_registered" ,
"className":"hidden-xs",
},
{ "data": "detail_id" }
],
"aoColumnDefs": [{
"aTargets": [7],
'searchable': false,
'orderable': false,
'className':'center',
'mRender': function (data, type, full, meta){
return '<button id="' + data+ '" onClick="getMoreInfo('+data+');" class="moreInfo"><i class="fa fa-info-circle"></i></button>';
}
}]
});<br>
来自服务器的初始响应
[{"detail_id":"1",
"fname":"Hakim",
"surname":"Ali",
"dentist":"Ariya Stark",
"clinic_name":"Doctor ",
"speciality":"O",
"date_registered":"13\/02\/16"
},
{"detail_id":"2",
"fname":"Maria",
"surname":"Arshad",
"dentist":"Ariya Stark",
"clinic_name":"Doctor ",
"speciality":"O",
"date_registered":"13\/02\/16"
}]
它适用于第一个。但我需要根据用户查询搜索获取新数据。当我重绘表时它说
DataTables警告:table id = patient_data - 第0行请求的未知参数'detail_id'。有关此错误的详细信息,请参阅http://datatables.net/tn/4
我的重绘代码段
$("#search_submit").on('click',function(){
if (search_patient.valid()) {
$('.errorHandler').fadeOut();
$.post('process/process-patient-search.php',$('#patient-search-form').serialize(), function(response){
if(isJson(response)){//isJson(response)
alert("data is json");
alert(response);
/* myDataTable.clear();
myDataTable.rows.add(response);
myDataTable.draw(); */
myDataTable.clear().draw();
myDataTable.rows.add(response); // Add new data
myDataTable.columns.adjust().draw();
}else{
alert(response);
/*
$('.errorHandler').html('');
$('.errorHandler').html(response);
$('.errorHandler').fadeIn("slow");
$('.errorHandler').fadeOut(2000); */
}
});
} else {
//alert('form is not valid');
}
});
function isJson(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}
服务器响应
[{"detail_id":"66",
"fname":"Waseem",
"surname":"Akram",
"dentist":"Ariya Stark",
"status":"1",
"clinic_name":"Doctor ",
"speciality":"S",
"date_registered":"15\/09\/16"
},
{"detail_id":"67",
"fname":"Waseem",
"surname":"Akram",
"dentist":"Ariya Stark",
"status":"1",
"clinic_name":"Doctor ",
"speciality":"S",
"date_registered":"15\/09\/16"
},
{"detail_id":"69",
"fname":"Waseem",
"surname":"Akram",
"dentist":"Ariya Stark",
"status":"1",
"clinic_name":"Doctor ",
"speciality":"S",
"date_registered":"15\/09\/16"
}]
我知道这个问题已经被问过了,我一整天都在搜索,但没有一个答案解决了我的问题。我尝试了每个论坛,包括堆栈流和数据库论坛。
我经历了所有问题并搜索了每一个可能的答案并尝试了所有的事情,但没有解决我的问题。我真的需要帮助。我不知道我的代码有什么问题。
答案 0 :(得分:0)
我想我自己找到了答案。
经修订的DataTable重绘代码段
define('FCPATH', 'C:/xampp/htdocs/your_project/');