How to get Initialized Table Sort order.
How to get Initialized Table Sort order.
Utlizing the API function myTable.init() I am able to retrieve most of what the original configuration settings of a table are.
but I've noticed .init() returns what ever the current sort order on the table is not the original sort order that the table was created with.
Where is the original story order stored.
Or do i need to grab it during table init and save it as it's own property.
Example
My Orginal dataTable Config object
{
columns:moveReqCol,
data: dataTableJSON,
scrollX: true,
stateSave: true,
deferRender: true,
colReorder: {realtime:false, fixedColumnsLeft:2},
fixedColumns: {
leftColumns: 2
},
dom: 'Blfrtip',
order: [[ 1, "desc" ]],
}
notice i have 1 order set.
now, after setting an order on the table the .init() function returns.
{columns: Array(17), data: Array(87), scrollX: "100%", stateSave: true, deferRender: true, …}
aaData: (87) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
aaSorting: (4) [Array(2), Array(3), Array(3), Array(3)]
aoColumns: (17) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
bDeferRender: true
bDestroy: true
bStateSave: true
buttons: ["test"]
colReorder: {realtime: false, fixedColumnsLeft: 2}
columns: (17) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
data: (87) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
deferRender: true
destroy: true
dom: "Blfrtip"
drawCallback: ƒ ()
fixedColumns: {leftColumns: 2}
order: (4) [Array(2), Array(3), Array(3), Array(3)]
sDom: "Blfrtip"
sScrollX: "100%"
scrollX: "100%"
stateSave: true}
we can see that the order has updated to what ever i've currently set it to.
I've Attached a jsFiddle below that console logs what im talking about.
This question has an accepted answers - jump to answer
Answers
My Solution for this issue was to create a deep copy of the dt.init() object during init event so i have a snapshot of what the table was orginally configured as before the user modifies the table. so I can "destroy" and reload the table as it's orginal configuration at a later date.
$(document).on('init.dt',funciton(){
aVariable = JSON.parse(JSON.stringify(dt.init()));
});
You can also use the
settings
parameter passed in theinitComplete
callback. Something like this:Example: http://live.datatables.net/jawucowa/1/edit
Kevin
Marked yours as an answer but my only solution to this so far is making a deep copy if anyone knows of a better way please let me know.
Hi @kinetik ,
If you destroy and reinitialise the table, it should return to that original state anyway. Also,
rows()
will return the data from that original order.Could you give some background on the reason for doing this, please. It might lead to an alternative approach that would work instead,
Cheers,
Colin
@colin I'm utilizing the data tables state api. to allow users to load custom "states" on the fly as sort of a memorized View from the server. I've made a plugin that allows the user to select states from a list of saved states. My problem is when I load a new "state" the table "loses" that initialized state when they select a new view because i've destroyed and recreated the table with a new "base configuration". So I needed a way to remember what the table was first loaded as before a user selected a new state to load.