Simple method for ordering by date
Simple method for ordering by date
Hi all,
Just wanted to say thanks for a great project. We just started to use it - but already finding it really useful.
Thought I would share this with the community as it makes things very simple.
We wanted a simple way to take our array and a config file and build a table full of data, then DataTables-ify it.
We got all this working well, but couldnt get date ordering working easily without having to specify the column layout in JS. We got around this by adding the MySQL format date in front of any other formatted dates - within a hidden DIV - to any columns that will contain formatted date-data.
//$object->get_value() contains the mysql date
//we have these set up to apply prefix and suffix to hidden tags
$hide_open = "<div class='remove-before-export'>#!!";
$hide_close = "!!#</div>";
if ($object->get_datatype() == 'date') {
//replace the value we will add to the cell with a modified version
$object->set_value($hide_open . $object->get_value() . $hide_close . print_nice_date($object->get_value()));
}
The table still 'sees' this value and it means when ordering it does so correctly (alphabetically).
The only issue was exporting the data - since it included the hidden text between the divs.
We got around this by doing the following:
var current = $('#' + table_id).position();
var table = $('#' + table_id).dataTable({
"scrollY": get_window_height() - current.top - 5,
"paging": false
});
var export_defaults = [
{
"sExtends":"xls",
"fnClick":function(nButton, oConfig, flash) {
var table_data = this.fnGetTableData(oConfig);
var new_table_data = table_data.replace(/#!!.+?!!#/g, "");
this.fnSetText(flash, new_table_data);
}
}
];
// we only want the copy and export xls button
$.fn.dataTable.TableTools.defaults.sSwfPath = "/swf/copy_csv_xls_pdf.swf";
$.fn.dataTable.TableTools.defaults.aButtons = export_defaults;
var tt = new $.fn.dataTable.TableTools( table );
$( tt.fnContainer() ).insertAfter('div.dataTables_filter');
Replies
More info and background here: http://www.samueledney.com/2014/09/05/php-and-datatables/