two new classes for sorting
authorThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Fri, 13 Nov 2009 13:31:30 +0000 (13:31 +0000)
committerThierry Parmentelat <thierry.parmentelat@sophia.inria.fr>
Fri, 13 Nov 2009 13:31:30 +0000 (13:31 +0000)
plekit/php/table.php
plekit/table/table.css
plekit/tablesort/plc_customsort.js [new file with mode: 0644]

index e5e74ea..480bd8b 100644 (file)
@@ -7,6 +7,7 @@ require_once 'prototype.php';
 drupal_set_html_head('
 <script type="text/javascript" src="/plekit/tablesort/tablesort.js"></script>
 <script type="text/javascript" src="/plekit/tablesort/customsort.js"></script>
+<script type="text/javascript" src="/plekit/tablesort/plc_customsort.js"></script>
 <script type="text/javascript" src="/plekit/tablesort/paginate.js"></script>
 <script type="text/javascript" src="/plekit/table/table.js"></script>
 <link href="/plekit/table/table.css" rel="stylesheet" type="text/css" />
index 1ba1fe6..cb38819 100644 (file)
@@ -64,6 +64,8 @@ th.sortable-sortFileSize,
 th.sortable-sortBandwidth,
 th.sortable-sortLastContact,
 th.sortable-sortAlphaNumeric,
+th.sortable-sortAlphaNumericTop,
+th.sortable-sortAlphaNumericBottom,
 th.sortable-sortEnglishDateTime {
     cursor:pointer;
     background: #CAE8EA url(/plekit/icons/tablesort-header-sortable.jpg) no-repeat;
diff --git a/plekit/tablesort/plc_customsort.js b/plekit/tablesort/plc_customsort.js
new file mode 100644 (file)
index 0000000..62ba8bb
--- /dev/null
@@ -0,0 +1,68 @@
+/* $Id$ */
+/*
+    sortAlphaNumeric{Top,Bottom}
+    -----------------------
+
+    These functions are derived from sortAlphaNumeric
+    inputs are expected to be alphaNumeric values e.g. 1, e, 1a, -23c, 54z
+
+    when comparing two values, the following happens
+    (*) if both have a numeric part, then they are compared; if equal the non-numeric part is used
+    (*) if only one has a numeric part, then 
+       . with sortAlphaNumericTop, the non-numeric value (typically n/a) happens first
+       . with sortAlphaNumericBottom, the non-numeric value (typically n/a) happens second
+*/
+function _sortAlphaNumericPrepareData(tdNode, innerText){
+        var aa = innerText.toLowerCase().replace(" ", "");
+        var reg = /((\-|\+)?(\s+)?[0-9]+\.([0-9]+)?|(\-|\+)?(\s+)?(\.)?[0-9]+)([a-z]+)/;
+
+        if(reg.test(aa)) {
+                var aaP = aa.match(reg);
+                return [aaP[1], aaP[8]];
+        };
+
+        // Return an array
+        return isNaN(aa) ? ["",aa] : [aa,""];
+}
+
+/* non_numeric_first : 
+   when comparing, say, '12' with 'n/a':
+   if non_numeric_first is true, we return 1 (n/a greater than 12)
+   otherwise we return -1 
+*/
+function _sortAlphaNumeric(a, b, non_numeric_first) {
+        // Get the previously prepared array
+        var aa = a[fdTableSort.pos];
+        var bb = b[fdTableSort.pos];
+
+        // Check numeric parts if not equal
+        if(aa[0] != bb[0]) {
+               // both are numeric and have different numeric parts : usual comparison
+                if(aa[0] != "" && bb[0] != "") { return aa[0] - bb[0]; };
+               // one numeric value is missing
+                if(aa[0] == "" && bb[0] != "") 
+                       return non_numeric_first ? -1 : 1 ;
+               else 
+                       return non_numeric_first ? 1 : -1 ;
+       // from here and on, aa[0] == bb[0]
+        } else if (aa[1] == bb[1]) {
+         return 0;
+       // the alpha part differ
+       } else {
+         return (aa[1]<bb[1] ? -1 : 1);
+       }
+}
+
+function sortAlphaNumericBottomPrepareData(tdNode, innerText) {
+       return _sortAlphaNumericPrepareData(tdNode, innerText);
+}
+function sortAlphaNumericBottom(a,b,) {
+       return _sortAlphaNumeric (a,b,false);
+}
+function sortAlphaNumericTopPrepareData(tdNode, innerText) {
+       return _sortAlphaNumericPrepareData(tdNode, innerText);
+}
+function sortAlphaNumericTop(a,b,) {
+       return _sortAlphaNumeric (a,b,true);
+}
+