← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 1638: best performance fix to cross-browser list filtering. Needs more testing across all uses of list ...

 

------------------------------------------------------------
revno: 1638
fixes bug(s): https://launchpad.net/bugs/537672
committer: Saptarshi <sunbiz@xxxxxxxxx>
branch nick: trunk
timestamp: Fri 2010-03-12 12:15:04 +0100
message:
  best performance fix to cross-browser list filtering. Needs more testing across all uses of list filtering inside DHIS
modified:
  dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/util/lists.js


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription.
=== modified file 'dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/util/lists.js'
--- dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/util/lists.js	2009-12-03 07:32:07 +0000
+++ dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/util/lists.js	2010-03-12 11:15:04 +0000
@@ -284,6 +284,21 @@
 }
 
 /**
+ * Creates a hidden select list used for temporarily storing
+ * options for filtering
+ *
+ * TODO: avoid performance hit on page with many selects, by checking use of filterList method
+ */
+$(document).ready(function() {
+    $("select").each(function(i,o){
+        var m = document.createElement("select");
+        m.id = o.id+"_storage";
+        m.style.visibility="hidden";
+        document.body.appendChild(m);
+    });
+});
+
+/**
  * Filters out options in a select list that don't match the filter string by
  * hiding them.
  *
@@ -291,18 +306,26 @@
  * @param listId the id of the list to filter.
  */
 function filterList( filter, listId ) {
-    var list = document.getElementById( listId );
-
-    for ( var i=0; i<list.options.length; i++ ) {
-        var value = list.options[i].text;
-
-        if ( value.toLowerCase().indexOf( filter.toLowerCase() ) != -1 ) {
-            list.options[i].style.display = "block";
-        }
-        else {
-            list.options[i].style.display = "none";
-        }
-    }
+    //Fastest cross-browser way to filter
+    $("#"+listId+" option").filter(function(i) {
+        var toMatch = $(this).text().toString().toLowerCase();
+        var filterLower = filter.toString().toLowerCase();
+        return toMatch.indexOf(filterLower) == -1;
+    }).appendTo("#"+listId+"_storage");
+    $("#"+listId+"_storage option").filter(function(i) {
+        var toMatch = $(this).text().toString().toLowerCase();
+        var filterLower = filter.toString().toLowerCase();
+        return toMatch.indexOf(filterLower) != -1;
+    }).appendTo("#"+listId);
+    var sortedVals = $.makeArray($("#"+listId+" option")).sort(function(a,b){
+        return $(a).text() > $(b).text() ? 1: -1;
+    });
+    $("#"+listId).empty().html(sortedVals);
+    $("#"+listId).val('--');
+
+    /* For FF only - no performance issues
+    $("#" + listId).find('option').hide();
+    $("#" + listId).find('option:Contains("' + filter + '")').show();*/
 }
 
 /**