jQuery DataTables : Ruby On Rails : Sorting of date/time column in any format : Type based sorting

//cdn.datatables.net/plug-ins/1.10.7/sorting/datetime-moment.js

1
2
3
4
5
6
7
8
9
10
11
<script type="text/javascript" src="jquery.dataTables.js"></script>
<script type="text/javascript" src="dataTables.numericComma.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $('#example').dataTable( {
            "columnDefs": [
                { "type": "numeric-comma", targets: 3 }
            ]
        } );
    } );
</script>
$.fn.dataTable.moment( '%B %e, %Y' ); // Format I used for date
$targetTable.dataTable({ /* Disable initial sort */
 "aaSorting": [],
 "order": [],
 "aoColumnDefs": [
   {
   'bSortable': false,
   'aTargets': ['action']
    }, 
   {
   "type": "datetime-moment", //use moment.js to calulate and sort date col
   targets: ['created-on', 'last-sent-on', 'date-applied']
   }
 ],
 'iDisplayLength': 10
});
<table id="bench-applicants-table">
 <thead>
 <tr>
 <th><%= t(:id) %></th>
 <th><%= t(:name) %></th>
 <th><%= t(:job_applied) %></th>
 <th><%= t(:status) %></th>
 <th><%= t(:number_of_email_sent) %></th>
 <th class="last-sent-on"><%= t(:last_sent_on) %></th>
 <th class="action"><%= t(:action) %></th>
 </tr>
 </thead>

similarly,
<th class="created-on"><%= t(:created_on) %></th>

Source: http://datatables.net/plug-ins/sorting/datetime-moment

http://datatables.net/plug-ins/sorting/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s