我使用的是dataTable的最新版本
我通过像这样的ajax请求删除了一行:
$.ajax
(
{
method : 'POST',
//la route (controleur) et le paramètre (id à supprimer)
url: url + "/professionnels/deleteProfessionnels/"+id_to_delete,
dataType: 'json',
success:function()
{
//console.log(retour);
$('#example').DataTable().ajax.reload();
PHP模型代码:
public function deleteProfessionnel($pro_id)
{
$sql = "DELETE FROM professionnels WHERE id = :pro_id";
$query = $this->db->prepare($sql);
$parameters = array(':pro_id' => $pro_id);
// useful for debugging: you can see the SQL behind above construction by using:
//echo '[ PDO DEBUG ]: ' . Helper::debugPDO($sql, $parameters); exit();
$query->execute($parameters);
}
PHP控制器代码:
我到达了ajax的成功功能,但我收到了这条消息:
DataTables警告:table id = example - 无效的JSON响应。有关此错误的详细信息,请参阅http://datatables.net/tn/1
public function deleteProfessionnels($professionnel_id)
{
// if we have an id of a song that should be deleted
if (isset($professionnel_id))
{
// Instance new Model (Song)
$professionnel = new Professionnel();
// do deleteSong() in model/model.php
$professionnel->deleteProfessionnel($professionnel_id);
//echo "id=> ".$professionnel_id;
}
// where to go after song has been deleted
$professionnels = $professionnel->getAllProfessionnels();
//print_r(json_encode($professionnels));
echo json_encode($professionnels, JSON_FORCE_OBJECT);
当我用enter link description here测试json结果时,我有错误。
1-为什么php会返回一个无效的json对象? 2-我有没有做这一切来重新加载表?
这是js(json对象)中的响应:
{"0":{"id":"123","nom":"G\u00e9om\u00e8tre","prenom":"Barack","adresse":null,"numero":null,"boite":null
,"cp":null,"ville":null,"mail":null,"telephone":null,"inami":null,"tva":null,"disponibilite":null,"commentaire"
:null},"1":{"id":"128","nom":"G\u00e9om\u00e8tre-expert","prenom":"Barack","adresse":null,"numero":"6"
,"boite":"3","cp":"1300","ville":"Bruxelles","mail":"contact@dubinfo.be","telephone":"0471301253","inami"
:"5533434343","tva":"BE 0832.581.586","disponibilite":null,"commentaire":null}}
答案 0 :(得分:1)
您可以尝试以下代码:
public function getCoursesForDatatable(){
$table = 'courses';
$primaryKey = 'course_id';
$columns = array(
array( 'db' => 'course_name', 'dt' => 0 ),
array( 'db' => 'course_short_description', 'dt' => 1 ),
array( 'db' => 'course_is_active', 'dt' => 2 ),
array( 'db' => 'course_added_at', 'dt' => 3 ),
array( 'db' => 'course_public_id', 'dt' => 4 )
);
// $join_where = "JOIN investors ON course_applications.course_user_id = investors.investor_id";
$arr = $this->datatable_model->get_rows( $_GET, $table, $primaryKey, $columns );
$ret = $this->formatArticleData($arr['data']['info']);
$a = array(
"draw" => $arr['draw'],
"recordsTotal" => $arr['recordsFiltered'],
"recordsFiltered" => $arr['recordsFiltered'],
"data" => $ret
);
echo json_encode($a);
}
public function formatArticleData($data){
if(!empty($data)){
$res = "";
foreach ($data as $key => $value){
$res[] = array(
$value['course_name'],
$value['course_short_description'],
( $value['course_is_active'] == 1 ? "Active" : "Inactive" ),
date("d-M-Y", strtotime($value['course_added_at'])),
'<a class="ui button mini app-button " href="' . base_url('courses/edit') . '/' . $value['course_public_id'] .'">Edit</a>'
);
}
return $res;
}else{
return "";
}
}