Bug 10792: fix sorting of funds table on acquisitions home page
authorGalen Charlton <gmc@esilibrary.com>
Thu, 5 Sep 2013 16:23:28 +0000 (16:23 +0000)
committerGalen Charlton <gmc@esilibrary.com>
Mon, 16 Sep 2013 15:30:33 +0000 (15:30 +0000)
This patch introduces a DataTables sorting plugin, title-numeric,
for sorting cells based on a decimal number embedded in a span title
attribute.  This allows currency amounts to be formatted properly
for display without having to writing a sorting plugin that's
super-smart about removing the formatting, particularly for locales
that use a comma as the decimal mark.

The sorter plugin can be used like this:

- In the DataTables config:

"aoColumns": [
   { "sType": "title-numeric" },
]

- In the table data

<td><span title="[% decimal_number_that_JS_parseFloat_accepts %]">
    [% formatted currency %]
</span></td>

To test:

[1] Ensure that there is at least one active budget and at least
    one inactive one.
[2] Go to the acquisitions home page.  Note that changing the sort order
    on the amount, ordered, spent, or avail columns results in incorrect
    sorting that is either ASCII-betical or which ignores any component
    of large numbers that occur after the thousands separator.
[3] Apply the patch.
[4] Verify that the sorting now works correctly and that no JavaScript
    errors appear in the JS debug console of your choice.

Signed-off-by: Galen Charlton <gmc@esilibrary.com>
Signed-off-by: Katrin Fischer <Katrin.Fischer.83@web.de>
Sorting now works correctly, for active and inactive funds.
Passes all tests and QA script.

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Galen Charlton <gmc@esilibrary.com>
acqui/acqui-home.pl
koha-tmpl/intranet-tmpl/prog/en/js/datatables.js
koha-tmpl/intranet-tmpl/prog/en/modules/acqui/acqui-home.tt

index 5688452..cd56831 100755 (executable)
@@ -135,7 +135,7 @@ foreach my $budget ( @{$budget_arr} ) {
     }
 
     for my $field (qw( budget_amount budget_spent budget_ordered budget_avail ) ) {
-        $budget->{$field} = $num_formatter->format_price( $budget->{$field} );
+        $budget->{"formatted_$field"} = $num_formatter->format_price( $budget->{$field} );
     }
 
     push @budget_loop, $budget;
index fc24036..d243392 100644 (file)
@@ -495,6 +495,34 @@ jQuery.extend( jQuery.fn.dataTableExt.oSort, {
     }
 } );
 
+
+/* Plugin to allow sorting on numeric data stored in a span's title attribute
+ *
+ * Ex: <td><span title="[% decimal_number_that_JS_parseFloat_accepts %]">
+ *              [% formatted currency %]
+ *     </span></td>
+ *
+ * In DataTables config:
+ *     "aoColumns": [
+ *        { "sType": "title-numeric" },
+ *      ]
+ * http://datatables.net/plug-ins/sorting#hidden_title
+ */
+jQuery.extend( jQuery.fn.dataTableExt.oSort, {
+    "title-numeric-pre": function ( a ) {
+        var x = a.match(/title="*(-?[0-9\.]+)/)[1];
+        return parseFloat( x );
+    },
+
+    "title-numeric-asc": function ( a, b ) {
+        return ((a < b) ? -1 : ((a > b) ? 1 : 0));
+    },
+
+    "title-numeric-desc": function ( a, b ) {
+        return ((a < b) ? 1 : ((a > b) ? -1 : 0));
+    }
+} );
+
 (function() {
 
     /* Plugin to allow text sorting to ignore articles
@@ -537,4 +565,4 @@ jQuery.extend( jQuery.fn.dataTableExt.oSort, {
         }
     });
 
-}());
\ No newline at end of file
+}());
index 2ee9d0b..4fabffe 100644 (file)
@@ -32,10 +32,10 @@ $(document).ready(function() {
             null,
             null,
             null,
-            null,
-            { "sType": "num-html" },
-            { "sType": "num-html" },
-            null
+            { "sType": "title-numeric" },
+            { "sType": "title-numeric" },
+            { "sType": "title-numeric" },
+            { "sType": "title-numeric" }
         ],
         'sDom': 't',
         'bPaginate': false,
@@ -154,10 +154,10 @@ $(document).ready(function() {
                     [% END %]
                 </td>
                 <td>[% loop_budge.budget_branchname %]</td>
-                <td class="data">[% loop_budge.budget_amount %]</td>
-                <td class="data"><a href="ordered.pl?fund=[% loop_budge.budget_id %]&amp;fund_code=[% loop_budge.budget_code %]">[% loop_budge.budget_ordered %]</a></td>
-                <td class="data"><a href="spent.pl?fund=[% loop_budge.budget_id %]&amp;fund_code=[% loop_budge.budget_code %]">[% loop_budge.budget_spent %]</a></td>
-                <td class="data">[% loop_budge.budget_avail %]</td>
+                <td class="data"><span title="[% loop_budge.budget_amount %]">[% loop_budge.formatted_budget_amount %]</span></td>
+                <td class="data"><span title="[% loop_budge.budget_ordered %]"><a href="ordered.pl?fund=[% loop_budge.budget_id %]&amp;fund_code=[% loop_budge.budget_code %]">[% loop_budge.formatted_budget_ordered %]</a></span></td>
+                <td class="data"><span title="[% loop_budge.budget_spent %]"><a href="spent.pl?fund=[% loop_budge.budget_id %]&amp;fund_code=[% loop_budge.budget_code %]">[% loop_budge.formatted_budget_spent %]</span></a></td>
+                <td class="data"><span title="[% loop_budge.budget_avail %]">[% loop_budge.formatted_budget_avail %]</td>
             </tr>
         [% ELSE %]
             <tr class="b_inactive">
@@ -177,10 +177,10 @@ $(document).ready(function() {
                     [% END %]
                 </td>
                 <td>[% loop_budge.budget_branchname %]</td>
-                <td class="data">[% loop_budge.budget_amount %]</td>
-                <td class="data"><a href="ordered.pl?fund=[% loop_budge.budget_id %]&amp;fund_code=[% loop_budge.budget_code %]">[% loop_budge.budget_ordered %]</a></td>
-                <td class="data"><a href="spent.pl?fund=[% loop_budge.budget_id %]&amp;fund_code=[% loop_budge.budget_code %]">[% loop_budge.budget_spent %]</a></td>
-                <td class="data">[% loop_budge.budget_avail %]</td>
+                <td class="data"><span title="[% loop_budge.budget_amount %]">[% loop_budge.formatted_budget_amount %]</span></td>
+                <td class="data"><span title="[% loop_budge.budget_ordered %]"><a href="ordered.pl?fund=[% loop_budge.budget_id %]&amp;fund_code=[% loop_budge.budget_code %]">[% loop_budge.formatted_budget_ordered %]</a></span></td>
+                <td class="data"><span title="[% loop_budge.budget_spent %]"><a href="spent.pl?fund=[% loop_budge.budget_id %]&amp;fund_code=[% loop_budge.budget_code %]">[% loop_budge.formatted_budget_spent %]</span></a></td>
+                <td class="data"><span title="[% loop_budge.budget_avail %]">[% loop_budge.formatted_budget_avail %]</td>
             </tr>
         [% END %]
         [% END %]