javascript - Instant search using optional Logical Operators to filter results -


i'm trying write search function can use logical operator filter results. way code works can type out string search or can put < or > @ beginning of text field filter greater or less numerical values. i'm looking better version or way write it. have (semi) working version, way wrote have put 1 or 2 zeros if value isn't more 3 digits. example, <050 has used search items under 50. using <50 brings wrong results. in addition, if result under 100 i'm having convert value 5 005. works if largest value under 1k otherwise have convert 0005.

function fulllistsearch() {    $("#infosearch").keyup(function() {     var v = this.value;     var value = this.value.tolowercase();     operator = value[0];     len = $(this).val().length;      if ( operator == ">" ) {        $("#info").find(".fulllist").each(function() {          var ths = $(this).text();          var npattern = /[0-9]+/g;          var nmatches = ths.match(npattern);          var valn = v.match(npattern);      if (nmatches.length > "1") {        if (nmatches[1].length == "1") { nmatches[1] = "00" + nmatches[1] };           $(this).show();        if (nmatches[1] < valn) {               $(this).toggle();        }      }      else { alert("the > operator not valid object");              return false;           }     });   }    else if ( operator == "<" ) {      $("#info").find(".fulllist").each(function() {      var ths = $(this).text();      var npattern = /[0-9]+/g;      var nmatches = ths.match(npattern);      var valn = v.match(npattern);       if (nmatches.length > "1") {         if (nmatches[1].length == "1") { nmatches[1] = "00" + nmatches[1] };         $(this).show();         if (nmatches[1] > valn) {             $(this).toggle();       }      }      else { alert("the > operator not valid object");              return false;      }     });   }    else {      $("#info").find(".fulllist").each(function() {       var id = $(this).first().text().tolowercase()           $(this).toggle(id.indexof(value) !== -1);     });     }   }); } 

i use like:

function fulllistsearch() {     $("#infosearch").keyup(function () {         var v = this.value;         var value = this.value.tolowercase();         var operator = value[0];          var compfunc = function (a, b) {             return == b;         };          if (operator == '>' || operator == '<') {             value = value.substring(1);              if (operator == '>') compfunc = function (a, b) {                 return > b;             };             else // '<'              compfunc = function (a, b) {                 return < b;             };         }         var numval = parseint(value);         if (isnan(numval)) alert("invalid input!");          $("#info").find(".fulllist").each(function () {             var val = parseint($(this).text());             $(this)[compfunc(val, numval) ? "show" : "hide"]();         });     }); } 

i haven't checked this, idea should clear.

few notes:

  1. regular expressions not best way validate, , if so, use more general such /^[><]?\d+$/ validate entire thing.

  2. why comparing each digit, when can compare numbers? use parseint().

  3. i wouldn't fire whole code on every keyup, wait time in order see if new key pressed. like:

     $("#infosearch").keyup(function () {       var timeout = $(this).data("timeout");       if (timeout) cleartimeout(timeout);       $(this).data("timeout", settimeout(function () {...........}, 200); // milliseconds  } 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -