I want to display data from database in my view using ajax. But i receve this message: DataTables warning: table id=users - Requested unknown parameter '0' for row 0, column 0. For more information about this error, please see http://datatables.net/tn/4
this is my controller :
public function listUser(){
if ($this->session->userdata("success"))
{
// les Variables de datatable
//
$draw = intval($this->input->post("draw"));
//
$start = intval($this->input->post("start"));
$length = intval($this->input->post("length"));
$user = $this->UserModel->getUsers();
$data = array();
foreach( $user as $r) {
$tab[] = array();
$tab[] = $r->username;
$tab[] =$r->usersurname;
$tab[] =$r->useremail;
$tab[] =$r->date_create;
$tab[] =$r->Num_tel;
$tab[] =$r->userright;
$tab[] = '<button type="button" name="update" id="'.$r->user_id.'" class="btn btn-warning btn-xs update">Modifier</button>';
$tab[] = '<button type="button" name="delete" id="'.$r->user_id.'" class="btn btn-danger btn-xs delete">Supprimer</button>';
$data = $tab;
}
$output = array(
"draw" => $draw,
"recordsTotal" => count($user),
"recordsFiltered" => count($user),
"data" => $data
);
echo json_encode($output);
exit();
}
// $this->load->view('users/listUser',['user'=>$user]);
// }
else
{
return redirect('Login');
}
}
Your problem might be in the loop section, every time the loop passes in this line $tab[] = array();
your not reseting the variable but actually appending a new array to the $tab
, you should use $tab = array();
to reset the variable, and in the end $data[] = $tab;
to append the $tab
data to the $data
variable.
$data = array();
foreach( $user as $r) {
$tab = array();
$tab[] = $r->username;
$tab[] = $r->usersurname;
$tab[] = $r->useremail;
$tab[] = $r->date_create;
$tab[] = $r->Num_tel;
$tab[] = $r->userright;
$tab[] = '<button type="button" name="update" id="'.$r->user_id.'" class="btn btn-warning btn-xs update">Modifier</button>';
$tab[] = '<button type="button" name="delete" id="'.$r->user_id.'" class="btn btn-danger btn-xs delete">Supprimer</button>';
$data[] = $tab;
}
ok thank you, but i receve another warning: DataTables warning: table id=users - Requested unknown parameter '6' for row 0, column 6. For more information about this error, please see http://datatables.net/tn/4
this is my views :
<table class="display responsive no-wrap table table-striped table-bordered table-hover table-checkable order-column dataTable no-footer" id="users" width="100%">
<thead>
<tr>
<th>Nom</th>
<th>Prenom</th>
<th>E-mail</th>
<th>Date de creation</th>
<th>Téléphone</th>
<th>Droit</th>
<th>Modifier</th>
<th>Supprimer</th>
</tr>
</thead>
</table>
<script type="text/javascript">
$('document').ready(function() {
let mouvementTable = $("#users").DataTable({
buttons: [{
extend: "print",
className: "btn dark btn-outline"
}, {
extend: "pdf",
className: "btn green btn-outline"
}, {
extend: "excel",
className: "btn yellow btn-outline "
}, {
extend: "csv",
className: "btn purple btn-outline "
}],
"ajax": {
url : "http://localhost/logistock/index.php/ControllerUser/listUser",
"type": "POST"
},
order: [[ 1, 'asc' ]],
"select": {
"style": 'multi',
"selector": 'td:first-child'
},
"dom": "<'row' <'col-md-12'B>><'row'<'col-md-6 col-sm-12'l><'col-md-6 col-sm-12'f>r><'table-scrollable't><'row'<'col-md-5 col-sm-12'i><'col-md-7 col-sm-12'p>>",
"lengthMenu": [[5, 10, 15, 20, -1], [5, 10, 15, 20, "All"]],
"language": {
"url": "//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/French.json"
}
});
});
</script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.