I wanted to point out that when using data from the server via Ajax, the solution is super simple, but may not be immediately obvious.
When returning the sort order array, Datatables will send (in the $_POST
) a 2 element array that would be equivalent to:
$_POST['order'][0] =array('column'=>'SortColumnName', 'dir'=>'asc');
// 2nd element is either 'asc' or 'desc'
Therefore, you may display the date in any format you want; just have your server return the sorting criteria based only upon the sortColumnName
.
For example, in PHP (with MySQL), I use the following:
if (isset($_POST['order'])) {
switch ($_POST['order'][0]['column']) {
case 0:// sort by Primary Key
$order = 'pkItemid';
break;
case 1:// Sort by reference number
$order = 'refNo';
break;
case 2://Date Started
$order = 'dOpen';
break;
default :
$order = 'pkItemid';
}
$orderdir = ($_POST['order'][0]['dir'] === 'desc') ? 'desc' : 'asc';
}
Note, that since nothing from the $_POST
is passed to $order
or $orderdir
, no cross-script attack is possible.
Now, just append to a MySQL query:
$sql ="SELECT pkItemid, refNo, DATE_FORMAT(dOpen,'%b %e, %Y') AS dateStarted
FROM tblReference
ORDER BY $order $orderdir;";
run the query, and return just the dateStarted
value to Datatables in json.