sorting related data from the server
sorting related data from the server
Shovo
Posts: 5Questions: 1Answers: 0
in DataTables
Hi guys. I use Server-side in the dataTable and everything works fine, except for sorting those columns that were connected to the main table, in the setting the query goes wrong, but the result is not valid. I will be grateful for help.
$ajax = Tracker::with('user')->with('settings')->where('trackers.status', '1');
try { return datatables($ajax )->make(true);
} catch (\Exception $e) {
$e->getMessage();
}
<script>
var table = $("#tracker").DataTable({
"processing": true,
"serverSide": true,
"ajax": "/trackers",
"columnDefs": [{ "orderable": false, "targets": [ 9 ] }],
"columns": [
{data: 'id'},
{data: 'imei'},
{data: 'name',
defaultContent: ''
},
{
data: 'user.email',
defaultContent: 'Не превязон к ползовотеля'
},
{
data: 'settings.tied',
render: function( data, type, full, meta ) {
return data === '1' ? 'Да' : (data === '0' ? 'Нет' : 'не известно');
},
defaultContent: ''
},
{data: 'settings.type',
defaultContent: 'Нет'
},
{
data: 'settings.sum_html',
defaultContent: '',
render: function( data ) {
if(data !== null && data !== undefined){
res = data.split('~');
let html = '';
res.forEach((e,i) => {
html += `<p>${e}</p>`;
});
return html;
}else{
return null
}
},
},
{data: 'settings.auto_renewal',
defaultContent: 'Нет'
},
{data: 'comment',
defaultContent: ''
},
{
data: null,
render: function( data ) {
return "<a href=\"/trackers/"+data.id+"\" class=\"btn btn-info\"> Подробно</a>\n" +
" <form action=\"/trackers/"+data.id+"\" method=\"post\" class=\"d-inline\">\n" +
' @csrf\n' +
' @method('DELETE')\n' +
" <button type=\"submit\" class=\"btn btn-danger delete-btn\">Удалить</button>\n" +
" </form>"
},
},
],
});
</script>
Answers
Do you mean that the columns where you use
columns.render
are not sorting correctly? Or is it something else and I've not understood?The
columns.render
is a client-side action, so yes, the server (which does the sorting whenserverSide
is enabled) wouldn't know anything about the rendering on the client-side. You would need to update your server-side script to do that rendering an sort based on the rendered data.Allan
look in the image, 1,2,3,4,9 work correctly and the rest do not, although I see a valid sql query in the debugger. for example!
Do you mean if you click to sort on the
Email
column it doesn't orderumiller
,wanda
,xsipes
? If so, that's an error in the server-side script.Allan
columns 0,1,2,8 belong to the Tracker model and they are sorted pretty well, but other connections via dependency (with ('user') -> with ('settings')) mail works fine here, but nothing else
We'll need to see the full query to understand why the ordering isn't happening.
Colin
TrackerController
TrackerFilter
QueryFilter
in phpmyadmin works correctly
In your query it is ordering by the
tied
column. However, I don't see anything in your code which is specifying that. So I don't know if that is a static part, or if you are changing the column based on the sorting information submitted by DataTables.Allan
what can I do to you