When i have sorted my table, i'm getting some records double in the second page and some other records are not shown.
it's the first time i'm using datatables, it's a table to get records back of all shops created on the website. While i'm going to the next page, sometimes one of my records is gone and instead there is a double record from the first page.
public function winkeltables()
{
$table = 'alle_winkels';
$primaryKey = 'w_id';
$id = $_GET['k_id'];
$columns = [
['db' => 'w_logo', 'dt' => 0, 'formatter' => function ($d, $row) {
if ($d == "") {
return "";
}
return "<img class='thumb' src='/" . $d . "'/>";
}],
['db' => 'k_beoordeling', 'dt' => 1, 'formatter'=> function($d, $row){
return beoordeling($d);
}],
['db' => 'w_naam', 'dt' => 2],
['db' => 'w_land', 'dt' => 3],
['db' => 'w_straat', 'dt' => 4],
['db' => 'w_postcode', 'dt' => 5],
['db' => 'w_woonplaats', 'dt' => 6],
['db' => 'w_id', 'dt' => 7, 'formatter' => function ($d, $row) {
return "<a data-id='" . $d . "' href='/winkeldetail?w_id=" . $d . "'>Bekijk</a>";
}],
];
$sql_details = [
'user' => 'x',
'pass' => 'x',
'db' => 'x',
'host' => 'x'
];
if($id != "")
{
$where = ["k_id = " . $id];
echo json_encode(
\SSP::complex($_GET, $sql_details, $table, $primaryKey, $columns, $where)
);
}
else{
echo json_encode(
\SSP::simple($_GET, $sql_details, $table, $primaryKey, $columns)
);
}
die;
}
The script in my view is:
<script>
$(document).ready(function() {
var dt = $('#example').DataTable({
"processing": true,
"serverSide": true,
"ajax": "/datatables/winkel",
"language": {
"url": "http://cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/Dutch.json"
}
});
$('#example tbody').on( 'click', 'tr', function () {
var url = $(this).find('td').last().find('a').attr('href');
if(url) {
window.location = url;
}
} );
});
</script>
I am using the standard files from www.datatables.net
I already solved my own problem. I solved my problem by adding another order in my ssp.php file.
The new code is:
if ( $requestColumn['orderable'] == 'true' ) {
$dir = $request['order'][$i]['dir'] === 'asc' ?
'ASC' :
'DESC';
$orderBy[] = '`'.$column['db'].'` '.$dir . ', '. '`'.'w_naam'.'`'. ' ASC';
}
The old code was:
if ( $requestColumn['orderable'] == 'true' ) {
$dir = $request['order'][$i]['dir'] === 'asc' ?
'ASC' :
'DESC';
$orderBy[] = '`'.$column['db'].'` '.$dir;
}
I dont know if this code is efficient, but atleast it works now!