I'm still in deep trouble with that issue. I use datatable.net to display some data from my database (mySql).
I do exactely what the API notice me to do in order to sort columns by name ASC (toggle DESC). https://datatables.net/manual/server-side
order[i][column] integer Column to which ordering should be applied. This is an index reference to the columns array of information that is also submitted to the server.
order[i][dir] string Ordering direction for this column. It will be asc or desc to indicate ascending ordering or descending ordering, respectively.
So my code is like this:
AJAX call:
var dataTable = $('#tableau').DataTable({
"processing":true,
"serverSide":true,
"order":[],
"orderFixed": [],
"ordering":true,
"ajax":{
url:"/charbon/clae-listes/actions/appel.php",
type:"POST"
},
"columnDefs":[
{
"targets":[ 2, 3],
"orderable":false,
},
],
});
And my appel.php script
...
if(isset($_POST["order"]))
{
$query .= 'ORDER BY '.$_POST['order'][0]['column'].' '.$_POST['order'][0]['dir'].' ';
}
else
{
$query .= 'ORDER BY id DESC ';
}
...
That's where trouble begins:
when I click my 1st column I receive that error message on localhost :
DataTables warning: table id=tableau - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1
And when I click my 2nd column to sort it that sorts the previous column. It's like datatables picks directly the column number from my database, and offset of 1 because my database doesn't have a column 0
how to attribute the right column to the right sorting?
output JSON response
1st column
<b>Fatal error</b>: Uncaught PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'order clause' in /Users/.../appel.php:45
2nd column
{"draw":9,"recordsTotal":10,"recordsFiltered":11,"data":[["type","Client","client".........
My headers
draw:9
columns[0][data]:0
columns[0][name]:
columns[0][searchable]:true
columns[0][orderable]:true
columns[0][search][value]:
columns[0][search][regex]:false
columns[1][data]:1
columns[1][name]:
columns[1][searchable]:true
columns[1][orderable]:true
columns[1][search][value]:
columns[1][search][regex]:false
columns[2][data]:2
columns[2][name]:
columns[2][searchable]:true
columns[2][orderable]:true
columns[2][search][value]:
columns[2][search][regex]:false
columns[3][data]:3
columns[3][name]:
columns[3][searchable]:true
columns[3][orderable]:false
columns[3][search][value]:
columns[3][search][regex]:false
order[0][column]:1
order[0][dir]:asc
start:0
length:10
search[value]:
search[regex]:false