javascript - Filtering on string starting value -


i have search bar show rows cell contains specific value, want ones start value, if start it, not if contain in middle of string.

this have:

var $rows = $('#tableid tbody tr');  $('#search').keyup(function() {     var val = $.trim($(this).val()).replace(/ +/g, ' ').tolowercase();      $rows.show().filter(function() {         var text = $('.filterclass', this).text().replace(/\s+/g, ' ').tolowercase();         return !~text.indexof(val);     }).hide(); }); 

what code does: using indexof test presence of val (the ~ operator converting -1 0 "falsy" , !~text.indexof(val) testing whether val anywhere in text).

solution: check if indexof 0 (in other words, val begins @ index 0 in text):

var $rows = $('#tableid tbody tr');  $('#search').keyup(function() {     var val = $.trim($(this).val()).replace(/ +/g, ' ').tolowercase();      $rows.show().filter(function() {         var text = $('.filterclass', this).text().replace(/\s+/g, ' ').tolowercase();         return text.indexof(val) !== 0;     // <-- note line     }).hide(); }); 

side note: simpler understand code means "only show rows this" understand "hide rows not this". suggest changing code's structure this:

 $rows.hide()  $rows.filter(/* filter code */).show() 

(this mean removing exclamation mark return statement well)


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -