简体   繁体   中英

Datatable server-side script

I've found this piece of code which looks to work for some people,

$dataArr['aaData'] = Array();
while($row = $res->fetch_assoc()){
$r = Array();
foreach($row as $key=>$value){
    $r[] = "$key $value";
}
$dataArr['aaData'][] = $r;} 
header('Content-Type: application/json');
echo json_encode($dataArr);
/* The output will be of the form, 
{"aaData": [ [
[
   "colname data"
   ...
],
 ] */

but I get an error " Table id =datos Invalid json response.

My Datatable columns do not have the same name as my db columns, and I'm not sure how to handle that. Here is my js :

   $(document).ready(function() {
function getCpAndVille(data, type, dataToSet) {
return data.cp + " " + data.ville;
}
$('#datos').dataTable({
 "order": [[ 3, "desc" ]],
 "bProcessing": true,
"sAjaxSource": 'ajx/datatable_process_search.php',
"aoColumnDefs": [
                  { "sName": "Réf.", "aTargets": [ 0 ] },
                  { "sName": "Poste", "aTargets": [ 1 ] },
                  { "sName": "Type de contrat", "aTargets": [ 2 ] },
                  { "sName": "Date de publication", "aTargets": [ 3 ]},
                  { "sName": "Lieu", "aTargets": [ 4 ], "mData": getCpAndVille  },
                  { "sName": "Descriptif", "aTargets": [ 5 ] }
                ],

Can someone tell me the right php code I should use, please, because it's quite hard to find it, and I don't understand the example given in http://www.datatables.net/examples/data_sources/server_side.html

Thanks in advance.

Console Network --> {"aaData":[["job_id 9261","job_intitule Assistant Achats","job_contrat Int\érim","job_date_insertion 2015-02-20","cp 06110","job_ville 19"], and so on...

For those who are looking for a serverside script including JOIN table

https://www.daniweb.com/web-development/php/threads/467455/convert-datatables-to-server-side-processing

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM