1

i have loading server-side datatable with ajax response data and there i have one column which shows:-time for member in format like(ex. 5 days, 11 hours, 12 minutes). so i want to sort that column based on numbers of days, hours & minutes. i can't show you whole code because it's confidential but i show you ajax call and table data like below:-

enter image description here

this is the column which show time and i want to do ascending & descending sorting based on day,hour & minute.

<script>


$('table').DataTable({

processing: true,
serverSide: true,
responsive: true,
searching: true,
    "ajax": {
        url : "<?php echo site_url("getStatus") ?>",
        "data":function(d){
        }
 },
        "columnDefs": [
        {
        "render": function ( data, type, full, meta) {
          //what should i have to do here for custom sorting. i have tried different ways but it won't work so i have removed that code.
        },
        }]         
});

Divyarajsinh
  • 156
  • 8
  • 1
    What do you mean by custom sorting? Datatable doesn't know about your sorting as that should happen in server side. All you have to do is enable sorting for the desired columns, datatable will send the sort request to server when the user clicks it. Get the column index in the back and sort and return the result. – Cerlin Jul 24 '19 at 06:22
  • it allows sorting by this :- [link](https://stackoverflow.com/questions/17806737/custom-sorting-of-jquery-datatable-columns) but it won't working in my there i have to make change but it not allowing. – Divyarajsinh Jul 24 '19 at 06:39
  • 1
    Yes but that one is a local sort. it will sort only the data which is available locally. With server side processing, you will need to sort the entire dataset – Cerlin Jul 24 '19 at 06:41
  • thank you for your response,but can you provide any external link or any example regarding this so i can implement or get better understanding to use it in my datatable. – Divyarajsinh Jul 24 '19 at 07:24
  • Just check the parameters of get request sent from your datatable in network tab. You will get an idea of data which is been sent to server from datatable. I have done this long back in a project and i dont have any link or example – Cerlin Jul 24 '19 at 07:31
  • look on github for `ignited datatables` – Alex Jul 24 '19 at 08:59
  • Topic has nothing to do with DataTables since with `serverSide: true` requested sorting has to be done server-side entirely, so I'd advise to remove tags 'jquery', 'datatables' and replace them with 'php', 'mysql' (or whatever backend storage you might use). – Yevhen Horbunkov Jul 24 '19 at 09:00

1 Answers1

0

@Divyarajsinh,

I am using laravel, I had same issue earlier. I hope it will help for you.

Edit your blade file script by adding aoColumns and order

"processing": true,
"serverSide": true,
"searching": false,        
'iDisplayLength': 10,
"bFilter" : false,               
"bLengthChange": false,
"aoColumns": [
        { "bSortable": true }
    ],
"order": [
      [0, "desc" ]
    ],
 "ajax": {
    "url": "{!! route("arrayList") !!}",
    "type": "POST",
    "jsonpCallback": 'jsonCallback',
    "dataType": "jsonp"
    }

route file

Route::post('arrayList', 'TestController@arrayList')->name('arrayList');

edit your array like this:

$arr=[
  "4 days, 20 hours, 29 minutes",
  "0 days, 00 hours, 14 minutes",
  "4 days, 21 hours, 12 minutes",
  "0 days, 00 hours, 41 minutes",
  "4 days, 21 hours, 23 minutes"
];

Then get order request into your controller and use sort array function.

$order=$request->order;
if($order[0]['dir']=='desc'){
      arsort($arr);
} elseif($order[0]['dir']=='asc'){
    asort($arr);
}
suba
  • 1,320
  • 15
  • 22