adam gouldie
adam gouldie

Reputation: 261

Restore original sort order with jQuery DataTables

I retrieve data from database already sorted, at times I need to reset back to the initial load order. I have tried this fnSortNeutral which doesn't work with the new API DataTables 1.10.

I have then tried this

https://datatables.net/forums/discussion/26430/fnsortneutral-fails-with-new-api

Not sure what this does, as it resets something but definitely not back to original load order, instead a sort order of its own

$('#removesort').click( function() {
    table.order.neutral().draw();
});

I can't use this as the sort column does not exsist on Datatable to be able to sort using it, sort is applied on database query

table
    .order( [[ 1, 'asc' ], [ 2, 'asc' ]] )
    .draw();

How does one get data table to show the original load order retrieved from database on click?

As requested this is basically the basic normal code that works, I could find a long way round to add another column for sorting but feel I am overcomplicating adding another field purely for sorting if order can be easily reset in datatables. Is this not possible?

                      var table = $('#example').DataTable( {    
                            order: [],            
                            "columnDefs": [ { "targets": 0, "orderable": false } ],                                    
                            "sAjaxSource": "external/load",
                            "oLanguage": {
                               "sLoadingRecords": "",
                               "sEmptyTable": "No data found"
                            },                                                     
                            "fnServerData": function ( sSource, aoData, fnCallback, oSettings ) {
                                  if($('#order'))
                                  {
                                    var order = $('#order').val();
                                    aoData.push( { "name": "order", "value": order } );
                                  }
                                  setTimeout(function() {
                                              oSettings.jqXHR = $.ajax( {
                                                       "dataType": 'json',
                                                       "type": "POST",
                                                       "url": sSource,
                                                       "data": aoData,
                                              "timeout": 15000,                         
                                              "success": fnCallback,
                                              "complete" : function(){

                                              },
                                              "error": function (e) {
                                                  console.log(e.message);
                                              }                  
                                        });
                                  },1000);

                            }
                      });

Upvotes: 6

Views: 5758

Answers (1)

Juliver Galleto
Juliver Galleto

Reputation: 9037

Have you tried placing the 'order' after the 'draw'? like.

.draw();
.order( [[ 1, 'asc' ], [ 2, 'asc' ]] )

in my case, I just use

.draw().order( [[ 0, 'desc' ]] );

and it works

Upvotes: 3

Related Questions