[Koha-patches] [PATCH] Bug 9061 - DataTables date sort does not work with date in uk format (dd/mm/yyyy)

Owen Leonard oleonard at myacpl.org
Fri Nov 16 18:12:22 CET 2012


The DataTables plugin doesn't include native support for sorting date
in uk/euro format (dd/mm/yyyy). This patch adds a plugin to be
optionally included in cases where the Koha dateformat preference
is set to "metric."

The patron circulation history page is the only page affected by this
patch. If it is found to be working well I will submit follow-ups
to cover other instances where sorting by date occurs.

To test, load the patron circulation history page (members/readingrec.pl)
for a patron with a circulation history. Sorting by "date," "checked out
on," "date due," and "Return date" should work correctly with under
all three dateformat settings.
---
 .../prog/en/js/datatables.date-euro.js             |   44 ++++++++++++++++++++
 .../prog/en/modules/members/readingrec.tt          |    8 +++-
 2 files changed, 50 insertions(+), 2 deletions(-)
 create mode 100644 koha-tmpl/intranet-tmpl/prog/en/js/datatables.date-euro.js

diff --git a/koha-tmpl/intranet-tmpl/prog/en/js/datatables.date-euro.js b/koha-tmpl/intranet-tmpl/prog/en/js/datatables.date-euro.js
new file mode 100644
index 0000000..e744d0b
--- /dev/null
+++ b/koha-tmpl/intranet-tmpl/prog/en/js/datatables.date-euro.js
@@ -0,0 +1,44 @@
+/* Adapted from  sorting plugin found at http://datatables.net/plug-ins/sorting,
+  "Date (dd/mm/YYY hh:ii:ss)," by Ronan Guilloux. Original plugin did not
+  account for variation in input string */
+
+jQuery.extend( jQuery.fn.dataTableExt.oSort, {
+    "date-euro-pre": function ( a ) {
+        // function should work with strings like "dd/mm/yyyy hh:mm:ss" or "dd/mm/yyyy"
+        // function doesn't validate the date, just checks for valid structure
+        var x;
+        var dateTimeParts;
+        var dateParts;
+        var timeParts;
+        if ($.trim(a) !== '') {
+            dateTimeParts = $.trim(a).split(' ');
+            dateParts = dateTimeParts[0].split('/');
+            if( dateParts.length == 3 ){
+                // validish date format
+                if( dateTimeParts.length > 1){
+                    // there is a time portion
+                    timeParts = dateTimeParts[1].split(':');
+                    if( timeParts.length > 1 ){
+                        // validish time portion
+                        x = (dateParts[2] + dateParts[1] + dateParts[0] + timeParts[0] + timeParts[1] + timeParts[2]) * 1;
+                    }
+                } else {
+                    x = (dateParts[2] + dateParts[1] + dateParts[0]) * 1;
+                }
+            } else {
+                x = 10000000000000; // = l'an 1000 ...
+            }
+        } else {
+                x = 10000000000000; // = l'an 1000 ...
+        }
+        return x;
+    },
+ 
+    "date-euro-asc": function ( a, b ) {
+        return a - b;
+    },
+ 
+    "date-euro-desc": function ( a, b ) {
+        return b - a;
+    }
+} );
\ No newline at end of file
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/members/readingrec.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/members/readingrec.tt
index b7c5f53..1b86667 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/members/readingrec.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/members/readingrec.tt
@@ -1,15 +1,19 @@
 [% USE KohaDates %]
 [% INCLUDE 'doc-head-open.inc' %]
-<title>Circulation History for [% INCLUDE 'patron-title.inc' %]</title>
+<title>Circulation history for [% INCLUDE 'patron-title.inc' %]</title>
 [% INCLUDE 'doc-head-close.inc' %]
 <link rel="stylesheet" type="text/css" href="/intranet-tmpl/prog/en/css/datatables.css" />
 <script type="text/javascript" src="/intranet-tmpl/prog/en/lib/jquery/plugins/jquery.dataTables.min.js"></script>
 [% INCLUDE 'datatables-strings.inc' %]
 <script type="text/javascript" src="/intranet-tmpl/prog/en/js/datatables.js"></script>
+[% IF ( dateformat_metric ) %]<script type="text/javascript" src="/intranet-tmpl/prog/en/js/datatables.date-euro.js"></script>[% END %]
 <script type="text/javascript" id="js">$(document).ready(function() {
  $(document).ready(function() {
     $("#table_readingrec").dataTable($.extend(true, {}, dataTablesDefaults, {
-        "sPaginationType": "four_button",
+        "sPaginationType": "four_button"
+        [% IF ( dateformat_metric ) %], "aoColumnDefs": [
+        { "sType": "date-euro", "aTargets": [ 0,6,8,9 ] }
+        ][% END %]
     }));
  });
 }); </script>
-- 
1.7.9.5


More information about the Koha-patches mailing list