order.dt event capture the column name
order.dt event capture the column name
I would like to understand the event handling in datatables. When I look at the request parameter list, I see that "order[0][column]" specifies the column on which user requested ordering and the "columns[n][data]" ( where n is the value of "order[0][column]" ) specifies the column name for server to do order by column name.
I would like to know if I can use the the event "order.dt" event and fire a new ajax and then redraw the table with a custom name=value? Backend developers will find that easy...if they get orderColumnName=Critical in the request parameter list and fetch data from database by applying the requested order by.
My code...
$('#example').dataTable({
"processing": true,
"serverSide": true,
"ajax": {
"url": "someURL",
"dataSrc": "data"
},
"aoColumnDefs" : [
{
"mData" : "Id",
"sType" : "integer",
"aTargets" : [ 0 ],
"bSortable": false,
},
{
"mData" : "Name",
"sType" : "String",
"aTargets" : [ 1 ],
"bSortable": true,
},
{
"mData" : "critical",
"sType" : "string",
"aTargets" : [ 2 ],
"bSortable": false,
}
}); // end of jquery data table init
$('#example').on( 'order.dt', function () {
// Can I read the column name here?
// then I would like to fire a ajax and do datatable reload
});
} );
Answers
You can use the API in the event handler to determine the order applied to the table:
order()
. Remember that DataTables supports multi-column filtering, so it might not just be one column which is being ordered upon.Allan
so far I got the column name for corresponding column id like this below..
You really want to avoid using the settings object as much as possible. The contents of the object are considered to be private and they can and do change between versions.
Also the
mData
property is not a name - it can be a function or various other types. What do you want it for?Allan